There are 4 ways to use the WAR Plugin:
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.1.
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
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.4</version> <configuration> <webappDirectory>/sample/servlet/container/deploy/directory</webappDirectory> </configuration> </plugin> </plugins> </build> ... </project>
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