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.

Monday, 7 January 2013

Glassfish Commands

Here are some useful glassfish commands

File structure

Glassfish has a weird file structure.  One installation everything is in

    c:/glassfish3/ 

but the actual default domain location is in

    c:/glassfish3/glassfish/domains

Other domains can be created in different locations to this and glassfish will only start the domain specified rather than starting all domains in a domain location.  This is glassfish's way of keeping separation between installations.  You can, of course, install multiple applications into a single domain.

In the commands below either the asadmin needs to be on the path or prefix the asadmin with <glassfish_home>/bin/.

Starting a Domain

To start a domain in the default location just do

    asadmin start-domain <domain_name>

if there is only one domain then the domain_name can be dropped.

Often it is useful to have domains installed in different locations.  To do this use

    asadmin start-domain --domaindir <domain_location> <domain_name>

Again if there is only one domain in the domain_location then the name can be dropped.

Stopping a Domain
The simple command to stop a running domain is

    asadmin stop-domain <domain_name>

If you are running a cluster you may need to do this bit first before stopping the individual domains.

    asadmin stop-cluster <cluster_name>

Creating a Domain

To create a new domain use the command

   asadmin --user admin --port 4848 --host localhost --interactive=true create-domain --domaindir <domain_location> --adminport 4848 --instanceport 8080  <domain_name>

Where domain_location and domain_name need to be completed.

Deleting a Domain

To delete a domain use

    asadmin delete-domain --domaindir <domain_location> <domain_name>