使用Maven插件Cargo实现自动化部署

本文介绍Cargo工具及其Maven插件cargo-maven2-plugin的功能与用法,包括standalone和existing两种部署模式,以及如何进行远程Web容器部署。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概念

Cargo是一组帮助用户操作Web容器的工具,能够实现自动化部署,并且支持几乎所有的Web容器,如Tomcat、JBoss、Jetty和Glassfish等。

Cargo通过cargo-maven2-plugin提供了Maven集成,可以使用该插件将Web项目部署到Web容器中。

cargo-maven2-plugin和jetty-maven-plugin功能相似,但目的不同。cargo-maven2-plugin主要服务于自动化部署,而jetty-maven-plugin主要用来帮助日常的快速开发和测试。

Cargo部署模式说明

cargo支持两种本地部署的方式,分别是standalone模式和existing模式。

standalone模式部署示例

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.fengyun.webpro</groupId>
    <artifactId>webpro</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.shared</groupId>
            <artifactId>maven-filtering</artifactId>
            <version>1.0-beta-2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4.3</version>
        </dependency>
    </dependencies>

    <build>
        <defaultGoal>clean compile</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <container>
                        <containerId>tomcat6x</containerId>
                        <home>D:\devware\apache-tomcat-6.0.44</home>
                    </container>
                    <configuration>
                        <type>standalone</type>
                        <home>${project.build.directory}/tomcat6x</home>
                        <properties>
                            <cargo.servlet.port>8080</cargo.servlet.port>
                        </properties>
                    </configuration>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

1、打包 package
这里写图片描述

2、执行命令 cargo:run,首次执行会下载相应的jar包,耐心等待即可
这里写图片描述

3、成功启动,访问测试
这里写图片描述

这里写图片描述

standalone模式部署总结

cargo会从Web容器安装目录复制一份配置到用户指定的目录,然后在此基础上部署应用,每次重新构建的时候,这个目录都会被清空,所有配置被重新生成。
其实就相当于把web容器复制了一份过来,然后再把项目部署到复制过来的容器中。

示例:
1、部署后target目录内容
这里写图片描述

2、部署后tomcat6x目录内容
这里写图片描述

3、部署后webapp目录内容
这里写图片描述

existing模式部署示例

建议更多的使用existing模式,

<plugins>
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <container>
                        <containerId>tomcat6x</containerId>
                        <home>D:\devware\apache-tomcat-6.0.44</home>
                    </container>
                    <configuration>
                        <type>existing</type>
                        <home>D:\devware\apache-tomcat-6.0.44</home>
                        <properties>
                            <cargo.servlet.port>8080</cargo.servlet.port>
                        </properties>
                    </configuration>
                </configuration>
            </plugin>

        </plugins>

1、编译、打包
这里写图片描述

2、打包成功
这里写图片描述

3、部署至容器,测试,成功
这里写图片描述

这里写图片描述

这里写图片描述

部署至远程Web容器

<plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <container>
                        <containerId>tomcat6x</containerId>
                        <!-- type默认值为installed -->
                        <type>remote</type>
                    </container>
                    <configuration>
                        <type>runtime</type>
                        <!-- 不同的Web容器,有不同的属性配置,需要查询相关具体的容器配置 -->
                        <properties>
                            <cargo.remote.username>admin</cargo.remote.username>
                            <cargo.remote.password>root</cargo.remote.password>
                            <cargo.tomcat.manager.url>http://ip:prot/manager</cargo.tomcat.manager.url>
                        </properties>
                    </configuration>
                </configuration>
            </plugin>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值