Code Scraps: Passing a parameter from Maven to TeamCity

I’ve spent a lot of my working career setting up build automation for teams I have worked with. For Java projects, this usually has involved creating Maven scripting for a project.

Using something like Maven is brilliant – as all major Java IDE’s support it and it allows your developers to work in whatever IDE they choose and feel comfortable with. One team I worked with had a mix of developers who favoured Eclipse, IntelliJ and NetBeans. All three IDEs happily read the Maven POM and tolerated project changes from other IDE’s with little to no issues.

If you use TeamCity to build your Maven projects, you may at times need to share parameters for builds with TeamCity. You can do this relatively easy in the Maven POM by adding the antrunner plugin to your build:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<tstamp>
<format property=”last.updated” pattern=”yyyy_MM_dd”/>
</tstamp>
<!– This echo command is purely for TeamCity to set the Updated Date for Build stamping. –>
<echo>##teamcity[setParameter name=’system.current_date’ value=’${last.updated}’]</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>

In the above code sample, the TeamCity setParameter directive will be echoed in the build log during code validation (depending on your need, you could do this during other phases too).

TeamCity will spot this in the log and in this case set a system parameter called “current_date” to the current date of the build. This “current_date” system value was then used in the naming convention for the output file.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.