How to get a command-line property to overwrite a maven property - maven

I have a pom file in which version numbers of some dependencies rely on the project version property specified in the settings of the pom file. Can I overwrite this via the command-line? If so, how?
Here's the long story:
We are currently transitioning our projects to maven, but we're not all the way there yet. There are multiple modules that are still not built with maven and are therefore dependencies in our project (they are built into jars through ant). Upon release, we want all of these jars to be built and contain the same version number as the parent project. For a release, two steps are performed (until we can get everything using maven)
The jars are built in ant with the correct release version (12.12.4.0).
The maven release plugin is used to deploy the project to our artifact repository.
In the second step command-line arguments are used to specify the release:
mvn release:prepare -DreleaseVersion=12.12.4.0 -DdevelopmentVersion=12.12.4.1-SNAPSHOT -Dtag=iv-12.12.4.0
I would like the pom file to be updated with the version specified. However, when this command is run, the version within the pom file (12.12.4.0-SNAPSHOT) is still being used. This fails the "checking dependencies and plugins for snapshots" step and I am required to resolve my jars that still have the 12.12.4.0-SNAPSHOT version used from the maven version property.
This led me to the original question of how I can override this so that the version resolves to that specified on the command line.
Additional questions that could get me past this is:
How to allow the maven release plugin to update the pom file before this check?
How to skip the snapshot check (not desirable)
I could create a property within the pom file that I can overwrite, but then I'd have to maintain the version number in two places within the pom file.
Thoughts?

Put parameters directly to pom from command line for example:
mvn clean install -Dtestng.version=6.3.1
Example:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<name>test</name>
<properties>
<testng.version>6.4</testng.version>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
If you run it normally testng version 6.4 will be used. But if you run it like: mvn clean install -Dtestng.version=6.3.1 testng version 6.3.1 will be used.
See Setting Maven params in Jenkins

Here is one more option:
<Logger name="com.mypackagename" level="${env:LOG_LEVEL:-error}" additivity="false">
<AppenderRef ref="Console-Appender"/>
</Logger>
${env:LOG_LEVEL:-error} means env = environment variable, LOG_LEVEL = env name, -error = default level error.

Related

versions-maven-plugin:update-property not updating pom.xml

Hi Meier I have used the following goal:
mvn versions:update-property
-Dproperty="emom.web.dependency.shr.version"
-Dincludes:org.safeway.com:emom-shr
-DgenerateBackupPoms=false
-DallowIncrementalVersios=true
-DallowSnapshots=true
clean package
My Job B pom.xml is:
<dependency>
<groupId>com.safeway.app</groupId>
<artifactId>emom-shr</artifactId>
<version>${emom.web.dependency.shr.version}</version>
</dependency>
Under the properties it has the version hard-coded:
<emom.web.dependency.shr.version>19.6.5-SNAPSHOT</emom.web.dependency.shr.version>
My Job A pom.xml:
<groupId>com.safeway.app</groupId>
<artifactId>emom-shr</artifactId>
<version>20.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
When I run the above goal, Maven is picking the latest version (i.e. 20.1.0) from Artifactory but when I check the pom.xml of Job B under properties it still says 19.6.5. I need a way to change the 19.6.5 or current version to latest version available. Am I doing something wrong? I'm not able to figure it out.
Here's an example of versions-maven-plugin:update-property working in practice. I've used the common Mockito library as an example that works for everyone as it's in Maven Central.
Starting with this POM (noting the mockito-version property):
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>abc</groupId>
<artifactId>def</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<mockito-version>2.22.0</mockito-version>
</properties>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
The simplest way to upgrade it to the latest release version is this:
mvn versions:update-property -Dproperty=mockito-version
Replace mockito-version with emom.web.dependency.shr.version in your case.
You can then start to use more of the goal options to adjust the options. For example, you might:
Permit snapshots, not just releases, with -DallowSnapshots=true.
Disallow major version updates (i.e. third element from the right) with -DallowMajorUpdates=false. Note that the logic around these version number sections seems a bit flaky in the plugin - or isn't how I expect.
Avoid creating backup POMs with -DgenerateBackupPoms=false. This is cleaner, but if you omit this option then you can use mvn versions:revert to get yourself back to where you started.
To apply this to your scenario, I think you need to:
Check you've not got typos in your actual command (like you have in the question and comments).
Get rid of options that don't appear in the options.
Probably, keep things simple by not trying to run this in conjunction with anything else (unless it's in automation), so get rid of the clean package at the end of the command.

Change version of Maven project without manipulating the POM file

Is it somehow possible to change the version of a Maven project without manipulating the POM file?
Let's say I have a Maven project with version 1.5.0-SNAPSHOT but I want to build it as 1.5.46.
The Versions Maven Plugin unfortunately modifies the POM files.
Since Maven 3.5.0 this is possible using a special predefined property: ${revision}. Define the property with a default value (e.g. 1.5.0-SNAPSHOT) and when needed, set it during execution to a specific version (e.g. 1.5.46).
For example, define the following in your pom.xml:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>foo</artifactId>
<name>Foo Module</name>
<version>${revision}</version>
...
<properties>
<revision>1.5.0-SNAPSHOT</revision>
</properties>
</project>
Build it using the default value:
mvn clean install
This will produce an artifact identified as org.example:foo:1.5.0-SNAPSHOT.
In order to build a specific version, set the revision property, for example:
mvn clean install -Drevision=1.5.46
This will produce an artifact identified as org.example:foo:1.5.46.
For further details, see the Maven CI Friendly Versions page.
Try to override project version with
mvn versions:set -DnewVersion=<version>
in your particular case:
mvn versions:set -DnewVersion=1.5.46
You can
Make a copy of your pom as temppom.xml
Replace the version in temppom.xml
Build with mvn -f temppom.xml.
Delete temppom.xml.
Maven supports delivery friendly versions, see https://issues.apache.org/jira/browse/MNG-5576 .
For more details I would suggest to talk with #khmarbaise
The plugin provides a goal to revert the changes made by it:
mvn versions:revert

maven - remove -SNAPSHOT from properties tag in pom.xml

The parent pom.xml of my application contains the following snippet:
...
<version>2.0-SNAPSHOT</version>
...
<properties>
<property.version>1.0-SNAPSHOT</property.version>
</properties>
Essentially, I would like to update both the -SNAPSHOT versions to release versions. i.e., I want my resultant pom.xml to be like:
...
<version>2.0</version>
...
<properties>
<property.version>1.0</property.version>
</properties>
[Note: v1.0 of <property.version> is not yet deployed to the artifactory and also might not be the latest version available
There might exist more properties with SNAPSHOT versions that I do not want to update to release versions]
I figured out a way to update the <version> using maven versions:set plugin. However, I could not find any solution for updating the version (by removing -SNAPSHOT) inside the <properties> tag.
I've looked at documentations for versions:update-properties, versions:update-property.
I am trying to achieve this by using the mvn versions plugin, otherwise I would have to write a script (eg. shell) which would parse and do the needful.

Jenkins fails to build multi-module Maven project

I have a multi-module Maven project where I have multiple micro services as modules so I have modules listed in my parent pom.xml like below:
<modules>
<module>core</module>
<module>model-base</module>
<module>module1</module>
<module>module2</module>
...
<module>module5</module>
<module>module7</module>
<module>module6</module>
</modules>
Here the module7 is dependent on module5, 6 so I have dependencies listed like below in my module7 pom.xml:
<parent>
<artifactId>pojectA</artifactId>
<groupId>com.domain</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>module7</artifactId>
<dependencies>
<dependency>
<groupId>com.domain</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.domain</groupId>
<artifactId>module5</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.domain</groupId>
<artifactId>module6</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
When I run mvn clean package in my local the module5, 6 called before the module7 as expected but in Jenkins it is trying to build module 5 then module7 making build fail saying:
[ERROR] Failed to execute goal on project module7: Could not resolve dependencies for project module7:jar:1.0-SNAPSHOT: Could not find artifact module6:jar:1.0-SNAPSHOT -> [Help 1]
Do I need to run any other jobs or re-order the modules in my pom.xml, how is it differ from local to Jenkins? Appreciate any help on this.
The order of modules is not relevant. Maven recognizes which project depends on which other project(s) and sets the build order in the reactor accordingly. See POM Reference, Aggregation (or Multi-Module):
You do not need to consider the inter-module dependencies yourself when listing the modules, i.e. the ordering of the modules given by the POM is not important. Maven will topologically sort the modules such that dependencies are always build before dependent modules.
Add Pre-Step as per below attached screenshot. This will compile all your top modules.
Then we can execute which ever module we want.
As is probably quite well understood, the issue is that the dependencies between the child modules fail because they aren't installed in the local repository yet (because they are yet to be built). The goal that causes this (for me anyway) is mvn test, which is invoked by mvn package. Your local build probably works because at some point you've done a mvn install and this has bootstrapped your system.
In Jenkins the only way I've found to make these builds work is to use the Pre-build step invoking a Maven target of install, and then build the main step as usual.

Maven copy parents declared in the POMs

My final goal is to create a Maven repository in a certain directory containing only a certain set of artifacts and all their dependencies.
For this I use the following command:
mvn.bat dependency:copy-dependencies -f dependencies.pom
-DoutputDirectory=localRepoDir -Dmdep.useRepositoryLayout=true
-Dmdep.copyPom=true -Dmdep.addParentPoms=true
dependencies.pom being:
<project>
<modelVersion>4.0.0</modelVersion>
<description>Dependencies</description>
<groupId>com.dummy</groupId>
<artifactId>dummy</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.dependency1</groupId>
<artifactId>dep1</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.dependency2</groupId>
<artifactId>dep2</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
When doing this, I notice that parents declared in the dependencies' poms are not copied from the .m2 Local maven repository to the destination directory.
Perhaps I'm missing something and there's a better way to do this, since it's kind of a hack to use a pom file to declare the artifacts I want to copy (together with their dependencies).
Turns out that maven was using version 2.8 as default for the dependency plugin. When explicitly indicating it to use the latest version (2.10), it worked just fine.
The addParentPoms parameter was already introduced on 2.8 for copy-dependencies, so I guess it must be a bug in the 2.8 release.
mvn org.apache.maven.plugins:maven-dependency-plugin:2.10:copy-dependencies

Resources