Maven使用Cargo實現自動化部署

原文:http://blog.csdn.net/steveguoshao/article/details/38469713html



因爲公司配置的垃圾臺式機,根本就無法開發,開一個sts,跑一個內嵌的tomcat就卡出翔,測試每次想在我機器上測試,因此弄了下maven下使用Cargo實現自動化部署,這樣就能夠把本地的應用部署到遠程服務器上去,省的垃圾電腦卡出翔。
web


先在settings.xml裏面的pluginGroups節點增長<pluginGroup>org.codehaus.cargo</pluginGroup>以便命令行調用,而後增長serverapache

[html]  view plain copy print ? 在CODE上查看代碼片 派生到個人代碼片
  1. <server>  
  2.     <id>tomcat7x</id>  
  3.     <username>admin</username>  
  4.     <password>password</password>  
  5. </server>  

1.部署到本地Web容器

1.1 standalone模式tomcat

 在standalone模式,Cargo會從Web容器的安裝目錄複製一份配置到用戶指定的目錄,而後在此基礎上部署應用,每次從新構建的時候,這個目錄都會被清空,全部配置被從新生成
服務器

[html]  view plain copy print ? 在CODE上查看代碼片 派生到個人代碼片
  1.  <plugin>  
  2.     <groupId>org.codehaus.cargo</groupId>  
  3.     <artifactId>cargo-maven2-plugin</artifactId>  
  4.     <version>1.4.9</version>  
  5.     <configuration>  
  6.         <container>  
  7.             <containerId>tomcat7x</containerId>  
  8.             <home>/usr/local/devtools/apache-tomcat-7.0.55</home>  
  9.         </container>  
  10.         <configuration>  
  11.             <type>standalone</type>  
  12.             <home>${project.build.directory}/tomcat7x</home>  
  13.             <properties>  
  14.                 <!-- 更改監聽端口 -->  
  15.                 <cargo.servlet.port>8088</cargo.servlet.port>  
  16.             </properties>  
  17.         </configuration>  
  18.     </configuration>  
  19. </plugin>  

而後用mvn cargo:run啓動,關於cargo:run於cargo:start有什麼區別,後續會講到。


1.2 existing模式app

在existing模式下,用戶須要指定現有的web容器配置目錄,而後Cargo會直接使用這些配置並將應用部署到其對應的位置
webapp

[html]  view plain copy print ? 在CODE上查看代碼片 派生到個人代碼片
  1. <plugin>  
  2.     <groupId>org.codehaus.cargo</groupId>  
  3.     <artifactId>cargo-maven2-plugin</artifactId>  
  4.     <version>1.4.9</version>  
  5.     <configuration>  
  6.         <container>  
  7.             <containerId>tomcat7x</containerId>  
  8.             <home>/usr/local/devtools/apache-tomcat-7.0.55</home>  
  9.         </container>  
  10.         <configuration>  
  11.             <type>existing</type>  
  12.             <home>/usr/local/devtools/apache-tomcat-7.0.55</home>  
  13.         </configuration>  
  14.     </configuration>  
  15. </plugin>  

而後運行cargo:run以後在對應的tomcat的webapps目錄下可以看到被部署的應用


2.部署到遠程Web容器maven

這裏注意在遠程部署模式下,container元素的type子元素的值必須爲remote,若是不指定,Cargo會默認使用installed,並尋找對應的容器安裝目錄或者安裝包,通常咱們遠程部署的服務器上都有設定好的web容器了,並不須要再區安裝。ide

pom.xml測試

[html]  view plain copy print ? 在CODE上查看代碼片 派生到個人代碼片
  1. <!-- tomcat7 -->  
  2. <plugin>  
  3.     <groupId>org.apache.tomcat.maven</groupId>  
  4.     <artifactId>tomcat7-maven-plugin</artifactId>  
  5.     <version>2.2</version>  
  6.     <configuration>  
  7.         <url>http://localhost:8080/manager/text</url>  
  8.         <URIEncoding>UTF-8</URIEncoding>  
  9.         <server>tomcat7x</server>  
  10.         <username>admin</username>  
  11.         <password>password</password>  
  12.         <path>/${project.artifactId}</path>  
  13.     </configuration>  
  14. </plugin>  

[html]  view plain copy print ? 在CODE上查看代碼片 派生到個人代碼片
  1. <plugin>  
  2.     <groupId>org.codehaus.cargo</groupId>  
  3.     <artifactId>cargo-maven2-plugin</artifactId>  
  4.     <version>1.4.9</version>  
  5.     <configuration>  
  6.         <container>  
  7.             <containerId>tomcat7x</containerId>  
  8.             <type>remote</type>  
  9.         </container>  
  10.         <configuration>  
  11.             <type>runtime</type>  
  12.             <properties>  
  13.                 <cargo.tomcat.manager.url>http://localhost:8080/manager/text</cargo.tomcat.manager.url>  
  14.                 <cargo.remote.username>admin</cargo.remote.username>  
  15.                 <cargo.remote.password>password</cargo.remote.password>  
  16.             </properties>  
  17.         </configuration>  
  18.         <deployables>  
  19.             <deployable>            
  20.                 <groupId>io.steveguoshao</groupId>    
  21.                 <artifactId>webapp</artifactId>    
  22.                 <type>war</type>    
  23.                 <properties>    
  24.                     <context>/${project.artifactId}</context>  
  25.                 </properties>    
  26.                 <!-- 可選:驗證是否部署成功 -->  
  27.                 <pingURL>http://localhost:8080/webapp</pingURL>  
  28.                 <!-- 可選:驗證超時時間,默認是120000 毫秒-->  
  29.                 <pingTimeout>60000</pingTimeout>  
  30.             </deployable>  
  31.         </deployables>  
  32.     </configuration>  
  33.     <executions>  
  34.         <execution>  
  35.             <id>verify-deployer</id>  
  36.             <phase>install</phase>  
  37.             <goals>  
  38.                 <goal>deployer-redeploy</goal>  
  39.             </goals>  
  40.         </execution>  
  41.         <execution>  
  42.             <id>clean-deployer</id>  
  43.             <phase>clean</phase>  
  44.             <goals>  
  45.                 <goal>deployer-undeploy</goal>  
  46.             </goals>  
  47.         </execution>    
  48.     </executions>    
  49. </plugin>  


在tomcat7的conf/tomcat-users.xml中增長角色和用戶, 否則會報403,無法訪問

[html]  view plain copy print ? 在CODE上查看代碼片 派生到個人代碼片
  1. <role rolename="manager-gui"/>  
  2. <role rolename="manager-script"/>  
  3. <role rolename="manager-jmx"/>    
  4. <role rolename="manager-status"/>  
  5. <role rolename="admin-gui"/>  
  6. <user username="admin" password="password" roles="admin-gui,manager-gui,manager-script,manager-status"/>  

另外還有一點要注意的是url,tomcat7是
[html]  view plain copy print ? 在CODE上查看代碼片 派生到個人代碼片
  1. http://localhost:8080/manager/text  

而tomcat6是
[html]  view plain copy print ? 在CODE上查看代碼片 派生到個人代碼片
  1. http://localhost:8080/manager/html  

配置好以後就能夠運行mvn cargo:redeploy 來部署應用了(必須保證tomcat是running狀態,不然無法部署),若是容器中已經部署的當前應用,Cargo會先卸載掉原來的應用,而後再從新部署。

爲何在配置了

[html]  view plain copy print ? 在CODE上查看代碼片 派生到個人代碼片
  1. <goal>deployer-undeploy</goal>  

的時候,明明剛剛install的時候已經生成了,可是卻每次clean都報找不到target目錄下war包呢?


3.Cargo插件中各個命令的之間的異同

Goals

Description

cargo:start

Start a container. That goal will:

    • If the plugin configuration requires so, installs the container.
    • If the plugin configuration defines a container with a standalone local configuration, it will create the configuration.
    • If the plugin configuration contains one or more deployables, it will deploy these to the container automatically.
    • If the plugin configuration contains no deployables but the project's packaging is Java EE (WAR, EAR, etc.), it will deploy the project's deployable to to the container automatically.
    • And, of course, start the container.

Note: A container that's started with cargo:start will automatically shut down as soon as the parent Maven instance quits (i.e., you see a BUILD SUCCESSFUL or BUILD FAILED message). If you want to start a container and perform manual testing, see our next goal cargo:run.

cargo:run

Start a container and wait for the user to press CTRL + C to stop. That goal will:

    • If the plugin configuration requires so, installs the container.
    • If the plugin configuration defines a container with a standalone local configuration, it will create the configuration.
    • If the plugin configuration contains one or more deployables, it will deploy these to the container automatically.
    • If the plugin configuration contains no deployables but the project's packaging is Java EE (WAR, EAR, etc.), it will deploy the project's deployable to to the container automatically.
    • And, of course, start the container and wait for the user to press CTRL + C to stop.

cargo:stop

Stop a container.

cargo:restart

Stop and start again a container. If the container was not running before calling cargo:restart, it will simply be started.

cargo:configure

Create the configuration for a local container, without starting it. Note that the cargo:start and cargo:run goals will also install the container automatically (but will not call cargo:install).

cargo:package

Package the local container.

cargo:daemon-start

Start a container via the daemon. Read more on: Cargo Daemon

Note: The daemon:start goal is actually equivalent to a restart in CARGO's terms; in the case a container with the same cargo.daemon.handleid already exists then it will be stopped first before your container is started. This also implies that in the case the new container fails to start, the old one will not be restarted.

cargo:daemon-stop Stop a container via the daemon. Read more on: Cargo Daemon

cargo:deployer-deploy (aliased to cargo:deploy)

Deploy a deployable to a running container.

Note: The cargo:start and cargo:run do already deploy the deployables specified in the configuration to the container; as a result calling cargo:deploy for a container which has been started by CARGO in the same Maven2/Maven3 project will most likely cause a second deployment of the same deployables (and might even fail).

cargo:deployer-undeploy(aliased to cargo:undeploy)

Undeploy a deployable from a running container.

cargo:deployer-start

Start a deployable already installed in a running container.

cargo:deployer-stop

Stop a deployed deployable without undeploying it.

cargo:deployer-redeploy(aliased to cargo:redeploy)

Undeploy and deploy again a deployable. If the deployable was not deployed before calling cargo:deployer-redeploy (or its alias cargo:redeploy) it will simply be deployed.

cargo:uberwar

Merge several WAR files into one.

cargo:install

Installs a container distribution on the file system. Note that the cargo:start goal will also install the container automatically (but will not call cargo:install).

cargo:help

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

相關文章
相關標籤/搜索