I have a multiple maven module project. I need to retrieve a module's current version.
Parent Pom (2.4.0)
|- module1 1.5.0
|- module2 1.3.4
|- module3 1.2.5
Get version of module2
would return 1.3.4
.
FYI: module1
maybe a dependency of module2
.
Found one way of doing it was
mvn depedency:tree -X | awk '/module1/ {split($0, a, ":"); if (a[4]) {print a[4]; exit}}'
Would prefer a cleaner way of doing it.
There are a couple of solution, but they're all based on the same logic:
mvn -f model2 org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate -Dexpression=project.version -q -DforceStdout
With -f model2
or -f model2/pom.xml
you specify the path to the pom.xml
With org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate
you call the evaluate goal of the maven-help-plugin. If the pom already has a locked version on this plugin, you can simply use help:evaluate
With -Dexpression=project.version
you specify what you want to know. In case it starts with project, you're following the xml structure of the pom.
With -q
you suppress the output logging as handled by the logging framework of Maven.
But that would also suppress the result of the evaluate goal too. Hence by using -DforceStdout
the result will be visible, because it bypasses the logging framework.