QuickTip : Generating project structure using Maven

Posted on Monday, January 28, 2008


When you are starting a new project it is very easy to get started if the project structure can be built for you. Maven provides a plugin called maven-archetype-plugin which can help you generate the desired project structure depending on the application that you are trying to build.

The Archetype Plugin allows the user to build archetype containers. These archetype containers contains the pom, default source, test, resources, test-resources, and site-resources files in a specific directory structure. The content of the pom, the default files, and the directory structure depends on the what archetype container was built.

To create a simple java application you can use

mvn archetype:create -DgroupId=[your project’s group id] -DartifactId=[your project’s artifact id] -DarchetypeArtifactId=maven-archetype-quickstart

Likewise to create a webapp use something like this

mvn archetype:create -DgroupId=org.vh.hibjpa -DartifactId=hibernatejpa_webapp -DarchetypeArtifactId=maven-archetype-webapp

we would end up with a structure like this

 |-- src
 |   `-- main
 |       `-- java
 |           |-- resources
 |           |-- webapp
 |           |   `-- WEB-INF
 |           |       `-- web.xml
 |           `-- index.jsp
  `-- pom.xml

There are several archetype containers available. A full list of containers shows 10 of them. List of some more useful archetypes which are not present on the apache site.

Once the project is generated go into the project and type mvn eclipse:eclipse, this would make it work with the eclipse ide. You would have to add the M2_REPO variable to the eclipse environment. If the archetype container is not present in your repository then it is downloaded once the archetype:create is executed.

AddThis Social Bookmark Button

Posted in: Java, Maven