pom.xmlの内容は以下です。
[pom.xml]
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>パッケージ名</groupId>
<artifactId>プロジェクト名</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>名前</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>ファイル名</finalName>
<plugins>
<!-- For Juit 4 -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
</plugin>
<!-- snip -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>exec-tomcat</id>
<phase>package</phase>
<goals>
<goal>redeploy</goal>
</goals>
<configuration>
<path>/mavenWeb</path>
<server>tomcat-localhost</server>
<url>http://localhost:8080/manager/text</url>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>パッケージ名</groupId>
<artifactId>プロジェクト名</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>名前</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>ファイル名</finalName>
<plugins>
<!-- For Juit 4 -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
</plugin>
<!-- snip -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>exec-tomcat</id>
<phase>package</phase>
<goals>
<goal>redeploy</goal>
</goals>
<configuration>
<path>/mavenWeb</path>
<server>tomcat-localhost</server>
<url>http://localhost:8080/manager/text</url>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
pluginにtomcat7-maven-pluginを追加しています。
また、executionタグを設定し、packageフェーズで自動的に
再デプロイされるようにしてみました。
フェーズは順番にcompile→test→package→install→deployと
実行されていきます。
テスト後に実行されるようpackageにしてみました。
再デプロイをしたくなければ、goalにdeployを指定します。
configuration以下には設定を記述します。
pathはデプロイ先、urlはTomcat ManagerのURLを指定します。
また、tomcatへログインする為の設定ファイルが必要です。
設定ファイルはユーザのフォルダ直下にある.m2フォルダに
用意します。
[.m2/settings.xml]
<settings>
<servers>
<server>
<id>tomcat-localhost</id>
<username>tomcat</username>
<password>tomcat</password>
</server>
</servers>
</settings>
<servers>
<server>
<id>tomcat-localhost</id>
<username>tomcat</username>
<password>tomcat</password>
</server>
</servers>
</settings>
Tomcat Managerにアクセスする必要があるので
Tomcatのmanager-script権限を持ったユーザの
名前とパスワードを指定します。
今回までの設定によりMavenのpackageフェーズ以降まで
実行すればコンパイル、ユニットテスト、Warファイルの作成、
Tomcatへのデプロイといったことが自動的に行われます。
勿論、ユニットテストでエラーがあればWarファイルの作成や
デプロイは行われません。
一度、設定してしまえば、これらの作業が自動的に
行われるので、かなり便利ではないでしょうか。

