docker、maven、tomcat8自动部署配置方法
1、先要选取一个基板docker镜像,此处选用tomcat:8-jre82、通过基板镜像,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:8080my_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 结果如:
Error stacktraces are turned on.
Scanning for projects...
Some problems were encountered while building the effective model for com.drpeng:busimgnt:war:1.0-SNAPSHOT
'build.plugins.plugin.version' for org.apache.maven.plugins:maven-deploy-plugin is missing. @ com.drpeng:busimgnt:, /root/work/OVERSEABOSS/modelBusisrv/busimgnt/pom.xml, line 97, column 21
'build.plugins.plugin.version' for org.apache.maven.plugins:maven-install-plugin is missing. @ com.drpeng:busimgnt:, /root/work/OVERSEABOSS/modelBusisrv/busimgnt/pom.xml, line 77, column 21
It is highly recommended to fix these problems because they threaten the stability of your build.
For this reason, future Maven versions might no longer support building such malformed projects.
------------------------------------------------------------------------
Building busimgnt Maven Webapp 1.0-SNAPSHOT
------------------------------------------------------------------------
>>> tomcat7-maven-plugin:2.2:deploy (default-cli) > package @ busimgnt >>>
--- maven-resources-plugin:2.6:resources (default-resources) @ busimgnt ---
Using 'UTF-8' encoding to copy filtered resources.
Copying 44 resources
--- maven-compiler-plugin:3.1:compile (default-compile) @ busimgnt ---
Changes detected - recompiling the module!
Compiling 186 source files to /root/work/OVERSEABOSS/modelBusisrv/busimgnt/target/classes
/root/work/OVERSEABOSS/modelBusisrv/busimgnt/src/main/java/com/drpeng/busimgnt/api/SaleDomyBoxFileInfoResource.java: warning: com.sun.org.apache.regexp.internal.RE is Sun proprietary API and may be removed in a future release
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
--- maven-resources-plugin:2.6:testResources (default-testResources) @ busimgnt ---
Not copying test resources
--- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ busimgnt ---
Not compiling test sources
--- maven-surefire-plugin:2.12.4:test (default-test) @ busimgnt ---
Tests are skipped.
--- maven-war-plugin:2.2:war (default-war) @ busimgnt ---
Packaging webapp
Assembling webapp in
Processing war project
Copying webapp resources
Webapp assembled in
Building war: /root/work/OVERSEABOSS/modelBusisrv/busimgnt/target/busimgnt.war
WEB-INF/web.xml already added, skipping
--- maven-jar-plugin:2.3:jar (make-a-jar) @ busimgnt ---
Building jar: /root/work/OVERSEABOSS/modelBusisrv/busimgnt/target/busimgnt-1.0-SNAPSHOT.jar
<<< tomcat7-maven-plugin:2.2:deploy (default-cli) < package @ busimgnt <<<
--- tomcat7-maven-plugin:2.2:deploy (default-cli) @ busimgnt ---
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)
tomcatManager status code:200, ReasonPhrase:OK
OK - Deployed application at context path /busimgnt
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 14.720 s
Finished at: 2016-11-16T10:10:31+08:00
Final Memory: 30M/430M
------------------------------------------------------------------------
我的工程名字叫busimgnt,此时访问:
我的ip:8080/busimgnt,我的index中只写了hello world,因此访问后得到了该字符串,这时候,热部署就完成了,下次打包之后,执行本命令,就可以将新包热部署。
页:
[1]