How to get Maven groupId, artifactId and project version on command line

I would like to get the group ID, artifact ID, and Maven project version from the command line.

The proposed solution in this topic " How to get the version of the Maven project on the bash command line " is to use the following plugin:

mvn org.apache.maven.plugins:maven-help-plugin:2.2:evaluate -Dexpression=project.artifactId

This works well, but I cannot figure out how to set project.groupId, project.artifactId & project.version to the -Dexpression argument at the same time.

I would not start the Maven command 3 times with a different argument -Dexpression every time ...

Thks


At the moment I am receiving data by following these steps:
 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+:)'' 
+10
source share
5 answers

Here's another approach that does not require the creation of a Maven plugin, although Olivier has shown that it is quite easy to make.

mvn -q -Dexec.executable = echo -Dexec.args = '$ {project.groupId} $ {project.artifactId} $ {project.version}' --non-recursive exec: exec 2> / dev / zero

Please note that this is tailored to the Linux environment. On Windows, you could probably create a batch file that prints its input or something.

The disadvantage of this approach is that you may have to add | grep -v "something" | grep -v "something" | grep -v "something" | grep -v "something" to the very end of the above command (after 2>/dev/null ) filter out some text that maven prints to standard output. In my case, I had only one line of text to filter, which will only be displayed in my company.

Credit where this is due: I adapted this information from fooobar.com/questions/53969 / ....

+7
source

Sometimes we forget how easy it is to adapt Maven to our needs: here is a very simple custom plugin that does one thing, combines groupId, artifactId and version and send it to the exit.

Check this out, run mvn clean install (local) or mvn clean deploy (repository) to make it available in your environment.

Then run mvn my.helper.maven.plugin:helper-maven-plugin:me or mvn helper:me (after adding <pluginGroup>my.helper.maven.plugin</pluginGroup> to your <pluginGroups> in your Maven settings file .xml) so that the following line is displayed on the console:

my.help.me={project.groupId}:{project.artifactId}:{project.version}

To filter everything except this line, all you have to do is run:

 mvn helper:me| grep ^my.help.me= 

or even simpler:

 mvn -q helper.me 
+4
source

You can specify several expressions for maven-help-plugin (so you only need to run it once) and then extract them from the captured output by doing grep'ing for the "key" you specify:

 output=$(printf \ 'LOCAL_REPOSITORY=${settings.localRepository}\n'\ 'GROUP_ID=${project.groupId}\n' 'ARTIFACT_ID=${project.artifactId}\n'\ 'POM_VERSION=${project.version}\n0\n' \ | mvn help:evaluate --non-recursive ) localRepository=$(echo "$output" | grep '^LOCAL_REPOSITORY' | cut -d = -f 2) groupId=$(echo "$output" | grep '^GROUP_ID' | cut -d = -f 2) artifactId=$(echo "$output" | grep '^ARTIFACT_ID' | cut -d = -f 2) pomVersion=$(echo "$output" | grep '^POM_VERSION' | cut -d = -f 2) 

Other solutions that parse pom.xml work for simple use cases, but they do not work if you need to access something that is not defined in pom.xml ( settings.localRepository ), or a potential value ( project.version ).

+4
source

In bash, consider the following lines that I use to get them.
It uses xmllint and some string manipulations.

 GROUP_ID=`echo -e 'setns x=http://maven.apache.org/POM/4.0.0\ncat /x:project/x:groupId/text()' | xmllint --shell pom.xml | grep -v /` ARTIFACT_ID=`echo -e 'setns x=http://maven.apache.org/POM/4.0.0\ncat /x:project/x:artifactId/text()' | xmllint --shell pom.xml | grep -v /` VERSION=`echo -e 'setns x=http://maven.apache.org/POM/4.0.0\ncat /x:project/x:version/text()' | xmllint --shell pom.xml | grep -v /` 

Hope this helps.

+3
source

I prefer to avoid new dependencies where I can easily solve the solution. Using powershell:

 [xml]$pomXml = Get-Content .\pom.xml # version Write-Host $pomXml.project.version # groupId Write-Host $pomXml.project.groupId # artifactId Write-Host $pomXml.project.artifactId 
+2
source

All Articles