24

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!

Wlad
  • 2,866
  • 3
  • 28
  • 42
spaceemotion
  • 1,404
  • 4
  • 24
  • 32
  • essentially you're looking for a way to read an xml file from your script - something like http://stackoverflow.com/q/4680143/289396 – JoseK May 22 '14 at 11:53
  • Well, I would like to not have to install another program - and I already got it working through "native" maven commands - thanks anyway! – spaceemotion May 22 '14 at 12:06
  • the second answer is better. it is not windows specific – Wlad Aug 12 '20 at 08:07

3 Answers3

56

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
Łukasz Kotyński
  • 1,007
  • 1
  • 9
  • 10
  • 3
    Answering old questions is encouraged here, especially if you've just solved the problem for yourself! – user7610 Nov 27 '19 at 14:37
12

If you know the name of the property you want, you can get the value with:

mvn help:evaluate -Dexpression=[property-name] | findstr /R ^^[^^\[INFO\]]

For example:

mvn help:evaluate -Dexpression=basedir | findstr /R ^^[^^\[INFO\]]

Will output:

C:\Users\nick\Local\Projects\example

This obviously assumes your building on a Windows box with the findstr removing all the other logging that Maven does when it runs. You'll be able to do something similar on Unix with a grep, but I leave that to you.

Nick Holt
  • 33,455
  • 4
  • 52
  • 58
  • 4
    Yup thanks a bunch! Since I'm on Linux ``mvn help:evaluate -Dexpression=project.name | grep "^[^\[]"`` is the right command ;) – spaceemotion May 22 '14 at 11:46
0

You may try the following option by github.com/jkot https://gist.github.com/jkot/8668441#echo-all-available-maven-properties

<!-- pom.xml -->
...
<build>
  <plugins>
    ...
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
            <execution>
                <phase>validate</phase>
                <goals>
                    <goal>run</goal>
                </goals>
                <configuration>
                    <tasks>
                        <echoproperties />
                    </tasks>
                </configuration>
            </execution>
        </executions>
    </plugin>
  </plugins>
</build>
...

run mvn validate and you'll get all properties

lazylead
  • 1,453
  • 1
  • 14
  • 26