3.2. Modifying an Existing Maven Project


Overview

If you already have a Maven project and you want to modify it so that it generates a WAR, perform the following steps:

Change the package type to WAR

Configure Maven to generate a WAR by changing the package type to war in your project's pom.xml file. Change the contents of the packaging element to war, as shown in the following example:
<project ... >
  ...
  <packaging>war</packaging>
  ...
</project>
The effect of this setting is to select the Maven WAR plug-in, maven-war-plugin, to perform packaging for this project.

Customize the JDK compiler version

It is almost always necessary to specify the JDK version in your POM file. If your code uses any modern features of the Java language—such as generics, static imports, and so on—and you have not customized the JDK version in the POM, Maven will fail to compile your source code. It is not sufficient to set the JAVA_HOME and the PATH environment variables to the correct values for your JDK, you must also modify the POM file.
To configure your POM file, so that it accepts the Java language features introduced in JDK 1.7, add the following maven-compiler-plugin plug-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

Resource files for the Web application are stored under the /WEB-INF directory 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 WEB-INF directory under ProjectDir/src/main/webapp in the Maven directory tree, as follows:
ProjectDir/
    pom.xml
    src/
        main/
            webapp/
                WEB-INF/
 web.xml
                    classes/
                    lib/
In particular, note that the web.xml file is stored at ProjectDir/src/main/webapp/WEB-INF/web.xml.

Customize the Maven WAR plug-in

It is possible to customize the Maven WAR plug-in by adding an entry to the plugins section of the pom.xml file. 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/resources directory (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>
The preceding plug-in configuration customizes the following settings:
webXml
Specifies where to find the web.xml file in the current Maven project, relative to the location of pom.xml. The default is src/main/webapp/WEB-INF/web.xml.
webResources
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 pom.xml.
  • 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).
For complete details of how to configure the Maven WAR plug-in, see http://maven.apache.org/plugins/maven-war-plugin/index.html.
Note
Do not use version 2.1 of the maven-war-plugin plug-in, which has a bug that causes two copies of the web.xml file to be inserted into the generated .war file.

Building the WAR

To build the WAR defined by the Maven project, open a command prompt, go to the project directory (that is, the directory containing the pom.xml file), and enter the following Maven command:
mvn install
The effect of this command is to compile all of the Java source files, to generate a WAR under the ProjectDir/target directory, and then to install the generated WAR in the local Maven repository.
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.