Thursday, 15 July 2010

Building your own GWT Spring Maven Archetype


Hello everyone,

While watching  this really interesting articles by Justin about Spring and GWT,i thought that it would be extremely useful to show how to build your own custom maven archetype .The archetype we will present is based on the last project of Justin and includes various technologies like Spring,GWT,AspectJ,HornetQ and Infinispan.
Enough talking ,let's get our hands dirty now.

Preparation of the archetype.
First you must have a template project that you will use to build your archetype.In our presentation you will find Justin project here.
Change directory to the root directory of the project and run the following command.
mvn archetype:create-from-project
After the completion of the command you will find a new project that is generated at the following location ${project_home}/target/generated-sources/archetype.Its a complete maven archetype project that you can customize for your needs.
  • At folder src/main/resources/META-INF/maven you will find archetype-metadata.xml which is the descriptor of the archetype.
  • At folder src/main/resources/archetype-resources is the template project that is going to be generated.

Customization of archetype.
Maven archetypes use apache velocity to generate their code. You can access velocity variables in a file by setting the following constants at the top of a file:
#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
Then you have access to maven properties like artifactId by using this syntax ${artifactId}.Also you can use the following syntax __artifactId__ to access the value of a parameter.
The basic parameters that every archetype has are the following :
  • groupId
  • artifactId
  • version
  • package
Also you can set as many variables as you want at the archetype-metadata.xml using the following syntax :

      
       default_value
      

please notice that each time you add a variable you should edit src/test/resources/projects/basic/archetype.properties and add your parameter.
 
version=0.1-SNAPSHOT
groupId=archetype.it
artifactId=basic
custom_variable=default_value

In our example we performed the following changes:

At src/main/resources/META-INF/maven/archetype-metadata.xml


       derby
      
      
       javacodegeeks
       
      
       ***
      
      
       ***
      
      
       y
       
At src/test/resources/projects/basic/archetype.properties

db=derby
dburl=javacodegeeks
dbusername=test
dbpassword=test
cache=n
At src/main/resources/archetype-resources/pom.xml

#if( $db == "derby" )
    
     org.apache.derby
     derby
     10.6.1.0
    
    #elseif( $db == "mysql" )
    
        mysql
        mysql-connector-java
        5.1.13
    
    #else 
    ^M
        org.apache.derby^M
        derby^M
        10.6.1.0^M
    ^M
   #end 


And

        maven-resources-plugin
        2.4.3
        
          
            copy-resources
            
            validate
            
              copy-resources
            
            
              ${basedir}/src/main/webapp/${package}.Application
              
                
                  ${basedir}/src/main/resources/${artifactId}
                  true
                
      
            
          
        
      

At src/main/resources/archetype-resources/src/main/webapp/WEB-INF/applicationContext.xml
#if($db == "mysql") 
  
 
  
  
  
   
    ${dburl}
    ${dbusername}
    ${dbpassword}
   
  
  
  
 
 #else
 
 
  
  
  
   
          ${dburl}
          create
   
  
  
  
  
 #end
At src/main/resources/archetype-resources/src/main/resources/META-INF/persistence.xml


#if($cache == "y")
   
   
   
   

 #else
   ^M
                        ^M

 #end
Rename com/javacodegeeks/gwtspring/public folder to __artifactId__ .

Installing and Running the archetype

To install the archetype go to the root folder of the archetype and type.

mvn install
This should create ~/.m2/archetype-catalog.xml file that you can import to eclipse if you are an eclipse an m2eclipse user.
To run the archetype run the following.

mvn archetype:generate -DarchetypeCatalog=local -DarchetypeGroupId=com.javacodegeeks -DarchetypeArtifactId=gwtspring-archetype
Or create a new maven project in eclispe and select the archetype from the local catalog you imported from ~/.m2/archetype-catalog.xml .
*** Notice that the archetype might be snapshot and you should check "Include snapshots archetypes".


The source code of the archetype is available here.
Hope you enjoy this article,
Best regards,

Pat



6 comments:

  1. Great blog!
    Ps. fix typo in title.

    ReplyDelete
  2. I guess there is mistake in topic headline ... manen = maven :)

    ReplyDelete
  3. You are both right.It 's not manen but maven. It's my mistake.

    ReplyDelete
  4. Hi! well done! you've saved me lot of hours. Thank you...documentation is awesome :-(

    ReplyDelete
  5. I am running into one issue. I have done everything. I did the mvn install. I see the archetype-catalog.xml file and all looks good. I get an error when trying to create a new project saying the archetype does not exist. Below is my console output. Anyone have any ideas what I am missing?

    C:\YB\dev\archetype2>mvn archetype:generate -DarchetypeCatalog=local
    [INFO] Scanning for projects...
    [INFO] Searching repository for plugin with prefix: 'archetype'.
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Maven Default Project
    [INFO] task-segment: [archetype:generate] (aggregator-style)
    [INFO] ------------------------------------------------------------------------
    [INFO] Preparing archetype:generate
    [INFO] No goals needed for project - skipping
    [INFO] [archetype:generate]
    [INFO] Generating project in Interactive mode
    [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.
    archetypes:maven-archetype-quickstart:1.0)
    Choose archetype:
    1: local -> gwtspring-archetype (gwtspring-archetype)
    Choose a number: : 1
    [INFO] ------------------------------------------------------------------------
    [ERROR] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] The desired archetype does not exist (com.javacodegeeks:gwtspring-archety
    pe:0.0.1-SNAPSHOT)
    [INFO] ------------------------------------------------------------------------
    [INFO] For more information, run Maven with the -e switch
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 5 seconds
    [INFO] Finished at: Wed Sep 15 11:38:36 EDT 2010
    [INFO] Final Memory: 10M/19M
    [INFO] ------------------------------------------------------------------------

    ReplyDelete
  6. Hello David,

    I'm not sure what the problem is.

    I suggest to try and reinstall the archetype by using the following command:

    mvn clean install

    Regards
    ./pat

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...