Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get pom.xml property from CommandLine

Tags:

I am currently writing a script for our Gitlab CI that automatically uploads files to an NFSShare folder in the network. Since I want to organize the builds and we're using maven, I thought I could "easily" get the project name from the pom.xml.

Is there a way to get the properties available from within a pom.xml through a command-line tool or something? My only other way I could think of was "regex-grepping the value by hand" - not a very clean solution in my opinion.

I already found the the properties plugin, but it only seem to ADD new properties through actual .properties files...

Any help would be much appreciated!

like image 435
spaceemotion Avatar asked May 22 '14 09:05

spaceemotion


People also ask

How do I access property in POM xml?

In maven pom. xml , a property is accessed by using ${property_name} . You can define your custom properties in Maven. Also, the maven supports a large scale of built-in properties.

How do you find the value of POM files?

The plugin is part of the Maven Super Pom and executed during the process-resources phase of the Jar Default Lifecyle. The only thing you have to do is to active filtering. How you make this property then available to your Java application is up to you - reading it from the classpath would work.

How do I pass a command line argument in Maven?

Passing an Argument to MavenMaven will use the value (2.5) passed as an argument to replace the COMMON_VERSION_CMD property set in our pom. xml. This is not limited to the package command — we can pass arguments together with any Maven command, such as install, test, or build.


1 Answers

I know the question is old but I spent some time looking for this.

To filter output you may use flags "-q -DforceStdout" where "-q" prevents output and "-DforceStdout" forces outputting result of plugin. E.g.:

BUILD_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) echo $BUILD_VERSION 

will result in printing version of project from POM.

Second important problem I had was accessing "properties" which is explained in Nick Holt comment. To access properties you just access them directly

<project ...>     <version>123</version>     (...)     <properties>         (...)         <docker.registry>docker.registry.com</docker.registry>         (...)     </properties>     (...) </project> 

WRONG

mvn help:evaluate -Dexpression=project.properties.docker.registry -q -DforceStdout 

OK

mvn help:evaluate -Dexpression=docker.registry -q -DforceStdout 
like image 52
Łukasz Kotyński Avatar answered Oct 05 '22 01:10

Łukasz Kotyński