Maven not honoring command line arguments
I have a project that I am trying to install with maven. pom.xml
has several properties in it that change when the maven install command is run depending on whatever version of the library we are trying to build with:
<properties>
<some-version>0</some-version>
</properties>
Zero is a placeholder here as we will always specify the legal version during our build process. This version is then referenced later in pom.xml
to indicate a few dependencies:
<dependencies>
<dependency>
<groupId>com.mycompany.myproduct</groupId>
<artifactId>someOtherProject</artifactId>
<version>${some-version}</version>
</dependency>
</dependencies
Construction is done with make with the following command line:
mvn -Dsome-version=1.6.2
Maven can version correctly and build as expected. However, the version installed in my local maven repository (/home/user/.m2) does not have the correct version. Installed pom.xml
does not have an updated version installed on the command line:
user@ubuntu:~/$ cat /home/user/.m2/repository/com/mycompany/myproduct/myproject/1.0.0/myproject-1.0.0.pom | grep some-version -C 1
<properties>
<some-version>0</some-version>
</properties>
--
<artifactId>someOtherProject</artifactId>
<version>${some-version}</version>
</dependency>
user@ubuntu:~/$
This prevents any other project that depends on my project from being able to build it as maven will complain that it cannot find version 0 of someOtherProject:
[ERROR] Failed to execute goal on project myproject:
Could not resolve dependencies for project mycompany.myproduct:myproject:jar:1.0.0:
The following artifacts could not be resolved: com.mycompany.myproduct:someOtherProject:jar:0,
Could not find artifact com.mycompany.myproduct:someOtherProject:jar:0 in central (https://mycompany.com/artifactory/repo/) -> [Help 1]
What do I need to do for maven to install with an updated version in the pom? Obviously a terrible hacky solution would be to use sed and modify the pom file directly, but it seems like Maven should be able to actually use command line options when installing the pom. Otherwise, the ability to set arguments on the command line seems extremely limited in efficiency.
source to share
In @JoopEggen's advice, I looked deeper into the maven versions plugin. He suggested an update-property target that would actually update the pom.xml value on disk instead of just rewriting it during the build phase. I was able to solve my problem by calling
mvn versions:update-property -Dproperty=some-version -DnewVersion=1.6.2 -DsearchReactor=false -DallowSnapshots=true
in the makefile before calling mvn install. Disconnecting the reactor was necessary to prevent the plugin from crashing on values it could not find in the remote repo (see here ), and allowSnapshots allows me to use version numbers like 1.6.2-SNAPSHOT useful when testing.
source to share
Better you can set your property to pom.xml
in a tag <properties>
like this -
<properties>
<property>
<name>some-version</name>
<value>1.6.2</value>
</property>
</properties>
If you use this, you don't have to provide the property every time you issue a command mvn
from the terminal.
source to share