Enterprise Java

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

<requiredProperties>
      <requiredProperty key="db">
       <defaultValue>derby</defaultValue>
      </requiredProperty>
      <requiredProperty key="dburl">
       <defaultValue>javacodegeeks</defaultValue>
      </requiredProperty>	
      <requiredProperty key="dbusername">
       <defaultValue>***</defaultValue>
      </requiredProperty>
      <requiredProperty key="dbpassword">
       <defaultValue>***</defaultValue>
      </requiredProperty>
      <requiredProperty key="cache">
       <defaultValue>y</defaultValue>
      </requiredProperty>
</requiredProperties>

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" )
    <dependency>
    	<groupId>org.apache.derby</groupId>
    	<artifactId>derby</artifactId>
    	<version>10.6.1.0</version>
    </dependency>
    #elseif( $db == "mysql" )
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.13</version>
    </dependency>
    #else	
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.6.1.0</version>
    </dependency>

And

<plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
          <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/src/main/webapp/${package}.Application</outputDirectory>
              <resources>
                <resource>
                  <directory>${basedir}/src/main/resources/${artifactId}</directory>
                  <filtering>true</filtering>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>

At src/main/resources/archetype-resources/src/main/webapp/WEB-INF/applicationContext.xml

#if($db == "mysql")	
	<bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
		init-method="init" destroy-method="close">
		<property name="uniqueResourceName" value="javacodegeeks" />
		<property name="xaDataSourceClassName"
			value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
		<property name="xaProperties">
			<props>
				<prop key="URL">${dburl}</prop>
				<prop key="user">${dbusername}</prop>
				<prop key="password">${dbpassword}</prop>
			</props>
		</property>
		<property name="maxPoolSize" value="50" />
		<property name="minPoolSize" value="20" />
	</bean>
	#else
	<bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
		init-method="init" destroy-method="close">
		<property name="uniqueResourceName" value="javacodegeeks" />
		<property name="xaDataSourceClassName" value="org.apache.derby.jdbc.EmbeddedXADataSource" />
		<property name="xaProperties">
			<props>
        		<prop key="databaseName">${dburl}</prop>
        		<prop key="createDatabase">create</prop>
			</props>
		</property>
		<property name="maxPoolSize" value="50" />
		<property name="minPoolSize" value="20" />
	</bean> 
	#end

At src/main/resources/archetype-resources/src/main/resources/META-INF/persistence.xml

#if($cache == "y")
			<property name="hibernate.cache.use_second_level_cache" value="true"/>
			<property name="hibernate.cache.use_query_cache" value="true"/>
			<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.infinispan.InfinispanRegionFactory"/>
			<!-- 
			<property name="hibernate.cache.infinispan.entity.eviction.strategy" value= "LRU"/>
			<property name="hibernate.cache.infinispan.entity.eviction.wake_up_interval" value= "2000"/>
			<property name="hibernate.cache.infinispan.entity.eviction.max_entries" value= "5000"/>
			<property name="hibernate.cache.infinispan.entity.expiration.lifespan" value= "60000"/>
			<property name="hibernate.cache.infinispan.entity.expiration.max_idle" value= "30000"/>
			-->

	#else
			<property name="hibernate.cache.use_second_level_cache" value="false"/>^M
                        <property name="hibernate.cache.use_query_cache" value="false"/>^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

Panagiotis Paterakis

Panagiotis is an open source enthusiast. In the morning he works as a freelancer implementing a wide variety of applications/services for the telecom industry (teleconference, NGIN, SOA). As a night job he craves to contribute to several open source projects. His latest activity is a forthcoming open source software that will bring the non trivial applications closer to the cloud
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button