Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Jenkins build number using maven

Tags:

maven

jenkins

I am trying to read the Jenkins number using a maven pom.xml using the following code. The issue is that, the build is failing with the error as I have specified below. I referred to the official document given by Jenkins Building a software project. If I try to use any text instead of ${BUILD_NUMBER} it is getting printed in the console but not the variable.

POM Profile reference:

<profiles>
    <profile>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <build.number>${BUILD_NUMBER}</build.number>
        </properties>
    </profile>
</profiles>

POM System Property:

<systemPropertyVariables>
    <buildnumber>${build.number}</buildnumber>
</systemPropertyVariables>

Please let me know if I am doing something wrong.

like image 777
Yellesh Chaparthi Avatar asked Sep 03 '14 17:09

Yellesh Chaparthi


People also ask

How do I find my maven build number?

The build number is obtained from scm. You can then place that build number in metadata, which can be accessed from your app, if desired. The mojo also has a couple of extra functions to ensure you get the proper build number. First, your local repository is checked to make sure it is up to date.


1 Answers

One thing I did to myself was to call a few maven commands before, like mvn versions:set and mvn -N versions:update-child-modules. My pom files are versioned x.y.z, so I make them look like x.y.z.$buildnumber. You can replace it enterelly by $buildnumber if you want, but dont forget maven will update the and commit it back to SCM, incrementing the minor version by 1. You have to handle this on the next build.

Please note that ${BUILD_NUMBER} is an envvar available on bash (and I can get it on my bash scripts), not a maven variable.

And as explained here: How to refer environment variable in POM.xml?, you can do ${env.variable_name}.

like image 115
Lovato Avatar answered Nov 30 '22 19:11

Lovato