I am trying to find out if I could get the value of a variable declared inside a Java program or maven-plugin and store it in a Jenkins environment variable.

This is because the Jenkins file has to create a new git branch from dev and call it Release-9.0.86 for example but the version number is inside a pom.xml.

I have already written a maven-plugin that retrieves the version from the pom.xml an writes it into another XML file, but I need to know if I could send the version to Jenkins to handle it.

1 Answers

Jonas Berlin On Best Solutions

You could perhaps create a properties file instead of a xml file with the needed information. Example contents of created branch-name.properties file:


Then you can add a build step "Inject environment variables" after the one that produces the properties file, and configure that to read the properties file you just created.

Properties File Path: branch-name.properties

After that you can use the environment variables as usual in the jenkins build, for example a "Execute shell" build step that creates the branch:

git branch ${NEW_BRANCH_NAME}

To then push the newly created branch you could add a "Git Publisher" post-build action.

Branch to push: ${NEW_BRANCH_NAME}

Please note that it might be useful to configure Git Publisher to "Push Only If Build Succeeds" i.e. branch will not be created if the build fails for any reason.