luni, 4 martie 2013

Discover the beauty of your own maven plugin

            Going these days through the message bundles files, that have been modified chaotically, I realized that we do not have a maven plugin that can synchronize those bundles.

 1. 'First I was afraid, I was petrified' - did a quick Google search on 'build maven plugin' and the first result pointed me at Build Maven Plugin. A quick read through got me going on this.

 2. I used the Mojo archetype with the following command line:

 mvn archetype:generate -DgroupId=syncronize.files -DartifactId=sync-files-keys-plugin -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-plugin

 3. I imported the generated archetype in eclipse and I started to work on it.


 
What I was looking for was the ability to automatically synchronize all my message bundle files. Having, let's say the English version as the most complete one (containing all the necessary pairs of key/value). I decided to use it as base file for synchronizing the other message bundle files.

4. I started to add the plugin parameters. All you have to do is to use the @Parameter. In this particular case we need:
-  targetPattern - This parameter defines the target files pattern that will be processed
-  targetDirPath -This parameter defines the target directory that contains the message bundle files
 /* use absolute path for it e.g. d:/work/project/war/src/main/resources/properties/.
-  baseFileName - This parameter defines the base file from where all the missing keys with their values will be pulled


@Parameter(property = "pattern", required = true)
private String targetPattern;

5. Implement the execute() method - using org.apache.commons.configuration.PropertiesConfiguration.PropertiesConfiguration I did iterate over all the other bundles and I added all the missing keys. Keep in mind that the comments from bundle will not be passed along.

The implementation itself is not the point of this post.

6. Build the maven plugin - use the classic mvn clean install

7. Install the plugin in your local repository
mvn install:install-file -Dfile=sync-files-keys-plugin-1.0.jar -DgroupId=syncronize.files -DartifactId=sync-files-keys-plugin -Dversion=1.0 -Dpackaging=maven-plugin

8. Use the plugin


As result I did get all the other message bundles files populated with the missing keys, with the values in English - as a nice feature we could call the Google translate API and get the translation of the value in the required language.

Download the maven plugin.
Download the source code.

Enjoy building your own maven plugin.

Un comentariu: