Getting Project Version from Maven POM in Jenkins

前端 未结 11 2259
情歌与酒
情歌与酒 2020-12-01 03:19

Is there any way a Jenkins build can be aware of the Maven version number of a project after processing the POM?

I\'ve got some projects where versioning is controll

相关标签:
11条回答
  • 2020-12-01 03:57

    Based on @Akom`s answer the pre steps to have POM_VERSION are:

    1. "Inject environment variables" with property file your_property_file. Note if you select "Inject environment variables to the build process" the file needs to exist in the jenkins workspace.
    2. run in a pre step execute shell the follwing bash script.

    Script

    mvn org.apache.maven.plugins:maven-help-plugin:evaluate -Dexpression=project.version -l project_version
    # grep for the version pattern rather than not mentioning '\['
    echo "POM_VERSION=$(grep -E  '^[0-9.]+(-SNAPSHOT)?$' project_version)" > your_property_file
    
    0 讨论(0)
  • 2020-12-01 03:58

    After a lot of digging around (I never realised how poorly-documented Jenkins is!) I found a quite trivial solution.

    1. Install the Groovy plugin
    2. Add a Post Step to your Maven build of type Execute **system** Groovy script
    3. Paste in the following snippet of Groovy:

    Script:

    import hudson.model.*;
    import hudson.util.*;
    
    def thr = Thread.currentThread();
    def currentBuild = thr?.executable;
    def mavenVer = currentBuild.getParent().getModules().toArray()[0].getVersion();
    def newParamAction = new hudson.model.ParametersAction(new hudson.model.StringParameterValue("MAVEN_VERSION", mavenVer));
    currentBuild.addAction(newParamAction);
    

    The build environment variable called MAVEN_VERSION will now be available for substitution into other post-build steps in the usual manner (${MAVEN_VERSION}). I'm using it for Git tagging amongst other things.

    0 讨论(0)
  • 2020-12-01 04:00

    Using 'Execute System Groovy Script' as follows:

    import jenkins.util.*;
    import jenkins.model.*;
    
    def thr = Thread.currentThread();
    def currentBuild = thr?.executable;
    def projectManager = build.getProject()
    def file = projectManager.getWorkspace().child("pom.xml");
    def project = new XmlSlurper().parseText(file.readToString())
    
    def param = new hudson.model.StringParameterValue("currentVersion", project.version.toString())
    currentBuild.addAction(new hudson.model.ParametersAction(param));
    

    By using Execute System Groovy script you have direct access to the build, from which you can get the project and thus the "child" file in this case pom.xml.

    You won't have to create a new file and as you can see it offers very powerful access to every file within the workspace.

    0 讨论(0)
  • 2020-12-01 04:08

    You could also do :

    MAVEN_VERSION=`grep A -2 -B 2 "<your_project_name>" pom.xml | grep version | cut -d\> -f 2 | cut -d\< -f 1`-commit-"`echo $GIT_COMMIT`"
    

    Explanation: assuming that you have your project name within a line or two above/below version like a normal pom:

    <groupId>org.apache.bigtop</groupId>
    <artifactId>bigpetstore</artifactId>
    <version>1.0-SNAPSHOT</version>
    

    Then you can easily grep for the artifactId, use the "before/after" grep actions to suck in the version with it, and then grep the version out and use the simple unix "cut" command to splice out the content between "version" tags.

    I like the Jenkins-groovy integration, but this is alot easier and will work even on a build server which you dont have control over (i.e. because bash is universal).

    0 讨论(0)
  • 2020-12-01 04:10

    Solution:

    POM_VERSION=$( \
        xmlstarlet sel \
        -N x='http://maven.apache.org/POM/4.0.0' \
        -t \
        -v '//x:project/x:version/text()' \
        pom.xml \
    )
    

    Explanation:

    You can do this in a one-liner using a command-line XPath tool, such as those mentioned at "How to execute XPath one-liners from shell?". I chose XMLStarlet, but they all have similar syntax.

    When parsing a POM, you have to account for namespaces. The docs here helped me figure this out.

    In order to get the text for an element in XPath, you use the text() function as explained at XPath: select text node.

    My POM looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.foo.bar</groupId>
        <artifactId>foobar</artifactId>
        <version>1.0.6-SNAPSHOT</version>
        <packaging>jar</packaging>
    

    The downside here is that if the namespace changes, you have to change the command.

    0 讨论(0)
提交回复
热议问题