Goal
To create an archetype that can be used to create multi module maven projects.
Multi project, maven projects give us the convenience of running a maven command in the base project.
So if we type mvn clean install in the base project, the command will be executed for all the enclosed projects.
To accomplish this we first need to create a multi module project. Then create an archetype from that project.
This blog details the following steps:
- How I created the base project
- How I generated the archetype from the base project
- How I used the archetype to create my first mult project maven project
Base multi module project
The Archetype
Here is the directory structure of the final archetype. As you can see the actual archetype will be found under target/generated-resources folder.
The idea was to install this archetype in the company repository and all new projects will use this archetype to create its project structure.
Example of a project that was created using the above shown archetype.
As you can see the __rootArtifactId__ token is replaced with the actual project name.
Which in this case is acctauthreq
Sample Project using the archetype
Okay, time to go over the steps that I went through to get the archetype going.
Creating the Base Project
Base project is the enclosing project. Here is the command I used to generate it. I have decided to call the base project for the archetype adi-identity-archetype.
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=edu.vanderbilt.adi -DartifactId=adi-identity-archetype -Dversion=1.0
As you can see the maven-archetype-quickstart was used to create the base project.
Running the above command will create a folder
adi-identity-archetype and a pom.xml with packaging type jar.
Cd to adi-identity-archetype and change the pom.xml packaging type to pom.
Also add dependencies to java ee, commons logging etc.
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
All the other maven projects will created inside this enclosing Base Project.
Creating the internal projects
Step 1: CD to adi-identity-archetype
Step 2: Create util project
Run the following command.
As you can see the artifactId is __rootArtifactId__-util.
Step 3: Create EJB project
CD to __rootArtifactId__-ejb
Then enter the relative path to the parent pom. The pom.xml of the enclosing Base Project will be the parent pom.
Add the following plugins
Add the following dependencies
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
All the other maven projects will created inside this enclosing Base Project.
Creating the internal projects
Step 1: CD to adi-identity-archetype
Step 2: Create util project
Run the following command.
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=edu.vanderbilt.adi.util -DartifactId=__rootArtifactId__-util -Dversion=1.0
As you can see the artifactId is __rootArtifactId__-util.
Step 3: Create EJB project
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=edu.vanderbilt.adi.ejb -DartifactId=__rootArtifactId__-ejb -Dversion=1.0
CD to __rootArtifactId__-ejb
Then enter the relative path to the parent pom. The pom.xml of the enclosing Base Project will be the parent pom.
Set Packaging to
<packaging>ejb</packaging>
Add the following plugins
Add the following dependencies
Step 4: Create Web project.
We use a struts based web project and thus used the struts2-archetype-blank to create the web project.
If you do not care for struts you could use the maven-archetype-webapp to create the web project.
Here is the command I used to create a struts based web app.
mvn archetype:generate -DgroupId=edu.vanderbilt.adi.web -DartifactId=__rootArtifactId__-war -Dversion=1.0 -DarchetypeGroupId=org.apache.struts -DarchetypeArtifactId=struts2-archetype-blank -DarchetypeVersion=2.1.8.1 -DarchetypeCatalog=http://people.apache.org/builds/struts/2.1.8.1/m2-staging-repository/
Add the relative path to the parent pom and change the packaging to war.
Since we want to filter the web.xml we would need to configure the maven war plugin.
Since we do not use spring and jetty remove explicit dependencies to these jars.
Step 5: Create EAR project
Most of our applications are eventually packaged in an ear. Thus creating a packaging ear project.
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=edu.vanderbilt.adi.ear -DartifactId=__rootArtifactId__-ear -Dversion=1.0
Cd to __rootArtifactId__-ear
Then enter the relative path to the parent pom. The pom.xml of the enclosing Base Project will be the parent pom.
Cd to __rootArtifactId__-ear
Then enter the relative path to the parent pom. The pom.xml of the enclosing Base Project will be the parent pom.
Change the packaging from jar to ear.
saa
Add the following dependencies. The other internal projects are being added here as dependencies.
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${groupId}.util</groupId>
<artifactId>__rootArtifactId__-util</artifactId>
<version>1.0</version>
<type>jar</type>
</dependency>
<dependency>
<!-- 3.3. Dependency on EJB Module. -->
<groupId>${groupId}.ejb</groupId>
<artifactId>__rootArtifactId__-ejb</artifactId>
<type>ejb</type>
<version>1.0</version>
</dependency>
<dependency>
<!-- 3.4. Dependency on Web Application Module. -->
<groupId>${groupId}.web</groupId>
<artifactId>__rootArtifactId__-war</artifactId>
<type>war</type>
<version>1.0</version>
</dependency>
</dependencies>
Configure the maven ear plugin as follows:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<!-- 4.5. Specify modules to include. -->
<modules>
<!-- 4.5.1. Include Java Module. -->
<javaModule>
<groupId>${groupId}.util</groupId>
<artifactId>__rootArtifactId__-util</artifactId>
</javaModule>
<!-- 4.5.2. Include EJB Module. -->
<ejbModule>
<groupId>${groupId}.ejb</groupId>
<artifactId>__rootArtifactId__-ejb</artifactId>
</ejbModule>
<!-- 4.5.3. Include Web Application Module. -->
<webModule>
<groupId>${groupId}.web</groupId>
<artifactId>__rootArtifactId__-war</artifactId>
<contextRoot>${webContext}</contextRoot>
</webModule>
</modules>
</configuration>
</plugin>
</plugins>
*Note specifying the webcontext as shown here will allow the user to specify the webcontext on demand during build. See Ex. below.
If the <contextRoot>${webContext}</contextRoot> is added in the maven ear plugin on mvn install you can pass in the required web context as a parameter
sds
And the resuting application.xml will have this value as the web context.
Step 6: Create Model
We use EJB3 and JPA entities (implemented as Hibernate entities) and hence creating a model project.
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=edu.vanderbilt.adi.model -DartifactId=__rootArtifactId__-model -Dversion=1.0
CD to __rootArtifactId__-model
Then enter the relative path to the parent pom. The pom.xml of the enclosing Base Project will be the parent pom.
sad
Add the following dependencies to hibernate.
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.0.1.Final</version>
</dependency>
Under folder __rootArtifactId__-model\src\main create folder resources
Under the resources folder create persistence.xml
Under __rootArtifactId__-model\src\main\java\edu\vanderbilt\adi\model create SampleEntity.java
Generating the archetype from the project
We are finally ready to create an archetype from this project.
This can be done as follows:
Step 1 : cd to adi-identity-archetype
Step 2: Execute the following in the adi-identity-archetype dierctory.
mvn archetype:create-from-project
This will create the target/generated-resources folder.
Step 3:
adi-identity-archetype\target\generated-sources\archetype\src\main\resources\archetype-resources
Step 4:
Now visit each pom.xml and replace references to __rootArtifactId__ to ${rootArtifactId}
Change the artifactId of each module to follow the pattern
${rootArtifactId}-<moduleId>
Ex.
<groupId>${groupId}.ejb</groupId>
<artifactId>${rootArtifactId}-ejb</artifactId>
<version>${version}</version>
<name>${rootArtifactId}-ejb</name>
Step 5: cd to
adi-identity-archetype\target\generated-sources\archetype\src\test\resources\projects\basic
Step 6: Modify the archetype.properties file as shown below.
#Tue Dec 31 12:37:25 CST 2013
package=it.pkg
version=0.1-SNAPSHOT
groupId=archetype.it
artifactId=basic
webContext=basic
Step7
CD to folder
adi-identity-archetype\target\generated-sources\archetype
Execute mvn install
That's it your archetype is now installed in the your repo.
References:
Some blogs and tutorials I relied on to get my archetype going. Thanks to the authors of these resources.
http://scriptlandia.blogspot.com/2006/05/how-to-make-javaee-project-with-maven2.html
http://struts.apache.org/release/2.2.x/docs/struts-2-blank-archetype.html
http://stackoverflow.com/questions/20325814/multi-module-dependencies-in-maven-ear
http://www.mkyong.com/maven/how-to-create-a-jar-file-with-maven/
http://maven.apache.org/shared/maven-archiver/index.html
http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
http://maven.apache.org/plugins/maven-deploy-plugin/usage.html
http://struts.apache.org/release/2.2.x/docs/struts-2-blank-archetype.html
http://stackoverflow.com/questions/20325814/multi-module-dependencies-in-maven-ear
http://www.mkyong.com/maven/how-to-create-a-jar-file-with-maven/
http://maven.apache.org/shared/maven-archiver/index.html
http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
http://maven.apache.org/plugins/maven-deploy-plugin/usage.html