Create a Maven webapp in 60 seconds

We will start by using Maven to generate a template web application for us from the command line. Here is how.

Before starting, note that I am generally assuming you are using Mac OSX. However, the commands are similar enough that if you are using Windows, you shouldn’t have a problem. When I say TERMINAL (Mac OSX lingo) then that should translate to COMMAND PROMPT in Windows.

1. In TERMINAL, navigate to where your Eclipse workspace is.

$ cd /Users/altheaparker/Documents/workspace

2. Use the maven archetype webapp to create a default web application.

$ mvn archetype:create -DgroupId=com.bitbybit -DartifactId=SpringGreetings -DarchetypeArtifactId=maven-archetype-webapp

Here is the output:

althea-parkers-macbook-pro:workspace altheaparker$ mvn archetype:create -DgroupId=com.bitbybit -DartifactId=SpringGreetings -DarchetypeArtifactId=maven-archetype-webapp
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO] --- maven-archetype-plugin:2.0:create (default-cli) @ standalone-pom ---
[WARNING] This goal is deprecated. Please use mvn archetype:generate instead
[INFO] Defaulting package to group ID: com.bitbybit
Downloaded: (498 B at 0.5 KB/sec)
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.bitbybit
[INFO] Parameter: packageName, Value: com.bitbybit
[INFO] Parameter: package, Value: com.bitbybit
[INFO] Parameter: artifactId, Value: SpringGreetings3
[INFO] Parameter: basedir, Value: /Users/altheaparker/Documents/workspace
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] project created from Old (1.x) Archetype in dir: /Users/altheaparker/Documents/workspace/SpringGreetings3
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.752s
[INFO] Finished at: Wed Feb 08 11:47:23 CET 2012
[INFO] Final Memory: 7M/81M
[INFO] ------------------------------------------------------------------------
DONE! It was that simple. Now you can take a look at the code generated in the “SpringGreetings” folder inside your workspace.

Here is the resulting code that Maven should have generated for you.

In Windows this would look like….

What just happened?

Maven downloaded the default webapp archetype and put it in your workspace. We told maven to create a basic web application from its template archives when we specified the parameter


We also assigned our default package path with the -DgroupId parameter


and assigned the -DartifactId parameter -DartifactId=SpringGreetings (which is the project id, or rather, the name of the folder where everything will go).

The default maven webapp has one dependency, junit 3.8.1. Maven checked your local repository before downloading the dependency “junit-3.8.1.jar” to see if it exists there already. Your local repository is configured in the maven/conf/settings.xml file. The default location (in my installation anyway) is Users/altheaparker/.m2/repository.

What is Maven, anyway?

As described on Maven’s wiki page:

Maven is a build automation and software comprehension tool. While primarily used for Java programming, it can also be used to build and manage projects written in C#, Ruby, Scala, and other languages. Maven serves a similar purpose to the Apache Ant tool, but it is based on different concepts and works in a profoundly different manner. Maven is hosted by the Apache Software Foundation, where it was formerly part of the Jakarta Project.

Maven uses a construct known as a Project Object Model (POM) to describe the software project being built, its dependencies on other external modules and components, and the build order. It comes with pre-defined targets for performing certain well-defined tasks such as compilation of code and its packaging.

Maven dynamically downloads Java libraries and Maven plug-ins from one or more repositories. Maven provides built-in support for retrieving files from the Maven 2 Central Repository and other Maven repositories, and can upload artifacts to specific repositories after a successful build. A local cache of downloaded artifacts acts as the primary means of synchronizing the output of projects on a local system.

How to learn more about Maven?

The Sonatype Reference has a great introduction to Maven and a simple comparison to Ant.

The Apache Maven home page is another great resource and contains an easy Apache Maven – 5 minute guide.

Be sure to check out maven’s Standard Directory Layout

Don’t miss this essential site where you can find dependencies at the maven repository search site. This is cool!

Here is a handy list of archetypes.


Post a Comment