Maven Web项目使用Cargo实现自动化部署

下面的案例,基于Tomcat7,maven 3.3.9,假设项目名称是WebProject

在pom.xml中配置Cargo相关信息

一、部署到本地Web容器

1、使用standalone模式

 
 
<build>
    <finalName>WebProject</finalName>
build>
    <finalName>WebProject</finalName>

    <plugins>

           <plugin>

                <groupId>org.codehaus.cargo</groupId>

                <artifactId>cargo-maven2-plugin</artifactId>

                <version>1.6.0</version>

                <configuration>

                     <container>

                          <containerId>tomcat7x</containerId>

                          <home>D:\JAVA\apache-tomcat-7.0.70</home>

                     </container>

                     <configuration>

                          <type>standalone</type>

                          <home>${project.bulid.directory}/tomcat7x</home>

                     </configuration>

                </configuration>

           </plugin>

     </plugins>

</build>

1、finalName:表示部署的项目名称,即war包名称也可以不声明;
2、在cargo-maven2-plugin中有两个配置,container(容器)、configuration(配置信息);
    1)container容器:指定Web容器的名称和路径
        【1】containerId表示容器的类型,
        【2】home表示容器的安装目录,
        【3】type可以不声明,取默认值installed;
    2)configuration:用于配置部署的模式及复制容器配置到什么位置,
	【1】type:部署模式,有三种值【standalone,existing,runtime】,runtime用于远程部署
	    <type>standalone</type>:将指定的web容器配置信息复制到指定路径,并将部署应用到指定路径;
	    <type>existing</type>:将应用部署到现有的web容器中,不需要重新复制容器的配置信息;
	    <type>runtime</type>:既不使用独立的容器配置,也不使用本地现有的容器配置,而是依赖一个已运行的容器。
	【2】home:指定路径,部署应用的路径
3、${project.bulid.directory}:表示当前项目下target目录
	【1】type:部署模式,有三种值【standalone,existing,runtime】,runtime用于远程部署
	    <type>standalone</type>:将指定的web容器配置信息复制到指定路径,并将部署应用到指定路径;
	    <type>existing</type>:将应用部署到现有的web容器中,不需要重新复制容器的配置信息;
	    <type>runtime</type>:既不使用独立的容器配置,也不使用本地现有的容器配置,而是依赖一个已运行的容器。
	【2】home:指定路径,部署应用的路径
3、${project.bulid.directory}:表示当前项目下target目录

将上述代码放入pom.xml文件保存后,执行mvn install,然后打开命令窗口,进入到项目的目录下,输入命令:mvn cargo:run,如果构建成功并启动成功,就可以访问项目路径了【http://localhost:8080/WebProject/】;

在项目上的变化,在target目录下多一个文件夹tomcat7,里面存放了从Tomcat中复制过来的配置信息和部署应用的war包,Tomcat安装目录下的webapps没有war包。

2、使用existing模式

 

<build>

     <finalName>WebProject</finalName>

     <plugins>

           <plugin>

                <groupId>org.codehaus.cargo</groupId>

                <artifactId>cargo-maven2-plugin</artifactId>

                <version>1.6.0</version>

                <configuration>

                     <container>

                          <containerId>tomcat7x</containerId>

                          <home>D:\JAVA\apache-tomcat-7.0.70</home>

                     </container>

                     <configuration>

                          <type>exsiting</type>

                          <home>D:\JAVA\apache-tomcat-7.0.70</home>

                     </configuration>

                </configuration>

           </plugin>

     </plugins>

  </build>

将上述代码放入pom.xml文件保存后,执行mvn install,然后打开命令窗口,进入到项目的目录下,输入命令:mvn cargo:run,如果构建成功并启动成功,就可以访问项目路径了【http://localhost:8080/WebProject/】;

在Tomcat安装目录下webapps中可以查看到war包信息,项目的target目录下没有多余的信息。

二、部署到远程Web容器

 

<build>

     <finalName>WebProject</finalName>

     <plugins>

           <plugin>

                <groupId>org.codehaus.cargo</groupId>

                <artifactId>cargo-maven2-plugin</artifactId>

                <version>1.6.0</version>

                <configuration>

                     <container>

                          <containerId>tomcat7x</containerId>

                          <type>remote</type>

                     </container>

                     <configuration>

                           <type>runtime</type>

                           <properties>

                                <cargo.remote.username>tomcat</cargo.remote.username>

                                <cargo.remote.password>123456</cargo.remote.password>

                                <cargo.tomcat.manager.url>http://localhost:8080/manager</cargo.tomcat.manager.url>

                           </properties>

                     </configuration>

                </configuration>

           </plugin>

     </plugins>

  </build>

上述代码:模拟访问本地Tomcat的管理地址【http://localhost:8080/manager

container中的type必须配置为remote,表示不需要安装目录或者安装包

configuration中的type需要配置为runtime,表示既不使用独立的容器配置,也不是使用本地现有的容器配置,而是依赖一个已运行的容器;properties用于声明一些热部署(热部署:能够将部署或取消部署到正在运行的容器中)相关的配置信息,比如在容器中声明容器类型是tomcat7x,需要指定tomcat的用户名、密码以及管理地址。

自动部署到远程Web容器的运行命令:mvn cargo:redeploy    

注意:执行这个命令前,需要先保证容器是运行状态(比如启动tomcat),否则命令执行失败。

猜你喜欢

转载自blog.csdn.net/cb_lcl/article/details/80960587