Reficio™ - Reestablish your software!

Getting version of the Maven project in a freestyle Jenkins build

Today, I have had to extract the version of a Maven project that is built as a part of a freestyle Jenkins build. There are some stackoverflow posts describing how to do it, but the implicit prerequisite is that the build is a native Maven build – not a freestyle build. That is not what I wanted.

Below, I enclose a script that I have come up with for a Jenkins freestyle build that contains a Maven project step. You may have to adjust the path to the pom.xml file. It is important that the script is executed as a system Groovy script, otherwise the Jenkins’ jars are not on the classpath.

The script sets an environment variable (MAVEN_VERSION) that may be used later on in the build. Enjoy!

Comments

  1. Maurits says:

    You want ‘def param = …’ instead of ‘dev param = …;

  2. Rahul says:

    Thanks, corrected typo that Maurtis mentioned and works like a charm.

  3. Murali Nadadhur says:

    def project = new XmlSlurper().parse(new File(“$workspace/pom.xml”))
    For some reason I keep getting FileNotFoundException when the xml is actually there.

    • Eric says:

      Hi Murali,
      If you’re facing FileNotFOundException, it is probably that your build is runnig on a slave. Unfortunately, *system* Groovy scripts run on master exclusively.
      Hope this helps
      Eric

  4. Everson Jr says:

    Hi guys!
    I don´t know why I am getting this error:

    ERROR: Build step failed with exception
    groovy.lang.MissingMethodException: No signature of method: Script1.dev() is applicable for argument types: (hudson.model.StringParameterValue) values: [(StringParameterValue) MAVEN_VERSION='1.0.0_r3']
    Possible solutions: run(), run(), grep(), dump(), any(), every()
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:78)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:46)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
    at Script1.run(Script1.groovy:9)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:650)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:636)
    at hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:98)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
    at hudson.model.Build$BuildExecution.build(Build.java:205)
    at hudson.model.Build$BuildExecution.doRun(Build.java:162)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:537)
    at hudson.model.Run.execute(Run.java:1741)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:98)
    at hudson.model.Executor.run(Executor.java:381)

    Thanks!!

  5. Hellko friends, hߋw is аll,and what you ԝould like to say ɑbout tһis paragraph,
    іn my view itѕ truⅼy amazing fօr me.

  6. Νormally I ɗo noot read post ⲟn blogs, howeνer I wish to sаү that
    tҺis writе-up very pressured mе to check out ɑnd do it!
    Your writing style hɑs beewn surprised me. Thanks,
    vеry nice article.

  7. Neel says:

    What if I want to get the following property from “Execute Groovy Script”

    Thread.currentThread().executable().
    I am running my job in slave node so I want to execute it with “Execute Groovy Script” not with “Execute System Groovy Script”. How can I do this?

  8. poker99 says:

    Attractive section of content. I just stumbled upon your website and
    in accession capital to assert that I get actually enjoyed account your blog posts.

    Anyway I’ll be subscribing to your feeds and even I achievement you access
    consistently rapidly.

Speak Your Mind

*