WEB+DB PRESSを参考にしつつCargo Maven2 Pluginを使った自動デプロイに挑戦してみました。
■ Cargo Maven2 Pluginとは?
Cargo Maven2 PluginはTomcatの起動・終了などの操作をMaven2から行えるようにするプラグインらしいです。 リモートのTomcatに対してデプロイ・アンデプロイする機能を持つので、こんなに簡単だと。
■ pom.xmlの編集
まずpom.xmlを編集します。pluginRepositoriesを追加します。
<pluginRepositories>
<pluginRepository>
<id>codehaus snapshot repository</id>
<url>http://snapshots.repository.codehaus.org/</url>
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
pluginを追加します。
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<container>
<containerId>tomcat5x</containerId>
<type>remote</type>
</container>
<configuration>
<type>runtime</type>
<properties>
<cargo.tomcat.manager.url>http://dev:8080/manager</cargo.tomcat.manager.url>
<cargo.remote.username>tomcat</cargo.remote.username><!--変更-->
<cargo.remote.password>tomcat</cargo.remote.password><!--変更-->
</properties>
</configuration>
<deployer>
<type>remote</type>
<deployables>
<deployable>
<groupId>com.examples</groupId><!--変更-->
<artifactId>exweb</artifactId><!--変更-->
<type>war</type>
</deployable>
</deployables>
</deployer>
</configuration>
</plugin>
■ mvnコマンドの実行
mvn clean package cargo:undeploy cargo:deploy
成功すると、http://dev:8080/で動作するTomcatに成果物がデプロイされます。
■ 補足
Tomcat managerでtomcatというユーザーが設定されていないとうまく動きません。 あらかじめ設定しておいて下さい。tomcat/tomactを追加する場合、TOMCAT_HOME/conf/tomcat-users.xml を変更します。
File Edit Options Buffers Tools Minibuf Help <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename="tomcat"/> <role rolename="role1"/> <role rolename="manager"/> <role rolename="admin"/> <user username="tomcat" password="tomcat" roles="tomcat,manager,admin"/> <user username="both" password="tomcat" roles="tomcat,role1"/> <user username="role1" password="tomcat" roles="role1"/> </tomcat-users>