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 -DgroupId=com.example.app -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]
[INFO] >>> maven-archetype-plugin:2.0-alpha-5:generate (default-cli) @
standalone-pom >>>

........

[INFO] Parameter: groupId, Value: com.example.app
[INFO] Parameter: packageName, Value: com.example.app
[INFO] Parameter: package, Value: com.example.app
[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
app
[INFO] ---------------------------------------------------------------
[INFO] BUILD SUCCESS
[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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelversion>4.0.0</modelVersion>
  <groupid>com.example.app</groupId>
  <artifactid>my-webapp</artifactId>
  <packaging>war</packaging>
  <version>1.0</version>
  <name>my-webapp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupid>junit</groupId>
      <artifactid>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalname>my-webapp</finalName>
  </build>
</project>

Following is the structure of application generated:

my-webapp
  |
  |--- 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:

<build>
    <finalname>my-webapp</finalName>
 <plugins>
      <plugin>
        <groupid>org.mortbay.jetty</groupId>
        <artifactid>maven-jetty-plugin</artifactId>
        <version>6.1.10</version>
        <configuration>
          <scanintervalseconds>10</scanIntervalSeconds>
          <connectors>
            <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
              <port>8080</port>
              <maxidletime>60000</maxIdleTime>
            </connector>
          </connectors>
        </configuration>
      </plugin>
    </plugins>
  </build>

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:

  <plugins>
    <plugin>
      <groupId>org.mortbay.jetty</groupId>
      <version>6.1.10</version>
      <configuration>
        <scanIntervalSeconds>10</scanIntervalSeconds>
        <connectors>
          <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
            <port>8080</port>
            <maxIdleTime>60000</maxIdleTime>
          </connector>
        </connectors>
      </configuration>
    </plugin>
  </plugins>

Regards
Tomas