Chapter 11. Building a WAR
11.1. Modifying an Existing Maven Project
Change the package type to WAR
warin your project's
pom.xmlfile. Change the contents of the
war, as shown in the following example:
<project ... > ... <packaging>war</packaging> ... </project>
maven-war-plugin, to perform packaging for this project.
Customize the JDK compiler version
PATHenvironment variables to the correct values for your JDK, you must also modify the POM file.
maven-compiler-pluginplug-in settings to your POM (if they are not already present):
<project ... > ... <build> <defaultGoal>install</defaultGoal> <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> ... </project>
Store resources under webapp/WEB-INF
/WEB-INFdirectory in the standard WAR directory layout. In order to ensure that these resources are copied into the root of the generated WAR package, store the
ProjectDir/src/main/webappin the Maven directory tree, as follows:
ProjectDir/ pom.xml src/ main/ webapp/ WEB-INF/ web.xml classes/ lib/
web.xmlfile is stored at
Customize the Maven WAR plug-in
pluginssection of the
pom.xmlfile. Most of the configuration options are concerned with adding additonal resources to the WAR package. For example, to include all of the resources under the
src/main/resourcesdirectory (specified relative to the location of
pom.xml) in the WAR package, you could add the following WAR plug-in configuration to your POM:
<project ...> ... <build> ... <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <!-- Optionally specify where the web.xml file comes from --> <webXml>src/main/webapp/WEB-INF/web.xml</webXml> <!-- Optionally specify extra resources to include --> <webResources> <resource> <directory>src/main/resources</directory> <targetPath>WEB-INF</targetPath> <includes> <include>**/*</include> </includes> </resource> </webResources> </configuration> </plugin> ... </plugins> </build> </project>
- Specifies where to find the
web.xmlfile in the current Maven project, relative to the location of
pom.xml. The default is
- Specifies additional resource files that are to be included in the generated WAR package. It can contain the following sub-elements:
webResources/resource—each resource elements specifies a set of resource files to include in the WAR.
webResources/resource/directory—specifies the base directory from which to copy resource files, where this directory is specified relative to the location of
webResources/resource/targetPath—specifies where to put the resource files in the generated WAR package.
webResources/resource/includes—uses an Ant-style wildcard pattern to specify explicitly which resources should be included in the WAR.
webResources/resource/excludes—uses an Ant-style wildcard pattern to specify explicitly which resources should be excluded from the WAR (exclusions have priority over inclusions).
maven-war-pluginplug-in, which has a bug that causes two copies of the
web.xmlfile to be inserted into the generated
Building the WAR
pom.xmlfile), and enter the following Maven command:
ProjectDir/targetdirectory, and then to install the generated WAR in the local Maven repository.