Is there a way to set the Maven version number dynamically? - maven

I would like to use Maven to produce an artifact in zip format. To give you some background; my project includes an APS package (Application Packaging Standard, used to provision cloud applications on the Parallels platform). This package is a zip file that contains a combination of XML as well as PHP files. It is generated by an APS plugin from within Eclipse and its name always includes the version and release number of its contents.
What I am trying to do is generate a zip file with Maven that would be kind of a release candidate that will be eventually sent to customers and would include not only the actual APS package but also other files such as README, User Guide.pdf, etc;. I would like the name of this zip file to contain the version number of the version number of the APS package. Currently I can generate this manually by using something like "mvn -Dversion=1.2.3-4 package" but I would like to automate the process and ideally run this from Jenkins.
Basically, my strategy is to run a script that would extract the version number from the initial APS package, once that is done, my script can invoke Maven and can pass this parameter to it so it can generate the final zip with the proper version number. This is fine but again, I need to run this script manually and I am looking for an automated process.
My question is; is it possible to invoke this script from within Maven and use its return as a parameter to set the version name (or the name of the file that will be generated) at run time? As I mentioned, I would like eventually Jenkins to handle this. It can pick up the pom file but I am not sure how it could kind of "auto configure" itself to have the proper version number.
Thanks is advance.

From jenkins build you can use profile with ${BUILD_NUMBER}:
<profile>
<id>jenkins</id>
<build>
<finalName>${artifactId}-${version}-${BUILD_NUMBER}</finalName>
</build>
</profile>
Then run in jenkins:
clean install -Pjenkins

I use the SVN (or any source versioning system) version to identify the software builds.
By simply executing this
REVISION=`svn info | grep '^Revision:' | sed -e 's/^Revision: //'`
on the sourcers folder you get the right value in $REVISION, then you can use it for your maven build
mvn -Dversion=1.2.3-$REVISION package
easy and clean

Related

Set up Jenkins to run a specific maven command

I’m new to Jenkins and currently working on a maven project.
I am able to run a simple Jenkins job using maven commands.
mvn clean install
However, the extended requirement requires me to us an additional parameter in the maven command
mvn clean install -DfileName=file1
Is it possible to have a drop down with file names (e.g. file1, file2 ..) and have the user selected one append to the maven command.
mvn clean install -DfileName = {selected filename from dropdown}.
Could some one please assist with this along with what plugin and how can I setup.
Parameterize your jenkins job see https://wiki.jenkins.io/plugins/servlet/mobile?contentId=34930782#content/view/34930782.
Use choice parameter to add your file name choices
Active Choices Plugin - https://wiki.jenkins.io/display/JENKINS/Active+Choices+Plugin
The user selected choice can be used in your maven command using "{params.param_name}".

Getting unique version numbers with Jenkins and Maven

I'm working on a multi module maven project with Jenkins. I have a Build-Job which I want to package my project with a unique version number and deploy it to Nexus.
The version number should be - where the "maven-version" is the version maintained in the root POM and "build-number" is Jenkins' job build number. For example: 1.2.3-1234
I'm using the maven-versions-plugin to set the desired version number but I have the problem that I want to have the first part of the version (1.2.3) maintained in the POM. So I need a way to extrakt the version from the POM into a Jenkins environment variable.
Until yet I found no direct way of doing this. My solution is to use a groovy script which pares the POM and writes the version number into a temporary property file. After that I use the EnvInject plugin to create the environment varaible for later reinjecting as version number for the "mvn versions:set" command.
My groovy script:
import jenkins.util.*;
def project = new XmlSlurper().parse(new File("pom.xml"))
def version = project.version.toString();
def mainversion = version.substring(0, version.indexOf("-SNAPSHOT"));
println "Version: $mainversion";
def versionFile = new File("v.properties");
versionFile << "VERSION=$mainversion";
This indirection through the property file is very ugly and error prone. Is there any way to directly create an environment variable within the groovy script? It is possible using a system groovy script but these kind of scripts are always executed on the master. So my job will not be runable on slaves which doesn't work for me because I do not execute builds on the master.
Welcome to another version of the Jenkins Chain upstream-downstream variable problem. I assume you are probably trying to do some sort of continuous integration pipeline.
At my company, we solved this one of two ways:
Option 1: Brute force by doing a sed search and replace as a execute shell in the beginning:
#sed -i '0,/<version>.*<\/version>/s/<version>.*<\/version>/<version>'${SVN_REVISION}'<\/version>/g' pom.xml;
#sed -i '0,/<name>.*<\/name>/s/<name>.*<\/name>/<name>'"${JOB_NAME}"'<\/name>/g' pom.xml;
#find . -name pom.xml | xargs sed -i 's/${build.number}/'${SVN_REVISION}'/g';
Option 2: Use Artifactory instead of Nexus and pay for Artifactory Pro. The Build/Release Management functions of the Artifactory jenkins plugin have saved us hours and hours of this kind of grief.

Activate a profile based on environment

Here's my scenario:
Maven 2.0.9 is our build system
We install code to multiple environments
All of our environment-specific properties are contained in property files, one for each environment
We currently read these properties into maven using the properties-maven-plugin; this sub-bullet is not a requirement, just our current solution
Goal:
Perform certain parts of the build (ie. plugin executions) only for certain environments
Control which parts are run by setting values in the environment-specific property files
What I've tried so far:
Maven allows plugins executions to be put inside pom profiles, which can be activated by properties; unfortunately these must be system properties - ie. from settings.xml or the command-line, not from properties loaded by the properties-maven-plugin
If possible, we'd like to keep everything encapsulated within the build workspace, which looks something like this:
project
pom.xml
src
...
conf
dev.properties
test.properties
prod.properties
build-scripts
build.groovy <-- the script that wraps maven to do the build
install.groovy <-- ... wraps maven to do the install
Running a build looks like:
cd build-scripts
./build.groovy
./install.groovy -e prod
Is there any possible way to accomplish these goals with the version of maven we are using? If not, is it possible with a newer version of maven?
This isn't possible using just Maven. (See also How to activate profile by means of maven property?) The reason is that profiles are the first thing evaluated before anything else to determine the effective POM.
My suggestion is to write some preprocessor that parses your environment specific property files and converts them to the required system properties before launching Maven. This script can be included in your ~/.mavenrc so that it runs automatically before Maven is launched. Here is an example script that that assumes the properties file is in a fixed location:
properties=`cat /etc/build-env.properties`
while read line; do
MAVEN_OPTS="$MAVEN_OPTS -D$line"
done <<< "$properties"
If the properties file is not fixed, you'll just need to add something to the script to discover the location (assuming it is discoverable).

How to get Maven dependencies printed to a file in a readable format?

I am working on a big projects with many pom.xml files and I need to specify all the libraries that I use. This means that I need to read pom.xml files recursively and get groupId, artifactId, scope and version. I checked out mvn dependency:tree but I can't find a way to print it to a file in a readable format. I saw appendOutput but I saw no example on how to use it in cmd. I saw some solutions done in Linux but I only have access to Windows XP.
This can (at least now) be done with command line options to the dependency:tree plugin.
Try:
mvn dependency:tree -Doutput=/path/to/file
Reference: Maven Dependency Plugin Page
You only asked about "readable" format, but you can also pass the -DoutputType parameter with various options. Also note that the version I have installed, I get the following warning:
[WARNING] The parameter output is deprecated. Use outputFile instead.
So, consider trying it with -DoutputFile=/path/to/file
Also, I was unable to get the -DoutputType paramater to give me anything other than the default text, but didn't have a chance to play around with it. YMMV.
If you have multiple modules under the same repo/project and want the dependencies of all the modules in one file, so as to be able to diff b/w one build and another to see if something changed somewhere, you can do
$project_dir> mvn dependency:tree -DoutputFile=<absolute_path_to_file> -DappendOutput=true
e.g.
$project_dir> mvn dependency:tree -DoutputFile=`pwd`/mvn_dependency_tree.txt -DappendOutput=true
See other options available at https://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html
Adding the
<plugin>
<groupId>org.apache.servicemix.tooling</groupId>
<artifactId>depends-maven-plugin</artifactId>
</plugin>
plugin produces a classes/META-INF/maven/dependencies.properties file with the project dependencies easily parseable.
Example of the output produced:
# Project dependencies generated by the Apache ServiceMix Maven Plugin
# Generated at: Mon Oct 10 17:43:00 CEST 2011
groupId = my.group.name
artifactId = my.artifact.name
version = 0.0.1-SNAPSHOT
my.group.name/my.artifact.name/version = 0.0.1-SNAPSHOT
# dependencies
junit/junit/version = 4.8
junit/junit/type = jar
junit/junit/scope = test
org.easymock/easymock/version = 2.4
org.easymock/easymock/type = jar
org.easymock/easymock/scope = test
On GNU/Linux I would just do mvn dependency:tree > myFile. However, if you're restricted to Windows only, than I would look for Windows' syntax for streaming the output of a command.
According to this site (just a top-results from Google) it seems that Windows' console also use > sign to direct the output stream to i.e. a file.
So would you mind trying this?
I have run the below command and got the file having all the maven dependency.
mvn dependency:tree -DoutputFile=temp/mvn_dependency_tree.txt
This command will create a folder named "temp" and inside this folder a file name mvn_dependency_tree.txt will be created with all the dependencies.
You can always install MinGW and MSYS and then use the Linux examples using dependency:tree in Windows
Perhaps effective-pom (in conjunction with some linux commands for saving the file) can be sufficient for your needs.

How do I specifiy or override the file and path name for ftp publish location after a hudson build?

I am using hudson and the "Publish artifacts to FTP" option. It makes up its own directory based on the date and time of the build. I would like to override that with a fixed name/location. How can I do that? Is it possible?
What version of the FTP Publisher plugin are you using? I just installed the 1.0 version on Hudson 1.361, and in that version I can control the path by selecting the Flatten files option in the job configuration. Timestamp directories can also be disabled (that was the default).
That would solve path control. For the filename control, there doesn't seem to be a way to rename the file. You will either have to create an artifact with the right name during the build, or use some other tool for the ftp upload.
Edit: For example, the Post Build task, that can run arbitrary shell commands based on the result of the build.

Resources