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

半城伤御伤魂 提交于 2020-05-09 18:32:05

问题


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

回答1:


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.




回答2:


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.




回答3:


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



回答4:


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).




回答5:


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



回答6:


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 here.
  • Zero dependency on external tools! (notice that I'm not using any echo or grep)

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.



回答7:


A simpler command, derived from @Patrick's answer:

echo '${project.groupId}:${project.artifactId}:${project.version}' | mvn -N -q -DforceStdout help:evaluate


来源:https://stackoverflow.com/questions/23521889/how-to-get-maven-groupid-artifactid-project-version-to-the-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!