Sunday, May 18, 2008

Accessing Hudson Variables with a Free-Style Project

Here is a great article on creating a manifest file containing hudson variables so that you can know exactly what build number and svn number built your artifact. However, in my case I wanted to create a properties file under my maven2 project under src/main/resources and let maven filtering take care of the rest. But for some reason it wouldn't work. After some further research, I eventually found out that free-style projects appear to not work the same as a maven2 project in hudson when trying to access hudson variables.

In summary, hudson variables work as expected when you create a maven2 project in hudson. With a free-style project you have to perform an extra step. Why, I don't know; I am sure there is a good reason. It's just too late to figure it out.

Here are the extra steps you can follow to create a properties file containing hudson values.

Update POM
In your projects POM or parent POM add the following properties at the bottom

<project>
....
<properties>
<build.number>${BUILD_NUMBER}</build.number>
<build.id>${BUILD_ID}</build.id>
<job.name>${JOB_NAME}</job.name>
<build.tag>${BUILD_TAG}</build.tag>
<executor.number>${EXECUTOR_NUMBER}</executor.number>
<workspace>${WORKSPACE}</workspace>
<hudson.url>${HUDSON_URL}</hudson.url>
<svn.revision>${SVN_REVISION}</svn.revision>
</properties>
</project>
Create a properties file
Create a application.properties file under your maven2 projects src/main/resources directory.

Now add this to it

build.number=${build.number}
build.id=${build.id}
job.name=${job.name}
build.tag=${build.tag}
executor.number=${executor.number}
workspace=${workspace}
hudson.url=${hudson.url}
svn.revision=${svn.revision}

4 comments:

Chad Beaudin said...

I did this and my resultant application.properties that gets put in the jar does not have the variables replaced with the Hudson build values.
Any clues?

Chad Beaudin said...

I found my problem.
You must have a "resource" section defined in your pom.

You can see an example of it here:
http://maven.apache.org/guides/getting-started/index.html#How_do_I_filter_resource_files


Thanks for posting how to do this James.

Chad Beaudin said...

You also must be aware that filtering of the resource directory will corrupt binary files. The fix is described at the bottom of the following link. You must set up mutually exclusive resource sets.

http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

Term Papers said...

I have been visiting various blogs for my term papers writing research. I have found your blog to be quite useful. Keep updating your blog with valuable information... Regards