Das WAR Maven Plugin ist ein Maven Plugin, das mehr Freiheiten beim Erstellen von WAR-Files erlaubt. Hauptzweck ist das Verwenden von Embedded Tomcat beim Entwickeln von Projekten. Darüber hinaus ist das Plugin schneller, da es die abhängigen Artifacts nicht in ein temporäres WEB-INF/lib kopiert.
Konfiguration#
Das Einbinden erfolgt durch die pom.xml als Build-Extension:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<packaging>war<packaging>
...
<build>
<extensions>
<extension>
<groupId>com.intersult</groupId>
<artifactId>war-maven-plugin</artifactId>
<version>1.2</version>
</extension>
</extensions>
...
</build>
</project>
Das Erzeugen des WAR-Verzeichnis kann man nun selbst gestalten. Anbei eine Standard-Konfiguration zum Compilieren der Java-Dateien und um die Web-Dateien in das WAR-Directory zu kopieren:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-jsp</id>
<phase>compile</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/webapp</directory>
<excludes>
<exclude>**/*.xml</exclude>
</excludes>
</resource>
</resources>
<outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Das Projekt kann dann mit "mvn compile" gebaut werden und mit "mvn package" bzw. "mvn install" wird das WAR-Archiv direkt im target gebaut.
Hinweis: Das Resource-Plugin ist so konfiguriert, dass es beim Einbinden in Eclipse über das Maven-Eclipse-Plugin (M2E) die Web-Resources einzeln ersetzt. Damit ist ein Hot-Deploy von XHTML-Dateien möglich.
Tomcat-Konfiguration#
Der Tomcat kann dann direkt darin gestartet werden, ohne dass die Dateien in WEB-INF/lib stören würden:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<warSourceDirectory>${project.build.directory}/${project.build.finalName}</warSourceDirectory>
</configuration>
</plugin>
Tomcat kann mit "mvn tomcat7:run" bzw. "mvnDebug tomcat7:run" direkt gestartet werden und mit dem Debugger verbunden. Das Ganze geht auch mit älteren Tomcat-Versionen, entsprechend ist dann tomcat6-maven-plugin zu verwenden.

