Monday, January 16, 2012

Maven 3: Building and Running a Java Web Application

You can easily create a web application with a Maven Archetype plugin. Following are the steps to generate a web application using Maven 3:

Start the command-line terminal and execute the following generate command:

mvn archetype:generate -DartifactId=testwebapp -DarchetypeArtifactId=maven-archetype-webapp

You should get the following output.

Notice that you will get a prompt asking for the version and package.

[INFO] Scanning for projects...
[INFO] -------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1 ------------------------
[INFO] >>> maven-archetype-plugin:2.0-alpha-5:generate (default-cli) @
standalone-pom >>>


[INFO] Parameter: groupId, Value:
[INFO] Parameter: packageName, Value:
[INFO] Parameter: package, Value:
[INFO] Parameter: artifactId, Value: my-webapp
[INFO] Parameter: basedir, Value: C:\test
[INFO] Parameter: version, Value: 1.0
[INFO] project created from Old (1.x) Archetype in dir: C:\my-web
[INFO] ---------------------------------------------------------------
[INFO] ---------------------------------------------------------------
[INFO] Total time: 11.288s
[INFO] Finished at: Mon Jan 16 00:38:44 CET 2012
[INFO] Final Memory: 9M/495M
[INFO] ---------------------------------------------------------------

The archetype:generate command executed before has created a new project folder, my-webapp, which contains the following POM file:

<project xmlns="" xmlns:xsi=""
  <name>my-webapp Maven Webapp</name>

Following is the structure of application generated:

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

The packaging element tells Maven to build as a WAR. Change into the webapp project's directory and write:

mvn clean package

This will generate my-webapp.war in 'target' directory.

Maven can automate the process of deploying, cleaning, and redeploying the WAR on a developer machine web application server, making the entire process less cumbersome for the developer. This is done with the Jetty plugin.

Using the Jetty Plugin enables you to quickly test your web application by skipping the last two steps. By default the Jetty Plugin scans target/classes for any changes in your Java sources and src/main/webapp for changes to your web sources. The Jetty Plugin will automatically reload the modified classes and web sources.

To use the Jetty Plugin just add the following in your pom.xml:

            <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">

Then start Jetty using:

mvn jetty:run

The command will block with Jetty listening on port 8080. Now check that my-webapp is running on localhost:8080/my-webapp.

1 comment:

TSzabo said...

Thank you for these instructions.
The JettyPlugin sniplet above have been corruptet though. Should be:

          <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">