1 、概念

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

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

区分:
cargo-maven2-plugin和jetty-maven-plugin功能相似,但目的不同。
而jetty-maven-plugin主要用来帮助日常的快速开发和测试。

2、本地部署

Cargo本地部署分为 standalone 和 existing 两种模式

standalone 模式:
Cargo会从Web容器目录复制一份配置到用户指定的目录,然后在此基础上部署应用,每次重新构建,这个目录会被清空,所有配置重新生成。

existing 模式:
用户需要指定现有的Web容器配置目录, 然后Cargo会直接使用这些配置并将应用部署到对应的位置。

2.1、standalone 模式配置

<build>
	<finalName>web</finalName>
	<plugins>
		<plugin>
			<groupId>org.codehaus.cargo</groupId>
			<artifactId>cargo-maven2-plugin</artifactId>
			<version>1.2.3</version>
			<configuration>
				<container>
					<containerId>tomcat7x</containerId>
					<home>D:/develop_tools/tomcat/apache-tomcat-7.0.72-windows-x64</home>
					<type>installed</type>
					<!-- type 可选值有 installed、remote
						默认值为installed,可以省略
					-->
				</container>
				<configuration>					
					<type>standalone</type> 
					<home>${project.build.directory}/tomcat6x</home>
					
					<properties>
						<!--  更改端口号,可以省去此配置:-->
						<cargo.servlet.port>8001</cargo.servlet.port>
					</properties>  
					
				</configuration>
			</configuration>
			<executions>
				<execution>
					<id>cargo-run</id>
					<phase>install</phase>
					<goals>
						<goal>run</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

2.1、existing 模式配置

<build>
	<finalName>web</finalName>
	<plugins>
		<plugin>
			<groupId>org.codehaus.cargo</groupId>
			<artifactId>cargo-maven2-plugin</artifactId>
			<version>1.2.3</version>
			<configuration>
				<container>
					<containerId>tomcat7x</containerId>
					<home>D:/develop_tools/tomcat/apache-tomcat-7.0.72-windows-x64</home>
					<type>installed</type>
					<!-- type 可选值有 installed、remote
						默认值为installed,可以省略
					-->
				</container>
				<configuration>
					<type>existing</type>
					<home>D:/develop_tools/tomcat/apache-tomcat-7.0.72-windows-x64</home>
					
					<properties>
						<!-- 更改端口号,可以省去此配置:-->
						<cargo.servlet.port>8001</cargo.servlet.port>
					</properties>  
					
				</configuration>
			</configuration>
			<executions>
				<execution>
					<id>cargo-run</id>
					<phase>install</phase>
					<goals>
						<goal>run</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

3、部署到远程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>

cargo的远程配置:

<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"/>  
  • 不同版本的tomcat的URL地址不同
tomcat6是
http://localhost:8080/manager/html

tomcat7是 
http://localhost:8080/manager/text  

配置好之后就可以运行mvn cargo:redeploy 来部署应用了(必须保证tomcat是running状态,否则没法部署),如果容器中已经部署的当前应用,Cargo会先卸载掉原来的应用,然后再重新部署。

cargo:start于cargo:run的不同之处了吧?

cargo:start
生命周期依赖于maven实例的生命周期,也就是说,maven构建成功或者失败之后,cargo插件的生命周期也自动停止了;

cargo:run不同,不管maven是否构建成功或者失败,都必须手工去按Ctrl + C来停止。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐