1、先要选取一个基板docker镜像,此处选用tomcat:8-jre8

2、通过基板镜像,build一个自有镜像:


Dockerfile:

FROM tomcat:8-jre8

MAINTAINER "Liu Li <lich@lichdiamond.win>" 

ADD settings.xml /usr/local/tomcat/conf/
ADD tomcat-users.xml /usr/local/tomcat/conf/

settings.xml:

<?xml version="1.0" encoding="UTF-8"?>
<settings>
<servers> 
<server>
<id>TomcatServer</id>
<username>admin</username>
<password>adm123</password>
</server> 
</servers>

tomcat-users.xml:

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="manager-gui"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="admin" password="adm123" roles="manager,manager-gui,manager-script" />
</tomcat-users>

这三个文件放在同一个文件夹里,然后在那个文件夹里执行:


 docker build -t my_tomcat:8-jre8 .

3、通过设定的自有镜像启动一个容器:


 docker run -d -p 8080:8080  my_tomcat:8-jre8

访问ip:8080可以获得tomcat的欢迎页

ip/manager可以获得tomcat的管理页面:登录用户名密码在上面文件中配置


4、maven管理的工程中的pom文件中:


注:如果pom中带s的复数标签已有,只需要将内容添加到标签下即可:

比如:

文件中已经存在plugins标签,往其中添加:


 <plugins>  
            <plugin>
                <groupId>xxxxxxxxxxxxxxxxxx</groupId>  
                <artifactId>xxxxxxxxxxxxxxxxxx</artifactId>  
                <version>xxxxxxxxxxx</version>  
            </plugin>  
         .
         .
         .
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <version>3.1</version>  
            </plugin>  
            <plugin>  
                <groupId>org.apache.tomcat.maven</groupId>  
                <artifactId>tomcat7-maven-plugin</artifactId>  
                <version>2.2</version>  
                <configuration>
                    <port>8080</port>   
                    <uriEncoding>UTF-8</uriEncoding>
                    <url>http://localhost:8080/manager/text</url>  
                    <username>admin</username>  
                    <password>adm123</password>  
                    <path>/${project.artifactId}</path>  
                </configuration>  
            </plugin>  
</plugins>


文件中存在properties标签时:


<properties>  
        .
        .
        .
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <project.deploy>deploy</project.deploy>  
        <project.tomcat.version>8.0.0-RC5</project.tomcat.version>  
</properties>



文件中存在dependencies标签:


 <dependencies>
         <dependency>  
            <groupId>xxxxxxxxxx</groupId>  
            <artifactId>xxxxxxxxxxxx</artifactId>  
            <version>xxxxxxxxxxxxx</version>  
            <scope>xxxxxxxxxxxx</scope>  
        </dependency> 
        .
        .
        .
        <dependency>  
            <groupId>org.apache.tomcat</groupId>  
            <artifactId>tomcat-servlet-api</artifactId>  
            <version>${project.tomcat.version}</version>  
            <scope>provided</scope>  
        </dependency> 
  </dependencies>



pom.xml:

localhost改成你部署的主机的ip

<properties>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <project.deploy>deploy</project.deploy>  
        <project.tomcat.version>8.0.0-RC5</project.tomcat.version>  
</properties>  
 <dependencies>
        <dependency>  
            <groupId>org.apache.tomcat</groupId>  
            <artifactId>tomcat-servlet-api</artifactId>  
            <version>${project.tomcat.version}</version>  
            <scope>provided</scope>  
        </dependency> 
  </dependencies>  
  <build>  
        <plugins>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <version>3.1</version>  
            </plugin>  
            <plugin>  
                <groupId>org.apache.tomcat.maven</groupId>  
                <artifactId>tomcat7-maven-plugin</artifactId>  
                <version>2.2</version>  
                <configuration>
                    <port>8080</port>   
                    <uriEncoding>UTF-8</uriEncoding>
                    <url>http://localhost:8080/manager/text</url>  
                    <username>admin</username>  
                    <password>adm123</password>  
                    <path>/${project.artifactId}</path>  
                </configuration>  
            </plugin>  
        </plugins>  
    </build>



5、完成之后,在项目工程文件夹中执行:


含义:构建并将工程部署到tomcat中,跳过单元测试。


mvn -Dmaven.test.skip=true tomcat7:deploy -e

结果如:


[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.drpeng:busimgnt:war:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-deploy-plugin is missing. @ com.drpeng:busimgnt:[unknown-version], /root/work/OVERSEABOSS/modelBusisrv/busimgnt/pom.xml, line 97, column 21
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-install-plugin is missing. @ com.drpeng:busimgnt:[unknown-version], /root/work/OVERSEABOSS/modelBusisrv/busimgnt/pom.xml, line 77, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building busimgnt Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> tomcat7-maven-plugin:2.2:deploy (default-cli) > package @ busimgnt >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ busimgnt ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 44 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ busimgnt ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 186 source files to /root/work/OVERSEABOSS/modelBusisrv/busimgnt/target/classes
[WARNING] /root/work/OVERSEABOSS/modelBusisrv/busimgnt/src/main/java/com/drpeng/busimgnt/api/SaleDomyBoxFileInfoResource.java:[9,42] warning: com.sun.org.apache.regexp.internal.RE is Sun proprietary API and may be removed in a future release
[WARNING] Note: Some input files use unchecked or unsafe operations.
[WARNING] Note: Recompile with -Xlint:unchecked for details.
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ busimgnt ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ busimgnt ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ busimgnt ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-war-plugin:2.2:war (default-war) @ busimgnt ---
[INFO] Packaging webapp
[INFO] Assembling webapp [busimgnt] in [/root/work/OVERSEABOSS/modelBusisrv/busimgnt/target/busimgnt]
[INFO] Processing war project
[INFO] Copying webapp resources [/root/work/OVERSEABOSS/modelBusisrv/busimgnt/src/main/webapp]
[INFO] Webapp assembled in [324 msecs]
[INFO] Building war: /root/work/OVERSEABOSS/modelBusisrv/busimgnt/target/busimgnt.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] 
[INFO] --- maven-jar-plugin:2.3:jar (make-a-jar) @ busimgnt ---
[INFO] Building jar: /root/work/OVERSEABOSS/modelBusisrv/busimgnt/target/busimgnt-1.0-SNAPSHOT.jar
[INFO] 
[INFO] <<< tomcat7-maven-plugin:2.2:deploy (default-cli) < package @ busimgnt <<<
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:deploy (default-cli) @ busimgnt ---
[INFO] Deploying war to http://localhost:8080/busimgnt  
Uploading: http://localhost:8080/manager/text/deploy?path=%2Fbusimgnt
Uploaded: http://localhost:8080/manager/text/deploy?path=%2Fbusimgnt (18430 KB at 61229.1 KB/sec)
[INFO] tomcatManager status code:200, ReasonPhrase:OK
[INFO] OK - Deployed application at context path /busimgnt
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.720 s
[INFO] Finished at: 2016-11-16T10:10:31+08:00
[INFO] Final Memory: 30M/430M
[INFO] ------------------------------------------------------------------------



我的工程名字叫busimgnt,此时访问:

我的ip:8080/busimgnt,我的index中只写了hello world,因此访问后得到了该字符串,这时候,热部署就完成了,下次打包之后,执行本命令,就可以将新包热部署。