Monday, March 05, 2007

From maven to mvn : Part 5 -- A Groovy Timestamp

Good afternoon and happy Monday! I hope you had a great weekend, I know I did. I have to apologize for today's entry up front. It isn't about writing a plugin as I had hoped it would be. It's also somewhat brief. Well, it's Monday and there have been more than the usual number of distractions...

I've been bothered about abusing the maven-buildnumber-plugin to set a timestamp property used in ${foo} replacement. I happened to be doing some reading over the weekend and came across the groovy-maven-plugin. Which lets you run a random bit of groovy script during your build process. Now I still intend to explore the exciting world of creating my own maven plugins but I think this is a good place to start. In fact, when I *do* write my own plugins in the future they will very likely be written in groovy using this technique.

So, today's brief entry will (a) drop the buildnumber plugin and (b) use the groovy-maven plugin to replace its functionality.

Begin by adding repository entries to your pom:
<repositories>
<repository>
<id>Codehaus Snapshots</id>
<url>http://snapshots.repository.codehaus.org/</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>Codehaus Snapshots</id>
<url>http://snapshots.repository.codehaus.org/</url>
</pluginRepository>
</pluginRepositories>

Note: If you already have repositories declared just add these new entries, don't blow away what you've got.

Next, declare the plugin and some script for it to execute:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
<body>
import java.util.Date
import java.text.MessageFormat
def timestamp = MessageFormat.format("{0,date,yyyy-MM-dd HH:mm:ss}", new Date())
System.setProperty("timestamp", timestamp)
</body>
</source>
</configuration>
</execution>
</executions>
</plugin>


That's it! Now, during the generate-resources phase of the maven build lifecycle groovy will execute the script to set the system property timestamp to the current time.

That's it for today. I have to run off to take care of some other things. I promise you a better Part 6.

4 comments:

Unknown said...

Thanks a bunch! You've saved me a lot of time.

martib said...

if you like to use the timestamp have been set in your manifest file:
try this...

<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
import java.util.Date
import java.text.MessageFormat
def timestamp = MessageFormat.format("{0,date,dd.MM.yyyy HH:mm:ss}", new Date())
project.properties['my_pom_property'] = timestamp
</source>
</configuration>
</execution>
</executions>
</plugin>

and the JAR plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<myManifestEntry>${my_pom_property}</myManifestEntry>
</manifestEntries>
</archive>
</configuration>
</plugin>

Anonymous said...

Thanks a lot! Much simpler than using a dedicated plugin for that!

Akaya said...

Great content! Super high-quality! If you want to learn more about PHP and Python, you can learn from here -
Generate QR Code in Python using PyQRCode
PHP create word document from html
Simple file upload in PHP
File upload validation in PHP
Python cv2.gaussianblur
how to add multiple markers on google maps javascript