iCodeLikeImDrunk
iCodeLikeImDrunk

Reputation: 17806

Is it possible to access tags inside a pom.xml file using mvn command?

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <twilio-version>6.3.0</twilio-version>
</properties>

Suppose I have the above setup in my pom.xml, is it possible to grab say the java version from it?

My idea is to have Jenkins/JenkinsFile to run some say 'maven GRAB_PARAM PARAM_NAME' method and use whatever the value to setup its build environment.

So my JenkinsFile would be:

node {
    stage 'Clean Up'
    deleteDir()

    stage 'Env Setup'
    def mvnHome = tool '3.0.3'
    env.JAVA_HOME = THE_MVN_COMMAND_THAT_CAN_GRAB_VERSION_FROM_POM

    stage 'Compile/Package'
    checkout scm
    sh "${mvnHome}/bin/mvn clean install -DskipTests"
}

I also wanna use this so I can easily grab the build version from previous build etc...

Upvotes: 2

Views: 168

Answers (2)

Tunaki
Tunaki

Reputation: 137064

Yes, you can use the Maven Help Plugin and its evaluate Mojo:

Evaluates Maven expressions given by the user in an interactive mode.

mvn help:evaluate -Dexpression=java.version

With versions up to 2.2, this has the unfortunate situation of printing a lot of information with it, that can be filtered out by piping the result to | findstr /v "[INFO]" on Windows, or | grep -v "[INFO]" or Unix-line systems.

Starting with version 3.0.0, you can use

mvn help:evaluate -Dexpression=java.version -Doutput=result.txt

to redirect the output of the evaluation to a file. As of today, 3.0.0 is still in a SNAPSHOT state, so you can add a plugin repository to your settings or POM, allowing snapshots, and pointing to Apache snapshot repo at https://repository.apache.org/content/repositories/snapshots/.

Upvotes: 2

ben75
ben75

Reputation: 28706

The maven-help-plugin and its goal evaluate is probably what you need.

Something like

mvn org.apache.maven.plugins:maven-help-plugin:2.2:evaluate -Dexpression=project.version

will output the project version.

Upvotes: 2

Related Questions