Usage

There are 4 ways to use the WAR Plugin:

  • using the package phase with the project package type as war
  • invocation of the war:war goal
  • invocation of the war:exploded goal
  • invocation of the war:inplace goal

Note: When using the war: goals it is assumed that the compile phase is already done. The WAR Plugin is not responsible for compiling the java sources or copying the resources.

To handle archiving this version of Maven WAR Plugin uses Maven Archiver 2.5.

To handle filtering this version of Maven WAR Plugin uses Maven Filtering 1.2.

Using the package phase with the project package type as war / invocation of the war:war goal

This is the normal way of using the WAR Plugin. To illustrate, here's the pom.xml for our project:

<project>
  ...
  <groupId>com.example.projects</groupId>
  <artifactId>documentedproject</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Documented Project</name>
  <url>http://example.com</url>
  ...
</project>

The project's structure looks like this:

 |-- pom.xml
 `-- src
     `-- main
         |-- java
         |   `-- com
         |       `-- example
         |           `-- projects
         |               `-- SampleAction.java
         |-- resources
         |   `-- images
         |       `-- sampleimage.jpg
         `-- webapp
             |-- WEB-INF
             |   `-- web.xml
             |-- index.jsp
             `-- jsp
                 `-- websource.jsp

Invoking

mvn package

or

mvn compile war:war

will generate the WAR file target/documentedproject-1.0-SNAPSHOT.war. Here are the contents of that WAR file:

documentedproject-1.0-SNAPSHOT.war
  |-- META-INF
  |   |-- MANIFEST.MF
  |   `-- maven
  |       `-- com.example.projects
  |           `-- documentedproject
  |               |-- pom.properties
  |               `-- pom.xml
  |-- WEB-INF
  |   |-- classes
  |   |   |-- com
  |   |   |   `-- example
  |   |   |       `-- projects
  |   |   |           `-- SampleAction.class
  |   |   `-- images
  |   |       `-- sampleimage.jpg
  |   `-- web.xml
  |-- index.jsp
  `-- jsp
      `-- websource.jsp

Invocation of war:exploded goal

To speed up testing during the developement phase, war:explode can be used to generate the WAR in exploded form. Use the same project as above and invoke:

mvn compile war:exploded

This will generate an exploded version of the WAR in target/documentedproject-1.0-SNAPSHOT. The contents of that directory looks like this:

 documentedproject-1.0-SNAPSHOT
 |-- META-INF
 |-- WEB-INF
 |   |-- classes
 |   |   |-- com
 |   |   |   `-- example
 |   |   |       `-- projects
 |   |   |           `-- SampleAction.class
 |   |   `-- images
 |   |       `-- sampleimage.jpg
 |   `-- web.xml
 |-- index.jsp
 `-- jsp
     `-- websource.jsp

The default directory for the exploded WAR is target/<finalName>. The finalName is usually in the form of <artifactId>-<version>. This default directory can be overridden by specifying the webappDirectory parameter.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <webappDirectory>/sample/servlet/container/deploy/directory</webappDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

Invocation of war:inplace goal

Another variation of war:exploded is war:inplace. With war:inplace the exploded WAR is created in the webapp source, which defaults to src/main/webapp. Use our sample project above, and invoke:

mvn compile war:inplace

This will result in:

 |-- pom.xml
 |-- src
 |   `-- main
 |       |-- java
 |       |   `-- com
 |       |       `-- example
 |       |           `-- projects
 |       |               `-- SampleAction.java
 |       |-- resources
 |       |   `-- images
 |       |       `-- sampleimage.jpg
 |       `-- webapp
 |           |-- META-INF
 |           |-- WEB-INF
 |           |   |-- classes
 |           |   |   |-- com
 |           |   |   |   `-- example
 |           |   |   |       `-- projects
 |           |   |   |           `-- SampleAction.class
 |           |   |   `-- images
 |           |   |       `-- sampleimage.jpg
 |           |   `-- web.xml
 |           |-- index.jsp
 |           `-- jsp
 |               `-- websource.jsp
 `-- target
     `-- classes
         |-- com
         |   `-- example
         |       `-- projects
         |           `-- SampleAction.class
         `-- images
             `-- sampleimage.jpg