0

Given a property defined inside the pom, can I refer to that property from the command line?

This is what I would like to achieve:

# instead of:
mvn versions:set -DnewVersion=x.y.z
# something like this:
mvn versions:set -DnewVersion=properties:library.version

This way I can manage my multi-modular project version in one place and update all modules at the same time.

Is something like this possible? Perhaps by preconfiguring the version plugin inside the pom itself and completely omit the newVersion property from the command line?

Benny Bottema
  • 11,111
  • 10
  • 71
  • 96
  • maybe this helps? https://stackoverflow.com/questions/7513319/passing-command-line-arguments-from-maven-as-properties-in-pom-xml – OldProgrammer Feb 08 '19 at 20:12
  • I don't think that would help me, I don't want to provide the value from outside. I just want the *version plugin* to use the property already there. This triggers me however, perhaps I can explicitly configure the *version plugin* in the pom itself? – Benny Bottema Feb 08 '19 at 20:18
  • If you like to use properties which are already defined inside the pom file you should configure versions-maven-plugin in your pom accordingly. Best would be pluginManagement. And you can use [configuration per goal](https://maven.apache.org/docs/3.3.1/release-notes.html#Plugin_Goal_Invocation_from_Command_Line) – khmarbaise Feb 08 '19 at 20:25

2 Answers2

0

It's actually very easy, just provide the newVersion property in the pom:

<properties>
    <library.version>1.0.0</library.version>
    <newVersion>${library.version}</newVersion>
</properties>

Then simply execute versions:set without providing the property and it will pick up on the property from the POM instead:

mvn versions:set


Alternative solution:

You can also skip the property and have maven dynamically update the current version:

The following increases patch version:

mvn build-helper:parse-version versions:set -DnewVersion=${parsedVersion.nextMajorVersion}.${parsedVersion.minorVersion}.${parsedVersion.IncrementalVersion} versions:commit

This is built into Maven and works out-of-the-box.

Benny Bottema
  • 11,111
  • 10
  • 71
  • 96
0

You actually have a better way to reach you goal: https://maven.apache.org/maven-ci-friendly.html

Idea is to use property ${revision} instead of version value.

So in all you POMs you do something like that:

Parent POM:

<groupId>my.group</groupId>
<artifactId>my.artifact</artifactId>
<version>${revision}</version>

Sub-modules:

<parent>
    <groupId>my.group</groupId>
    <artifactId>my.artifact</artifactId>
    <version>${revision}</version>
</parent>

Now at the root of the project (where you project parent POM) you provide /.mvn/maven.config file (FYI: https://maven.apache.org/docs/3.3.1/release-notes.html (JVM and Command Line Options)):

/.mvn
    maven.config
/submodule-one
/submodule-two
pom.xml

maven.config contains setting of the version to the property: -Drevision=1.1.10-SNAPSHOT

And do not forget to provide in the parent POM flatten-maven-plugin configuration with flattenMode=resolveCiFriendliesOnly, exactly like described in the documentation by link above.

As result, maven.config is the only place where you need set version for all you modules. And you not need to change anything in POMs when you want to change version.

But to make it works you need at least maven 3.5.0-beta-1.

P.S. you can see all of that in my maven testing project: https://github.com/Gmugra/net.cactusthorn.maven

Gmugra
  • 468
  • 7
  • 10