1.自动化部署概念
我的理解自动化部署是将一个web工程放到 servlet容器,并且同时启动servlet容器的过程。我们开始玩的web项目的时候,没少干将war包复制tomcat的webapps目录下,然后catlina run tomcat 会解压该war包形成一个web项目目录。
2.maven是怎样实现自动化部署的
maven秉着自动化构建的理念是可以帮我们完成这一步的。可以在maven web项目中的pom.xml中 定义 build标签,然后在执行mvn deploy命令就可以了。当然这过程maven 得依赖其他插件,有一个比较有名的插件cargo 就是专门从事这个工作的。
web项目的pom.xml
- cargo使用tomcat容器来自动化部署
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>MavenProject</artifactId>
<groupId>com.javxuam.maven</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>WebProject</artifactId>
<packaging>war</packaging>
<name>WebProject Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--项目的最终名字-->
<finalName>WebProject</finalName>
<plugins>
<!--声明特殊插件-->
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.6.7</version>
<!--该插件的配置-->
<configuration>
<!--声明servlet容器以及路径-->
<container>
<containerId>tomcat7x</containerId>
<home>D:\devInstall\apache-tomcat-7.0.82</home>
</container>
<!--serlet容器的配置-->
<configuration>
<!--检查一下servlet容器是否存在-->
<type>existing</type>
<home>D:\devInstall\apache-tomcat-7.0.82</home>
<!--配置 serlvet容器的使用port 默认8080不需设置-->
<properties>
<cargo.servlet.port>8080</cargo.servlet.port>
</properties>
</configuration>
</configuration>
<!-- 配置插件执行的条件-->
<executions>
<execution>
<id>cargo-run</id>
<!--maven项目生命周期的那个阶段执行-->
<phase>install</phase>
<goals>
<!--配置执行目标-->
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
在cmd窗口 执行mvn deploy(直接使用ide的mvn deploy 会有日志乱码以及serlvet容器停不了的情况)
启动日志
在浏览器中访问 localhost:8080/WebProject
至此maven 使用cargo 自动化部署到tomcat就成功。后续我们会补充 cargo 使用 was,jetty等serlvet容器的自动化配置。