在给Maven项目进行package打包时,希望能自动将生成的WAR包自动发布到Tomcat中,而插件exec-maven-plugin正好可以使用。
1. 编写一个批处理脚本D:\Program Files\apache-maven-3.2.3\mytools\copy.bat (Linux下可相应编写shell脚本)
@echo off
rmdir /s %3
copy /y %1 %2
2. 在pom.xml中加入如下代码(可按需修改Tomcat的路径和WAR文件名)
<project>
...
<properties>
<copy-cmd>
"D:/Program Files/apache-maven-3.2.3/mytools/copy.bat"
</copy-cmd>
<output-war>
"D:/Program Files/eclipse-jee-kepler-R-win32/workspace/Jersey/${project.build.finalName}/target/${project.build.finalName}.war"
</output-war>
<tomcat-webapps-path>
"D:/Program Files/apache-tomcat-6.0.26/webapps"
</tomcat-webapps-path>
<tomcat-webapps-old-project>
"${tomcat-webapps-path}/${project.build.finalName}"
</tomcat-webapps-old-project>
</properties>
...
<build>
<finalName>JerseyCh3</finalName>
<plugins>
<!-- 复制WAR包到Tomcat中的插件(调用操作系统批处理命令) -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>Creating a Production Build with Sencha Command</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>
${copy-cmd} ${output-war} ${tomcat-webapps-path} ${tomcat-webapps-old-project}
</executable>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
这样,当给Maven项目进行package打包的时候,就会自动清理Tomcat里的旧项目,同时把生成的WAR包复制过去。