How to create a project with Maven template

In this tutorial we will show you how to use mvn archetype:generate to generate a project from a list of existing Maven templates. In Maven 3.1.1, there are 1000+ templates, crazy, Maven team should filter out some useless templates.

Normally, we just use the following two templates :

  1. maven-archetype-webapp – Java Web Project (WAR)
  2. maven-archetype-quickstart – Java Project (JAR)

1. Maven 1000+ Templates

If you type mvn archetype:generate, 1000+ templates will be prompted on screen and you are no way to see it or choose what you want. To solve it, output the template list to a text file like this :

$ mvn archetype:generate > templates.txt
 
//waiting few seconds, then exits
Press CTRL + C
 
$ vim templates.txt

2. Maven archetype:generate

Steps to guide you how to generate a web project from an existing Spring-Hibernate template :

2.1 List out the Maven templates :

$ mvn archetype:generate
 
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---
[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:
 
311: remote -> jboss:mobicents-ra-archetype (An archetype for slee resource adaptor.)
//...
 
314: remote -> ml.rugal.archetype:springmvc-spring-hibernate (A pretty useful JavaEE application archetype based on springmvc spring and hibernate)
 
//...
486: remote -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (An archetype which contains a simplifed sample J2EE application.)
487: remote -> org.apache.maven.archetypes:maven-archetype-marmalade-mojo (-)
488: remote -> org.apache.maven.archetypes:maven-archetype-mojo (An archetype which contains a sample a sample Maven plugin.)
489: remote -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype which contains a sample Maven plugin.)
491: remote -> org.apache.maven.archetypes:maven-archetype-portlet (An archetype which contains a sample JSR-268 Portlet.)
492: remote -> org.apache.maven.archetypes:maven-archetype-profiles (-)
493: remote -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)
//...

2.2 Choose number “314” for ml.rugal.archetype:springmvc-spring-hibernate templete, and fill in the detail :

Note
This number 314 may vary at your environment. Find your correct number with the technique listed in step 1 above.
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 493: 314
Choose ml.rugal.archetype:springmvc-spring-hibernate version: 
1: 0.1
2: 0.2
3: 0.3
Choose a number: 3: 3
Downloading: http://repo.maven.apache.org/maven2/ml/rugal/archetype/springmvc-spring-hibernate/0.3/springmvc-spring-hibernate-0.3.jar
Downloaded: http://repo.maven.apache.org/maven2/ml/rugal/archetype/springmvc-spring-hibernate/0.3/springmvc-spring-hibernate-0.3.jar (29 KB at 19.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/ml/rugal/archetype/springmvc-spring-hibernate/0.3/springmvc-spring-hibernate-0.3.pom
Downloaded: http://repo.maven.apache.org/maven2/ml/rugal/archetype/springmvc-spring-hibernate/0.3/springmvc-spring-hibernate-0.3.pom (4 KB at 5.5 KB/sec)
Define value for property 'groupId': : com.mkyong.web
Define value for property 'artifactId': : myweb
Define value for property 'version':  1.0-SNAPSHOT: : 
Define value for property 'package':  com.mkyong.web: : 
Confirm properties configuration:
groupId: com.mkyong.web
artifactId: myweb
version: 1.0-SNAPSHOT
package: com.mkyong.web
 Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: springmvc-spring-hibernate:0.3
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.mkyong.web
[INFO] Parameter: artifactId, Value: myweb
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.mkyong.web
[INFO] Parameter: packageInPathFormat, Value: com/mkyong/web
[INFO] Parameter: package, Value: com.mkyong.web
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.mkyong.web
[INFO] Parameter: artifactId, Value: myweb
[INFO] project created from Archetype in dir: /Users/mkyong/Documents/workspace/maven/template/myweb
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 35.210s
[INFO] Finished at: Tue Oct 28 15:32:10 MYT 2014
[INFO] Final Memory: 10M/29M
[INFO] ------------------------------------------------------------------------

It will generate the following project folders and files.

maven-template

Figure : Generated Project structure in Eclipse IDE.

Note
To imports the project into Eclipse, type mvn eclipse:eclipse, and import it as a normal project :

$ cd myweb
$ pwd
/Users/mkyong/Documents/workspace/maven/template/myweb
 
$ mvn eclipse:eclipse

3. More Examples

If you know which archetypeArtifactId to use, just skip the command in interactive mode like this :

3.1 maven-archetype-quickstart (Java Project)

$ mvn archetype:generate -DgroupId=com.mkyong.core -DartifactId=ProjectName
	-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Review this example – How to create a Java project with Maven

3.2 maven-archetype-webapp (Java Web Project)

$ mvn archetype:generate -DgroupId=com.mkyong.web -DartifactId=ProjectName
	-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Review this example – How to create a Java web application project with Maven

References

  1. Newbie: maven archetype:generate
  2. Guide to Creating Archetypes
  3. Maven Archetype Plugin – Usage
  4. personal site for Rugal Bernstein
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments