由于公司配置的垃圾台式机,根本就没法开发,开一个sts,跑一个内嵌的tomcat就卡出翔,测试每次想在我机器上测试,所以弄了下maven下使用Cargo实现自动化部署,这样就可以把本地的应用部署到远程服务器上去,省的垃圾电脑卡出翔。
先在settings.xml里面的pluginGroups节点增加<pluginGroup>org.codehaus.cargo</pluginGroup>以便命令行调用,然后增加server
<server> <id>tomcat7x</id> <username>admin</username> <password>password</password> </server>
1.1 standalone模式
在standalone模式,Cargo会从Web容器的安装目录复制一份配置到用户指定的目录,然后在此基础上部署应用,每次重新构建的时候,这个目录都会被清空,所有配置被重新生成
<plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.4.9</version> <configuration> <container> <containerId>tomcat7x</containerId> <home>/usr/local/devtools/apache-tomcat-7.0.55</home> </container> <configuration> <type>standalone</type> <home>${project.build.directory}/tomcat7x</home> <properties> <!-- 更改监听端口 --> <cargo.servlet.port>8088</cargo.servlet.port> </properties> </configuration> </configuration> </plugin>
1.2 existing模式
在existing模式下,用户需要指定现有的web容器配置目录,然后Cargo会直接使用这些配置并将应用部署到其对应的位置
<plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.4.9</version> <configuration> <container> <containerId>tomcat7x</containerId> <home>/usr/local/devtools/apache-tomcat-7.0.55</home> </container> <configuration> <type>existing</type> <home>/usr/local/devtools/apache-tomcat-7.0.55</home> </configuration> </configuration> </plugin>
2.部署到远程Web容器
这里注意在远程部署模式下,container元素的type子元素的值必须为remote,如果不指定,Cargo会默认使用installed,并寻找对应的容器安装目录或者安装包,一般我们远程部署的服务器上都有设定好的web容器了,并不需要再区安装。
pom.xml
<!-- tomcat7 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <url>http://localhost:8080/manager/text</url> <URIEncoding>UTF-8</URIEncoding> <server>tomcat7x</server> <username>admin</username> <password>password</password> <path>/${project.artifactId}</path> </configuration> </plugin>
<plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.4.9</version> <configuration> <container> <containerId>tomcat7x</containerId> <type>remote</type> </container> <configuration> <type>runtime</type> <properties> <cargo.tomcat.manager.url>http://localhost:8080/manager/text</cargo.tomcat.manager.url> <cargo.remote.username>admin</cargo.remote.username> <cargo.remote.password>password</cargo.remote.password> </properties> </configuration> <deployables> <deployable> <groupId>io.steveguoshao</groupId> <artifactId>webapp</artifactId> <type>war</type> <properties> <context>/${project.artifactId}</context> </properties> <!-- 可选:验证是否部署成功 --> <pingURL>http://localhost:8080/webapp</pingURL> <!-- 可选:验证超时时间,默认是120000 毫秒--> <pingTimeout>60000</pingTimeout> </deployable> </deployables> </configuration> <executions> <execution> <id>verify-deployer</id> <phase>install</phase> <goals> <goal>deployer-redeploy</goal> </goals> </execution> <execution> <id>clean-deployer</id> <phase>clean</phase> <goals> <goal>deployer-undeploy</goal> </goals> </execution> </executions> </plugin>
在tomcat7的conf/tomcat-users.xml中增加角色和用户, 不然会报403,没法访问
<role rolename="manager-gui"/> <role rolename="manager-script"/> <role rolename="manager-jmx"/> <role rolename="manager-status"/> <role rolename="admin-gui"/> <user username="admin" password="password" roles="admin-gui,manager-gui,manager-script,manager-status"/>
http://localhost:8080/manager/text而tomcat6是
http://localhost:8080/manager/html
配置好之后就可以运行mvn cargo:redeploy 来部署应用了(必须保证tomcat是running状态,否则没法部署),如果容器中已经部署的当前应用,Cargo会先卸载掉原来的应用,然后再重新部署。
为什么在配置了
<goal>deployer-undeploy</goal>
的时候,明明刚刚install的时候已经生成了,但是却每次clean都报找不到target目录下war包呢?
3.Cargo插件中各个命令的之间的异同
Goals |
Description |
---|---|
|
Start a container. That goal will:
Note: A container that's started with |
|
Start a container and wait for the user to press
|
|
Stop a container. |
|
Stop and start again a container. If the container was not running before calling cargo:restart , it will simply be started. |
|
Create the configuration for a local container, without starting it. Note that the |
|
Package the local container. |
cargo:daemon-start |
Start a container via the daemon. Read more on: Cargo Daemon Note: The |
cargo:daemon-stop |
Stop a container via the daemon. Read more on: Cargo Daemon |
|
Deploy a deployable to a running container. Note: The |
|
Undeploy a deployable from a running container. |
|
Start a deployable already installed in a running container. |
|
Stop a deployed deployable without undeploying it. |
|
Undeploy and deploy again a deployable. If the deployable was not deployed before calling |
|
Merge several WAR files into one. |
|
Installs a container distribution on the file system. Note that the |
|
Get help (list of available goals, available options, etc.). |
从上面可以看出,cargo:start于cargo:run的不同之处了吧?cargo:start的生命周期依赖于maven实例的生命周期,也就是说,maven构建成功或者失败之后,cargo插件的生命周期也自动停止了;而cargo:run不同,不管maven是否构建成功或者失败,都必须手工去按Ctrl + C来停止。
参考资料:
1.徐文斌的《Maven实战》
2.http://cargo.codehaus.org/Maven2+Plugin+Reference+Guide
3.http://cargo.codehaus.org/Maven2+plugin
4.http://cargo.codehaus.org/Deploying+to+a+running+container