To create your web application project, run mvn archetype:generate with
an artifactId and a groupId. Run
archetype:generate as shown below, choose archetype #19
"maven-archetype-webapp", and then press Y to confirm and create the new
web application project:
$ mvn archetype:generate -DgroupId=org.sonatype.mavenbook.simpleweb \ -DartifactId=simple-webapp \ -Dpackage=org.sonatype.mavenbook \ -Dversion=1.0-SNAPSHOT ... [INFO] [archetype:generate {execution: default-cli}] Choose archetype: ... 19: internal -> maven-archetype-webapp (A simple Java web application) ... Choose a number: (...) 15: : 19 Confirm properties configuration: groupId: org.sonatype.mavenbook.simpleweb artifactId: simple-webapp version: 1.0-SNAPSHOT package: org.sonatype.mavenbook.simpleweb Y: : Y [INFO] Parameter: groupId, Value: org.sonatype.mavenbook.simpleweb [INFO] Parameter: packageName, Value: org.sonatype.mavenbook.simpleweb [INFO] Parameter: package, Value: org.sonatype.mavenbook.simpleweb [INFO] Parameter: artifactId, Value: simple-webapp [INFO] Parameter: basedir, Value: /private/tmp [INFO] Parameter: version, Value: 1.0-SNAPSHOT ... [INFO] BUILD SUCCESSFUL
Once the Maven Archetype plugin creates the project, change
directories into the simple-webapp directory and take a look at
the pom.xml. You should see the XML document shown in the following
example:
Example 5.1. Initial POM for the simple-webapp project
<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>org.sonatype.mavenbook.simpleweb</groupId>
<artifactId>simple-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-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>simple-webapp</finalName>
</build>
</project>
Next, you will need to configure the Maven Compiler plugin to target
Java 5. To do this, add the plugins element to the
initial POM as shown in Example 5.2, “POM for the simple-webapp project with
compiler configuration”.
Example 5.2. POM for the simple-webapp project with compiler configuration
<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>org.sonatype.mavenbook.simpleweb</groupId>
<artifactId>simple-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-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>simple-webapp</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Notice the packaging element contains the value
war. This packaging type is what configures Maven to
produce a web application archive in a
WAR file. A project with war
packaging is going to create a WAR file in the
target/ directory. The default name of this file is
${artifactId}-${version}.war. In this project, the
default WAR would be generated in
target/simple-webapp-1.0-SNAPSHOT.war. In the
simple-webapp project, we’ve customized the name of the
generated WAR file by adding a
finalName element inside of this project’s build
configuration. With a finalName of
simple-webapp, the package phase
produces a WAR file in
target/simple-webapp.war.
