Maven cargo 远程自动部署war
如果不使用maven构建项目,则可以直接考虑将war包直接通过cargo远程部署到远端服务器上
(也就是说先创建个maven项目,打好包,再通过shell脚本,将所需要的项目打成war包,放到target目录下,对war包进行覆盖)。
打war包:
cd 项目目录,即WEB-INF所在目录
jar -cfM0 /app/proj-1.0-SNAPSHOT.war ./
1、创建maven项目
mvn archetype:generate
2、在pom.xml中配置maven-war-plugin和cargo-maven2-plugin
<build> <plugins> <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.4.9</version> <configuration> <container> <containerId>tomcat5x</containerId> <type>remote</type> </container> <configuration> <type>runtime</type> <properties> <cargo.remote.uri>http://你的IP:8080/manager</cargo.remote.uri> <cargo.remote.username>admin</cargo.remote.username> <cargo.remote.password>admin</cargo.remote.password> </properties> </configuration> <deployables> <deployable> <groupId>${project.groupId}</groupId> <artifactId>${project.artifactId}</artifactId> <type>war</type> <properties> <!-- 使用根路径,作为上下文 --> <context>/</context> </properties> <!-- 可选:验证是否部署成功 --> <pingURL>http://你的IP:8080</pingURL> <!-- 可选:验证超时时间,默认是120000 毫秒--> <pingTimeout>60000</pingTimeout> </deployable> </deployables> </configuration> </plugin> <!-- 本地部署 <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.4.0</version> <configuration> <container> <containerId>tomcat5x</containerId> <home>D:\Tomcat5.5</home> </container> <configuration> <type>existing</type> <home>D:\Tomcat5.5</home> </configuration> </configuration> </plugin> --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.2</version> <configuration> <webXml>src\webapps\WEB-INF\web.xml</webXml> </configuration> </plugin> </plugins> </build> </project>
3、修改tomcat配置
Tomcat5.5\conf\server.xml
<Host
appBase="webapps"
name="localhost">
<!--上面cargo进行部署时使用的根路径,这里就不要配置了-->
<!-- Context path="" docBase="app" debug="0" reloadable="false" crossContext="true"/-->
<!--配置tomcat的manager http://localhost:8080/manager/html-->
<Context path="/manager" docBase="/app/Tomcat5.5/server/webapps/manager" debug="0" privileged="true"/>
</Host>
Tomcat5.5\conf\tomcat-users.xml 添加
<role rolename="admin"/>
<user username="admin" password="admin" roles="admin,manager"/>
Tomcat8 则使用
<role rolename="admin"/>
<user username="admin" password="admin" roles="admin,manager-script,manager-gui"/>
相关权限都可以在/webapps/manager/WEB-INFO/web.xml里找到
发布主要使用manager-script权限,界面的展示主要使用manager-gui权限
访问的地址:http://你的IP:你的端口/manager/
同时POM中的配置也需要调整:
<containerId>tomcat8x</containerId>
<cargo.remote.uri>http://127.0.0.1:8080/manager/text</cargo.remote.uri>
<configuration> <container> <containerId>tomcat8x</containerId> <type>remote</type> <timeout>120000</timeout> </container> <configuration> <type>runtime</type> <properties> <cargo.remote.uri>http://127.0.0.1:8080/manager/text</cargo.remote.uri> <cargo.remote.username>admin</cargo.remote.username> <cargo.remote.password>admin</cargo.remote.password> </properties> </configuration>对于上传的war包过大的问题,也可以进行一些配置/webapps/manager/WEB-INFO/web.xml
<multipart-config> <!-- 50MB max --> <max-file-size>-1</max-file-size> <max-request-size>-1</max-request-size> <file-size-threshold>0</file-size-threshold> </multipart-config>
改成-1不再受限制,默认的是50M
出现这个错误时需要修改如下文件
org.apache.catalina.servlets.InvokerServlet is privileged and cannot be loaded by this web application.
Tomcat5.5\conf\context.xml
将<Context>替换成<Context reloadable="true" privileged="true">
4、启动Tomcat
5、进入项目目录,即pom.xml文件所在目录
mvn clean install package
mvn cargo:deploy 或者 mvn cargo:redeploy 或者 mvn cargo:undeploy
查看日志信息可以加 -X 例如:mvn cargo:deploy -X
注意:
如果是发布在根路径,即使用http://你的IP:你的端口/
需要把webapps/ROOT 删除,以避免冲突