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

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+:)'`

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.

Maven – Guide to Naming Conventions, Guide to naming conventions on groupId, artifactId, and version However, it will be difficult to get a new single word group ID approved for inclusion in the� Maven groupId and artifactId naming – If you are creating new project with Maven then very first thing you will have to is decide groupId, artifactId and version. Before doing this you will have to understand three attribute: Example pom.xml. 1.

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

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 /`

I hope this helps.

Maven – Maven Getting Started Guide, apache.maven.plugins is the designated groupId for all Maven plugins. artifactId This element indicates the unique base name of the primary artifact being� Guide to naming conventions on groupId, artifactId, and version. groupId uniquely identifies your project across all projects. A group ID should follow Java's package name rules. This means it starts with a reversed domain name you control. For example, org.apache.maven, org.apache.commons. Maven does not enforce this rule.

Sometimes, we forget how easy it is to tailor Maven to our need : here is a very simple custom plugin that do one thing, concatenate groupId,artifactId and version and send it to out.

Check it 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.xml) to have the following line displayed on console :

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

To filter out everything but this line, all you have to do is running :

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

or even simpler :

mvn -q helper.me

Get the version of your Maven project on command line , I bet you have been faced with the situation to get the version of your Maven project GROUPID=$(mvn help:evaluate -Dexpression=project. @Michael groupId and artifactId are pure maven. sure, they are used a lot in Java projects, then again, so is english. I don't consider the difference between "their, they're and there" to be Java relatedd either. – Stultuske Oct 29 '18 at 12:26

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

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 the pom.xml work for simple use cases, however they fall short if you need to access something that is either not defined in the pom.xml (settings.localRepository) or a potentially derived value (project.version).

How to get Maven groupId, artifactId & project version to - html, 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 � I am working on maven based android project. I need to add all jars files of libs folder into maven local repositories because many jars are not available into maven central repositories. For this i am going to use following commands.But my question is how to get group-id,artifact-id from any external jar.

I prefer avoiding new dependencies where I can solve the solution easily. 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

How do I display the value of a pom.xml element?, A Maven POM file (Project Object Model) is an XML file that have one POM file for the parent project, and one POM file for each subproject. In the case of my Java Web Crawler project, the artifact ID would� In contrast, artifactId is an XML element in the POM.XML of a Maven project that specifies the id of the project (artifact). Thus, this is the main difference between groupId and artifactId in Maven. Usage. Furthermore, another difference between groupId and artifactId in Maven is that groupId helps to identify the project group while

Maven Tutorial, In order to learn how to install Maven on your system, please check this The first three of these (groupId:artifactId:version) combine to form the� From the gradle maven-publish plugin's documentation, it's clear that you set the groupId and version of the project directly in build.gradle: group = 'org.gradle.sample' version = '1.0'. However, the artifactId appears to be taken from the name of the folder you are working within.

Apache Maven Tutorial, sudo apt-get install mvn # if that does not work, try sudo apt-get These are defined via the groupId, artifactId, version and packaging property. 3. artifactId is the id of the artifact which can be jar dependency or a maven plugin or some other product released by the organisation denoted by groupid, version is the version of that artifact. You are right about the definition of a group id. You may leave out the group id while referencing official maven plugins which use the group id org.apache.maven.plugins.

Maven for building Java applications - Tutorial, groupdId and artifactId are the same values we gave in the command line. packaging is the package format of the artifact. Default value is jar. It� mvn archetype:generate -DgroupId=[your project's group id] -DartifactId=[your project's artifact id] -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-archetype Afterwhich, you can now customize the contents of the archetype-resources directory, and archetype-metadata.xml , then, proceed to Step#4 (Install the archetype and run the archetype plugin).

Comments
  • One drawback for this approach is that all the dependencies need to be resolved before extracting the properties. There exists cases where you do not want to resolve the dependencies and simply want the properties declared within the pom file. In that case, help:evaluate is still the better choice.
  • Currently I do almost the same thing as you but it's still three different calls...
  • Still, this is fast and simple. Don't over engineer it :-)
  • This may not work if the version is inherited from the parent.
  • Thanks for your plugin, it works well. but unfortunately the environment can't be upgraded with "custom" plugins (settings.xml) and the builded pom is in read only too. Currently I've to use common/default tools for this script... KR
  • As long as you have a way to push the plugin in your maven repo, using the "long" command should work, without the need to modified settings.xml or pom.xml. But I understand that sometimes, 'no' means 'no' ;)
  • Note that this will only work for POMs using literal values for these fields, as opposed to interpolation.
  • To get different properties and assign them to shell variables: IFS=: read id version <<< "$(echo '${project.artifactId}:${project.version}' | mvn --quiet --non-recursive help:evaluate -DforceStdout)"