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

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


× five = 25



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books