天天看点

使用Cargo实现自动化部署

Cargo是一组帮助用户操作web容器的工具,能够帮助用户实现自动化部署,几乎支持所有的web容器,如tomcat、jboss、glassfish等。

为了能在命令行中使用cargo,需要修改maven的settings.xml文件,修改如下所示:

<pluginGroup>org.codehaus.cargo</pluginGroup>
           

下面从本地部署和远程部署进行描述:

(1)部署至本地web容器

Cargo支持两种本地部署的方式:standalone模式和existing模式。

a)standalone模式

在该模式中,Cargo会从web容器的安装目录中复制一份配置到用户指定的目录,然后在此基础上部署应用,每次重新构建的时候,

这个目录会被清空,所有配置被重新 生成。

打开项目的pom文件,添加如下配置代码:

<span style="white-space:pre">		</span><plugin>
				<groupId>org.codehaus.cargo</groupId>
				<artifactId>cargo-maven2-plugin</artifactId>
				<version>1.4.0</version>
				<configuration>
					<container>
						<containerId>tomcat6x</containerId>
						<home>D:\Program Files\apache-tomcat-6.0.18</home>
					</container>
					<configuration>
						<type>standalone</type>
						<home>${project.build.directory}/tomcat6x</home>
						 <properties> <cargo.servlet.port>8181</cargo.servlet.port> </properties>
					</configuration>
				</configuration>
			</plugin>
           

对参数做如下说明:

containerId表示容器的类型。

home表示容器的安装目录。

configuration子元素type表示部署模式。

configuration子元素home表示复制容器配置到什么位置,其中${project.build.directory}表示target目录。

Cargo.servlet.port表示绑定的端口号,默认为8080。

打开命令提示符,执行如下命令:

mvn clean package
mvn cargo:run
           

或者

mvn clean package cargo:run
           
使用Cargo实现自动化部署

访问:http://localhost:8181/user-web2/test.jsp,显示如下:

使用Cargo实现自动化部署

b) existing模式

在该模式中,指定现有的web容器配置目录,cargo会直接使用这些配置并将应用部署到对应的位置。

打开项目的pom文件,添加如下代码:

<span style="white-space:pre">	</span><plugin>
			<groupId>org.codehaus.cargo</groupId>
			<artifactId>cargo-maven2-plugin</artifactId>
			<version>1.4.0</version>
			<configuration>
				<container>
						<containerId>tomcat6x</containerId>
						<home>D:\Program Files\apache-tomcat-6.0.18</home>
				</container>
				<configuration>
					<type>existing</type>
					<home>D:\Program Files\apache-tomcat-6.0.18</home>
				</configuration>	
			</configuration>
		</plugin>
           

打开命令提示符,输入:

mvn clean compile
mvn clean package
mvn cargo:run
           

之后cargo会自动把war包部署到D:\Program Files\apache-tomcat-6.0.18\webapps中。

(2)部署至远程web容器

Cargo可以部署应用至远程的正在运行的web容器中,前提是拥有容器的相应管理员权限。

打开项目的pom文件,添加如下代码:

<plugin>
    		<groupId>org.codehaus.cargo</groupId>
    		<artifactId>cargo-maven2-plugin</artifactId>
    		<version>1.4.0</version>
    		<configuration>
				<container>
					<containerId>tomcat6x</containerId>
					<type>remote</type>
				</container>
				<configuration>
					<type>runtime</type>
					<properties>
						<cargo.remote.uri>http://10.64.204.188:8080/manager</cargo.remote.uri>
						<cargo.remote.username>admin</cargo.remote.username>
						<cargo.remote.password>admin</cargo.remote.password>
					</properties>
				</configuration>
    		</configuration>
    	</plugin>
           

其中configuration的type子元素值runtime表示依赖于一个已运行的容器。Properties元素用来声明一些容器热部署相关配置。对于不同的容器,配置不同,详情参考:

http://cargo.codehaus.org/

启动10.64.204.188上面的tomcat(apache-tomcat-6.0.36)。

打开本地的命令提示符,依次输入:

mvn clean compile
 mvn clean package
 mvn cargo:redeploy
           

从远程tomcat的控制台中就可以看到部署的项目信息。

完。