I am using Jenkins as my CI server and Artifactory as my artifact repository. The Artifactory plugin works in free-style projects and can be used just by using Invoke Maven3 build step to both fetch the dependencies and deploy the artifacts. Furthermore, I have another build step for building a docker image and pushing it to my private docker registry using CloudBees Docker Build and Publish plugin.
The problem is that I want to use POM_* variables in CloudBees configurarion that should be exposed by Maven Plugin as mentioned here. But everytime I build the project, I got the following error:
ERROR: Unrecognized macro 'POM_VERSION' in '${POM_VERSION}'
org.jenkinsci.plugins.tokenmacro.MacroEvaluationException: Unrecognized macro 'POM_VERSION' in '${POM_VERSION}'
at org.jenkinsci.plugins.tokenmacro.TokenMacro.expand(TokenMacro.java:198)
at org.jenkinsci.plugins.tokenmacro.TokenMacro.expandAll(TokenMacro.java:233)
at org.jenkinsci.plugins.tokenmacro.TokenMacro.expandAll(TokenMacro.java:222)
at com.cloudbees.dockerpublish.DockerBuilder$Perform.expandAll(DockerBuilder.java:266)
at com.cloudbees.dockerpublish.DockerBuilder$Perform.getNameAndTag(DockerBuilder.java:277)
at com.cloudbees.dockerpublish.DockerBuilder$Perform.exec(DockerBuilder.java:247)
at com.cloudbees.dockerpublish.DockerBuilder$Perform.access$100(DockerBuilder.java:233)
at com.cloudbees.dockerpublish.DockerBuilder.perform(DockerBuilder.java:208)
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:1744)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:374)
Build step 'Docker Build and Publish' marked build as failure
I do not know what exactly is the reason of this problem. Maybe the Invoke Maven3 build step is causing this problem by not exposing env variables. Another source of this could be CloudBees Plugin that does not utilize env variables. Any ideas?
Based on @Jesse Glick answer, searching around a little and struggling a bit with jenkins, I ended up with the following solution that works like a charm.
1-Add an "Execute Shell" build step containing following commands:
echo POM_VERSION=$(mvn help:evaluate -Dexpression=project.version | fgrep -v '[INFO]') > pom_file
echo POM_ARTIFACTID=$(mvn help:evaluate -Dexpression=project.groupId | fgrep -v '[INFO]') >> pom_file
echo POM_GROUPID=$(mvn help:evaluate -Dexpression=project.artifactId | fgrep -v '[INFO]') >> pom_file
2-Add an "Inject environment variables" build step (from EnvInject plugin) and enter "pom_file" as "Properties File Path"
3-Add a "Docker Build and Publish" build step and use POM_VERSION, POM_ARTIFACTID, POM_GROUPID in its configuration as you wish.