Summary

Weaving a Nest (dkeats, Flickr)Basically Maven tool used to apply patterns to a project’s build infrastructure in order to promote comprehension and productivity by providing a clear path in the use of best practices.  Maven is essentially a project management and comprehension tool and as such provides a way to managing

    • Builds
    • Documentation
    • Reporting
    • Dependencies
    • SCMs
    • Releases
    • Distribution
In this article I will be describing a way to create a maven based Java/J2EE based project using Archetype.

What is Maven Archetype?

In a simple note, Archetype is a Maven project templating toolkit.  An archetype is defined as an original pattern or model from which all other things of the same kind are made.  Using archetypes provides a great way to enable developers quickly in a way consistent with best practices employed by your project or organization.

Exclusive list of Maven Archetypes(around 607 types) I have gathered click here!

How to create Maven Archetype for your need, please refer http://maven.apache.org/guides/mini/guide-creating-archetypes.html


Benefits of Maven Archetype

Maven archetype is best channel to bring patterns and industry best practices to your Java/J2EE development/ existing project.

  • Get your developers up and running as quickly as possible by providing a sample project that demonstrates many of the features of Maven
  • In a matter of seconds a new developer can have a working Maven project to use as a jumping board to make use more of the features in Maven
  • Archetype is a mechanism additive and by that allowing portions of a project to be captured in an archetype so that pieces or aspects of a project can be added to existing projects

“There is no faster way to get started with Java/J2EE project than using these archetypes!”

For Example: To standardize Java/J2EE development within your organization so you may want to provide archetypes for EJBs, or WARs, or web services, etc.  Once these archetypes are created and deployed in your organization’s repository they are available for use by all developers within your organization.


How to Generate Project using Maven Archetype?

Now let’s see ‘How to generate Maven based Java/J2EE Project using Archetype?’.  I will be describing a steps in simple way, however if you have any clarification or query please leave comment below.

Step 1:

Start with below maven command

mvn archetype:generate -DgroupId=com.myjeeva.maven -DartifactId=sampleapplication -Dversion=1.0-SNAPSHOT

Step 2:

Now maven will be downloading necessary POM files and Jar into your Local Maven Repo

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] org.apache.maven.plugins: checking for updates from central
[INFO] org.codehaus.mojo: checking for updates from central
[INFO] artifact org.apache.maven.plugins:maven-archetype-plugin: checking for updates from central
Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/2.2/maven-archetype-plugin-2.2.pom
1K downloaded (maven-archetype-plugin-2.2.pom)
Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetype/maven-archetype/2.2/maven-archetype-2.2.pom
2K downloaded (maven-archetype-2.2.pom)
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-parent/21/maven-parent-21.pom
4K downloaded (maven-parent-21.pom)
Downloading: http://repo1.maven.org/maven2/org/apache/apache/10/apache-10.pom
3K downloaded (apache-10.pom)
Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/2.2/maven-archetype-plugin-2.2.jar
78K downloaded (maven-archetype-plugin-2.2.jar)
[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
Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetype/archetype-catalog/2.2/archetype-catalog-2.2.pom
873b downloaded (archetype-catalog-2.2.pom)
Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetype/archetype-models/2.2/archetype-models-2.2.pom
1K downloaded (archetype-models-2.2.pom)
..
..
.. [downloading required pom and jar files]
..

Step 3:

Maven will display all the available archetype (while this execution I have got 607 archetypes) from Maven Repository.  For illustration purpose I will using maven quick start archetype.

Note: Default value of Archetype is “197: remote -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)”

[INFO] [archetype:generate {execution: default-cli}]
[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: remote -> com.agilejava.docbkx:docbkx-quickstart-archetype (-)
2: remote -> com.alibaba.citrus.sample:archetype-webx-quickstart (-)
3: remote -> com.bsb.common.vaadin:com.bsb.common.vaadin.embed-simple-archetype (-)
4: remote -> com.bsb.common.vaadin:com.bsb.common.vaadin7.embed-simple-archetype (-)
5: remote -> com.cedarsoft.open.archetype:multi (-)
6: remote -> com.cedarsoft.open.archetype:simple (-)
7: remote -> com.dyuproject.protostuff.archetype:basic-gwt-webapp (webapp archetype using protostuff, json and gwt)
8: remote -> com.dyuproject.protostuff.archetype:basic-webapp (webapp archetype using protostuff, json and jquery)
9: remote -> com.dyuproject.protostuff.archetype:simple-gwt-webapp (webapp archetype using protobuf, json and gwt)
10: remote -> com.dyuproject.protostuff.archetype:simple-webapp (webapp archetype using protobuf, json and jquery)
11: remote -> com.force.sdk:springmvc-archetype (-)
12: remote -> com.github.igor-petruk.archetypes:maven-archetype-executable (Executable Quickstart Archetype that is ready to run with 'java -jar')
13: remote -> com.github.igor-petruk.archetypes:maven-archetype-scala-executable (Creates executable Scala Project that is ready to run with 'java -jar')
14: remote -> com.github.jpaoletti:jpm-archetype (Archetype for a jpm-struts1-bootstrap project)
15: remote -> com.github.lalyos:standalone-jpa-eclipselink-archetype (StandAlone (j2se) jpa project with eclipseLink implementations using embedded DerbiDB)
16: remote -> com.github.venkatramanm.swf-all:swf-archetype (Archetype to create apps using SWF)
17: remote -> com.github.venkatramanm.swf-all:swf-plugin-archetype (Archetype to create plugins for SWF)
18: remote -> com.google.code.plsqlmaven:plsql-package-archetype (a sample archetype that creates a project with a PL/SQL package inside and extends the parent project)
19: remote -> com.google.code.plsqlmaven:plsql-project-archetype (preconfigured PL/SQL project)
20: remote -> com.google.code.plsqlmaven:plsql-webapp-archetype (preconfigured PL/SQL webapp)
21: remote -> com.googlecode.android-player-root-archetype:parent-archetype (-)
22: remote -> com.googlecode.apparat:apparat-archetype-asm (-)
23: remote -> com.googlecode.apparat:apparat-archetype-tdsi (-)
...
...
...
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 197: [Press enter/return key]
Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6: [Press enter/return key]
Step 4:

Now maven will display given properties for confirmation, provide ‘Y’ as a input

[INFO] Using property: groupId = com.myjeeva.maven
[INFO] Using property: artifactId = sampleapplication
[INFO] Using property: version = 1.0-SNAPSHOT
[INFO] Using property: package = com.myjeeva.maven
Confirm properties configuration:
groupId: com.myjeeva.maven
artifactId: sampleapplication
version: 1.0-SNAPSHOT
package: com.myjeeva.maven
Y: Y

Step 5:

Maven will create a project as given in the -DartifactId parameter and display you the BUILD SUCCESSFUL information

[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project: maven-archetype-quickstart:2.2
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.myjeeva.maven
[INFO] Parameter: packageName, Value: com.myjeeva.maven
[INFO] Parameter: package, Value: com.myjeeva.maven
[INFO] Parameter: artifactId, Value: sampleapplication
[INFO] Parameter: basedir, Value: /Users/jeeva/Documents/my-contributions/generic-repo
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created in dir: /Users/jeeva/Documents/my-contributions/generic-repo/sampleapplication
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 minutes 11 seconds
[INFO] Finished at: Tue Jun 26 16:13:42 GMT+05:30 2012
[INFO] Final Memory: 20M/265M
[INFO] ------------------------------------------------------------------------
mac-book-pro:generic-repo jeeva$

Result

That’s it, you have successfully created a Java/J2EE project using Maven.


Downloads & Checkout

This article generated ‘sampleapplication’ has been checked-in to GitHub git://github.com/jeevatkm/generic-repo.git
Also downloadable zip archive available click here!

Reference: maven.apache.org

Tagged with →  
  • http://fearofjava.wordpress.com KWood

    Great article, thanks! One question remains, though. Isn’t there a simpl(er) way to do this without having to scroll through a list of 600+ possible archetypes? If I know the archetype I want in advance, is there a way to specify that on the command line? Thanks in advance!

    • http://www.myjeeva.com Jeevanandam Madanagopal

      You’re welcome, Yes (if we know archetypeArtifactId) we can specify in command line.
      For Example:
      mvn archetype:generate -DgroupId=com.myjeeva.sample.app -DartifactId=sample-app -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

      Cheers, Jeeva

  • http://test322.com Benjamin Z Walton

    Hello, Nice blog