使用Cargo實現自動化部署


Cargo是一組幫助用戶操作Web容器的工具,它能幫助用戶實現自動化部署,而且它幾乎支持所有的Web容器,如Tomcat、JBoss、Jetty和Glassfish。Cargo通過cargo-maven2-plugin提供了Maven集成,Maven用戶可以使用該插件將Web項目部署到Web容器中。

Cargo支持兩種供本地部署方式,分別為standalone模式和existing模式。在standalone模式中,Cargo會從Web容器的安裝目錄復制一份配置到用戶指定的目錄,然后在次基礎上部署應用,每次重新構建的時候,這個目錄會被清空,所以配置被重新生成。在existing模式中,用戶需要指定現有的Web容器配置目錄,然后Cargo會直接使用這些配置並將應用部署到其對應的位置。

jetty-maven-plugin主要用來幫助日常的快速開發和測試

cargo-maven2-plugin主要服務於自動化部署

<plugin>
    <groupId>org.codehaus.cargo</groupId>
    <artifactId>cargo-maven2-plugin</artifactId>
    <version>1.0</version>
    <configuration>
        <container>
            <containerId>tomcat6x</containerId>  <!-- 容器的類型 -->
            <home>C://apache-tomcat-6.0.29</home>
        </container>
        <configuration>
            <type>standalone</type>
            <home>${project.build.directory}/tomcat6x</home> <!-- 構建輸出目錄 -->
        </configuration>
    </configuration>
</plugin>

cargo-maven2-plugin的groupId 是org.codehaus.cargo,這不屬於官方的兩個Maven插件groupId,因此用戶需要將其添加到settings.xml的pluginGroup元素中以方便命令調用

mvn cargo:start  //Cargo啟動Tomcat並部署應用

默認情況下,Cargo會讓Web容器堅挺8080端口,可以通過修改Cargo的cargo.servlet.port屬性來改變這一配置

<plugin>
    <groupId>org.codehaus.cargo</groupId>
    <artifactId>cargo-maven2-plugin</artifactId>
    <version>1.0</version>
    <configuration>
        <container>
            <containerId>tomcat6x</containerId>
            <home>D://apache-tomcat-6.0.29</home>
        </container>
        <configuration>    
            <type>standalone</type>  //existing 將項目部署到現有的Web容器中
            <home>${project.build.directory}/tomcat6x</home>
            <properties>
                <cargo.server.port>8081</cargo.servlet.port>
            </properties>
        </configuration>
    </configuration>
</plugin>

部署應用至遠程Web容器

<plugin>
    <groupId>org.codehaus.cargo</groupId>    
    <artifactId>cargo-maven-plugin</artifactId>
    <version>1.0</version>
    <configuration>
        <container>
            <containerId>tomcat6x</containerId>
            <type>remote</type>
        </container>
        <configuration>
            <type>runtime</type>
            <properties>
                <cargp.remote.username>username</cargp.remote.username>
                <cargp.remote.password>password<cargp.remote.password>
                <cargp.tomcat.manager.url><cargp.tomcat.manager.url>
            </properties>
        </configuration>
    </configuration>
</plugin>

 對於遠程部署的方式來說,container元素的type子元素的值必須為remote。如果不顯式指定,Cargo會使用默認值installed,並尋找對應容器安裝目錄或者安裝包。configuration中的type子元素值為runtime,表示既不使用獨立的容器配置,也不實用本地現有的容器配置,而是依賴於一個已運行的容器

 

mvn cargo:redeploy  //使用Cargo部署應用


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM