Wednesday, 30 January 2013

Maven Replacer Plugin

The replacer plugin allows files to be edited during a maven build.  This can be useful on generated classes so that the time stamp is removed or comments added.  Removing the timestamp means that the classes have no difference if the schema hasn't changed.  Without removing the timestamp classes will constantly look different and will require a check in to the cvs / subversion / tfs repository.

Example



    <plugin>
        <groupId>com.google.code.maven-replacer-plugin</groupId>
        <artifactId>replacer</artifactId>
        <version>1.5.2</version>
        <executions>
            <execution> 
                <phase>prepare-package</phase>
                <goals>
                    <goal>replace</goal>
                </goals>
            </execution>
        </executions>
        <configuration>                         
            <includes>                              
                <include>src/main/java/com/me/generated/**/*.java</include>            
            </includes>
            <replacements>
                <replacement>
                    <token>//${line.separator}// This file was generated by the JavaTM</token>
                    <value>//
// CSOFF: a.*
// This file was generated by the JavaTM</value>  
                </replacement>
                <replacement>
                    <token>^ \*/
@Xml</token>
                    <value> */
@SuppressWarnings("all")
@Xml</value>  
                </replacement>
                <replacement>
                    <token>^// Generated on.*$</token>
                    <value>// Generated on: [TEXT REMOVED by maven-replacer-plugin to avoid generating changes]</value>  
                </replacement>
            </replacements>                       
            <regexFlags>
                <regexFlag>MULTILINE</regexFlag>
            </regexFlags>
        </configuration>
    </plugin>

There are three replacement sections configured here.

1) Adding a comment at the top of the file.  In this case it is a CSOFF comment so that checkstyle doesn't check these generated classes but it could be a copyright comment or anything. Note the use of the ${line.separator} which means that this is still cross-platform compliant as the line separators are different for Windows compared with linux / unix.
2) The section replacement adds a PMD suppression just above the @Xml annotation.  This is similar to the checkstyle comment above but this works for PMD.  Again it could be used for other annotations or comments.
3) The third replacement is for getting rid of the generated date.  If this is run everytime the maven project is packaged then the classes will constantly look like they need a check in but only because the generated date has changed and not because the actual content of the class has changed.  By removing the date this problem is also removed.

This plugin could also be used for resources / properties etc.  It is a hugely useful plugin.  My only beef with it is that it seems hard to bind the plugin to any other goal other than package without Eclipse IDE getting confused and constantly rebuilding.

No comments:

Post a comment