Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Maven groupId, artifactId & project version to the command line

Tags:

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


For the moment, I retrieve data by doing the following:
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+:)'`
like image 639
gudepier Avatar asked May 07 '14 15:05

gudepier


People also ask

What is groupId artifactId in Maven?

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.

What is groupId in Maven project?

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.


2 Answers

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.

like image 146
user3483102 Avatar answered Sep 30 '22 19:09

user3483102


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:

  • This works fine on all operating systems (OS X, Ubuntu, Windows etc.) and on all shells (bash, zsh etc.)
  • [very important] This works fine even if groupId or version is inherited from parent pom.xml.
    • Note that xmllint-based solutions fail when POM properties are inherited!
  • Zero dependency on external tools! (notice that I'm not using any 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.
  • Option -q suppresses verbose messages.
like image 25
Manu Manjunath Avatar answered Sep 30 '22 18:09

Manu Manjunath