I would like to retrieve from the command line the groupId, the artifactId & the version of a Maven project.
The proposed solution in this topic "How to get Maven project version to the bash command line" is to use the following plugin:
mvn org.apache.maven.plugins:maven-help-plugin:2.2:evaluate -Dexpression=project.artifactId
It works well but I can't figure out how to set, at the same time, the project.groupId, project.artifactId & project.version
to the -Dexpression argument.
I would avoid launching 3 times the Maven command with a different -Dexpression argument each time...
Thks
local pom_groupid=`mvn org.apache.maven.plugins:maven-help-plugin:2.2:evaluate -Dexpression=project.groupId |grep -Ev '(^\[|Download\w+:)'`
local pom_artifactid=`mvn org.apache.maven.plugins:maven-help-plugin:2.2:evaluate -Dexpression=project.artifactId |grep -Ev '(^\[|Download\w+:)'`
local pom_version=`mvn org.apache.maven.plugins:maven-help-plugin:2.2:evaluate -Dexpression=project.version |grep -Ev '(^\[|Download\w+:)'`
The groupId is a parameter indicating the group or individual that created a project, which is often a reversed company domain name. The artifactId is the base package name used in the project, and we use the standard archetype.
groupId This element indicates the unique identifier of the organization or group that created the project. The groupId is one of the key identifiers of a project and is typically based on the fully qualified domain name of your organization. For example org. apache. maven.
Here's another approach that doesn't require creating a Maven plugin even though Olivier has shown that it is pretty easy to make one.
mvn -q -Dexec.executable=echo -Dexec.args='${project.groupId} ${project.artifactId} ${project.version}' --non-recursive exec:exec 2>/dev/null
Note that this is tailored to the linux environment. On windows you could probably create a batch file that prints its input or something.
One drawback of this approach is that you may have to add | grep -v "something"
to the very end of the command above (after the 2>/dev/null
) to filter out some text that maven prints to stdout. In my case I only had one line of text to filter that'll only appear at my company.
Credit where it is due: I adapted this info from this other StackOverflow thread.
This is the cleanest solution available:
mvn org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate \
-Dexpression=project.groupId -q -DforceStdout
mvn org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate \
-Dexpression=project.artifactId -q -DforceStdout
mvn org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate \
-Dexpression=project.version -q -DforceStdout
Advantages:
groupId
or version
is inherited from parent pom.xml
.
xmllint
-based solutions fail when POM properties are inherited!echo
or grep
)Alternatively, you can add this entry in your pom.xml
, under plugins
section:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>3.2.0</version>
</plugin>
and then run commands compactly as follows:
mvn help:evaluate -Dexpression=project.groupId -q -DforceStdout
mvn help:evaluate -Dexpression=project.artifactId -q -DforceStdout
mvn help:evaluate -Dexpression=project.version -q -DforceStdout
Please note:
maven-help-plugin
version 3.2.0
(and above) has forceStdout
option. You may replace 3.2.0
in above command with a newer version from the list of available versions of mvn-help-plugin from artifactory, if available.-q
suppresses verbose messages.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With