Skip to main content

Creating Maven projects from the command line - the easy way

Posted by johnsmart on August 7, 2008 at 12:53 AM PDT

Maven archetypes are great. But remember how painful it is to create a new Maven project from the command line, with all those command-line options to remember? Enough to put off even the most hardened Maven enthusiast!

Sure, you can always use your trusty IDE. But what about those times when you really need to create a Maven project from the command line. Maybe your using a machine without all the right plugins for your favorite IDE. Maybe the machine doesn't have your favorite IDE. But, there you are, still needing to create a Maven project, and not wanting to do it by hand.

Fortunately, there is a better way. You can still use the Maven archetype plugin, but if you use the archetype:generate goal, Maven will step you through the options using a command-line wizard. The first thing this plugin does is to propose a somewhat imposing list of available archetypes (44 at last count), each with a unique number: you'll never have to remember the exact name of an archetype again!

To get started, just open a command line window in your project directory and type archetype:generate:

C:\Users\john\projects>mvn archetype:generate
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Pet Database Core Module
[INFO]    task-segment: [archetype:generate] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextCla
ssLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:generate]
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-arc
hetype-quickstart:1.0)
Choose archetype:
1: internal -> appfuse-basic-jsf (AppFuse archetype for creating a web application with Hibernate, S
pring and JSF)
2: internal -> appfuse-basic-spring (AppFuse archetype for creating a web application with Hibernate
, Spring and Spring MVC)
3: internal -> appfuse-basic-struts (AppFuse archetype for creating a web application with Hibernate
, Spring and Struts 2)
4: internal -> appfuse-basic-tapestry (AppFuse archetype for creating a web application with Hiberna
te, Spring and Tapestry 4)
5: internal -> appfuse-core (AppFuse archetype for creating a jar application with Hibernate and Spr
ing and XFire)
6: internal -> appfuse-modular-jsf (AppFuse archetype for creating a modular application with Hibern
ate, Spring and JSF)
7: internal -> appfuse-modular-spring (AppFuse archetype for creating a modular application with Hib
ernate, Spring and Spring MVC)
8: internal -> appfuse-modular-struts (AppFuse archetype for creating a modular application with Hib
ernate, Spring and Struts 2)
9: internal -> appfuse-modular-tapestry (AppFuse archetype for creating a modular application with H
ibernate, Spring and Tapestry 4)
10: internal -> maven-archetype-j2ee-simple (A simple J2EE Java application)
11: internal -> maven-archetype-marmalade-mojo (A Maven plugin development project using marmalade)
12: internal -> maven-archetype-mojo (A Maven Java plugin development project)
13: internal -> maven-archetype-portlet (A simple portlet application)
14: internal -> maven-archetype-profiles ()
15: internal -> maven-archetype-quickstart ()
16: internal -> maven-archetype-site-simple (A simple site generation project)
17: internal -> maven-archetype-site (A more complex site project)
18: internal -> maven-archetype-webapp (A simple Java web application)
19: internal -> struts2-archetype-starter (A starter Struts 2 application with Sitemesh, DWR, and Sp
ring)
20: internal -> struts2-archetype-blank (A minimal Struts 2 application)
21: internal -> struts2-archetype-portlet (A minimal Struts 2 application that can be deployed as a
portlet)
22: internal -> struts2-archetype-dbportlet (A starter Struts 2 portlet that demonstrates a simple C
RUD interface with db backing)
23: internal -> struts2-archetype-plugin (A Struts 2 plugin)
24: internal -> shale-archetype-blank (A blank Shale web application with JSF)
25: internal -> maven-adf-archetype (Archetype to ease the burden of creating a new application base
d with ADF)
26: internal -> data-app (A new Databinder application with sources and resources.)
27: internal -> jini-service-archetype (Archetype for Jini service project creation)
28: internal -> softeu-archetype-seam (JSF+Facelets+Seam Archetype)
29: internal -> softeu-archetype-seam-simple (JSF+Facelets+Seam (no persistence) Archetype)
30: internal -> softeu-archetype-jsf (JSF+Facelets Archetype)
31: internal -> jpa-maven-archetype (JPA application)
32: internal -> spring-osgi-bundle-archetype (Spring-OSGi archetype)
33: internal -> confluence-plugin-archetype (Atlassian Confluence plugin archetype)
34: internal -> jira-plugin-archetype (Atlassian JIRA plugin archetype)
35: internal -> maven-archetype-har (Hibernate Archive)
36: internal -> maven-archetype-sar (JBoss Service Archive)
37: internal -> wicket-archetype-quickstart (A simple Apache Wicket project)
38: internal -> quickstart (A simple Apache Tapestry 5 Project)
39: internal -> scala-archetype-simple (A simple scala project)
40: internal -> lift-archetype-blank (A blank/empty liftweb project)
41: internal -> lift-archetype-basic (The basic (liftweb) project)
42: internal -> cocoon-22-archetype-block-plain ([http://cocoon.apache.org/2.2/maven-plugins/])
43: internal -> cocoon-22-archetype-block ([http://cocoon.apache.org/2.2/maven-plugins/])
44: internal -> cocoon-22-archetype-webapp ([http://cocoon.apache.org/2.2/maven-plugins/])
Choose a number:  (1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/
31/32/33/34/35/36/37/38/39/40/41/42/43/44) 15: :18

The default archetype is the "maven-archetype-quickstart", the simple Maven project structure that we are all familiar with. But there are plenty of others to choose from, including all of the appfuse archetypes, and many other technology stacks.

Next, you are prompted for the groupId, artifactId, version and package:

[INFO] artifact org.apache.maven.archetypes:maven-archetype-webapp: checking for updates from maven-
archetype-webapp-repo
[INFO] artifact org.apache.maven.archetypes:maven-archetype-webapp: checking for updates from centra
l
Downloading: http://localhost:8081/nexus/content/groups/public/org/apache/maven/arche... downloaded
Define value for groupId: : com.wakaleo.onlinebank
Define value for artifactId: : onlinebank-core
Define value for version:  1.0-SNAPSHOT: :
Define value for package: : com.wakaleo.onlinebank.core

Finally, you can review your choices, and, if you are happy, go ahead and create the project.

Confirm properties configuration:
groupId: com.wakaleo.onlinebank
artifactId: onlinebank-core
version: 1.0-SNAPSHOT
package: com.wakaleo.onlinebank.core
Y: :
  Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: maven-archetype-quickstart:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.wakaleo.onlinebank
[INFO] Parameter: packageName, Value: com.wakaleo.onlinebank.core
[INFO] Parameter: package, Value: com.wakaleo.onlinebank.core
[INFO] Parameter: artifactId, Value: onlinebank-core
[INFO] Parameter: basedir, Value: C:\Users\john\projects\onlinkbank
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM *******************
****
[INFO] OldArchetype created in dir: C:\Users\john\projects\onlinkbank\onlinebank-core
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 27 seconds
[INFO] Finished at: Thu Aug 07 19:51:37 NZST 2008
[INFO] Final Memory: 8M/15M
[INFO] ------------------------------------------------------------------------

All in all, a very convenient way to create a Maven project structure with a minimum of fuss.

Comments

Thank you for this tip!