由於公司配置的垃圾臺式機,根本就沒法開發,開一個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.部署到本地Web容器
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>
然後用mvn cargo:run啓動,關於cargo:run於cargo:start有什麼區別,後續會講到。
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>
然後運行cargo:run之後在對應的tomcat的webapps目錄下能夠看到被部署的應用
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"/>
另外還有一點要注意的是url,tomcat7是
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