1、新建一台虚拟机安装tomcat

(1)安装JDK

官网下载jdk:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
下载jdk8,放到/usr/local/src/目录下
[root@linux-node3 ~]# tar -zxvf jdk-8u151-linux-x64.tar.gz
[root@linux-node3 ~]# mv jdk1.8.0_151 /usr/local/jdk1.8
[root@linux-node3 ~]# vim /etc/profile
增加如下内容:
JAVA_HOME=/usr/local/jdk1.8/
JAVA_BIN=/usr/local/jdk1.8/bin
JRE_HOME=/usr/local/jdk1.8/jre
PATH=$PATH:/usr/local/jdk1.8/bin:/usr/local/jdk1.8/jre/bin
CLASSPATH=/usr/local/jdk1.8/jre/lib:/usr/local/jdk1.8/lib:/usr/local/jdk1.8/jre/lib/charsets.jar
[root@linux-node3 ~]# source /etc/profile
[root@linux-node3 ~]# java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

(2)安装tomcat

[root@linux-node3 ~]# wget http://apache.fayea.com/tomcat/tomcat-8/v8.5.20/bin/apache-tomcat-8.5.20.tar.gz
[root@linux-node3 ~]# tar -zxvf apache-tomcat-8.5.24.tar.gz 
[root@linux-node3 ~]# mv apache-tomcat-8.5.24 /usr/local/tomcat
[root@linux-node3 ~]# /usr/local/tomcat/bin/startup.sh 
[root@linux-node3 ~]# netstat -tulnp |grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      3765/java 

提示:三个端口8080为提供web服务的端口,8005为管理端口,8009端口为第三方服务调用的端口,比如httpd和Tomcat结合时会用到

(3)配置tomcat

[root@linux-node3 ~]# vim /usr/local/tomcat/conf/tomcat-users.xml 
<tomcat-users>
<role rolename="admin"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="manager"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user name="admin" password="admin123456" roles="admin,manager,admin-gui,admin-script,manager-gui,manager-script,manager-jmx,manager-status" />
</tomcat-users>
[root@linux-node3 ~]# /usr/local/tomcat/bin/shutdown.sh 
[root@linux-node3 ~]# /usr/local/tomcat/bin/startup.sh 

在访问tomcat时,点击"manager webapp"会跳出一个验证弹框,输入用户名密码
自动化部署之发布Java项目到tomcat
会跳转到以下界面,说明用户名密码无误,如图:
自动化部署之发布Java项目到tomcat

2、下载zrlog的java代码push到gitlab上

(1)下载zrlog的代码

[root@linux-node2 home]# wget https://codeload.github.com/94fzb/zrlog/zip/master
--2017-12-25 11:02:45--  https://codeload.github.com/94fzb/zrlog/zip/master
Resolving codeload.github.com (codeload.github.com)... 192.30.255.121, 192.30.255.120
Connecting to codeload.github.com (codeload.github.com)|192.30.255.121|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/zip]
Saving to: ‘master’
    [                                      <=>   ] 1,669,502    199KB/s   in 8.6s   
2017-12-25 11:02:56 (189 KB/s) - ‘master’ saved [1669502]
[root@linux-node2 home]# du -sh
1.7M.
[root@linux-node2 home]# du -sh master
1.6Mmaster

(2)解压

[root@linux-node2 home]# yum install -y unzip
[root@linux-node2 home]# unzip master

(3)初始化仓库,并将zrlog的代码push到gitlab

[root@linux-node2 home]# mkdir Java
[root@linux-node2 Java]# mv zrlog-master/* Java/
[root@linux-node2 Java]# git remote add orgin git@192.168.56.11:java/zrlog.git
[root@linux-node2 Java]# git add .
[root@linux-node2 Java]# git commit -m "add origin"
# On branch master
nothing to commit, working directory clean
[root@linux-node2 Java]# ll
total 48
drwxr-xr-x 2 root root 55 Dec 25 17:45 bin
-rw-r--r-- 1 root root 6114 Dec 25 17:45 CHANGELOG.md
drwxr-xr-x 2 root root 42 Dec 25 17:45 doc
-rw-r--r-- 1 root root 11358 Dec 25 17:45 LICENSE
-rwxr-xr-x 1 root root 6468 Dec 25 17:45 mvnw
-rw-r--r-- 1 root root 4994 Dec 25 17:45 mvnw.cmd
-rw-r--r-- 1 root root 7664 Dec 25 17:45 pom.xml
-rw-r--r-- 1 root root 3574 Dec 25 17:45 README.md
drwxr-xr-x 4 root root 28 Dec 25 17:45 src
[root@linux-node2 Java]# git push origin master
Counting objects: 457, done.
Compressing objects: 100% (426/426), done.
Writing objects: 100% (457/457), 2.98 MiB | 1.72 MiB/s, done.
Total 457 (delta 51), reused 0 (delta 0)
remote: Resolving deltas: 100% (51/51), done.
To git@192.168.56.11:java/zrlog.git
* [new branch] master -> master

3、新建一个maven项目zr_blog

(1)新建项目

登录jenkins,点击“新建”,任务名称自定义(zr_blog)
选择“构建一个maven项目”
点击“确定”

(2)配置项目

源码管理
选择 “git”, Repository URL填写你的git地址,并配置对应的key,大家最好事前先在jenkins这台机器上能够直接远程git操作私有仓库
构建触发器、构建环境、Pre Steps保持默认
Build
“Root POM”为pom.xml, Goals and options 填写 clean install -D maven.test.skip=true
Post Steps、构建设置保持默认
构建后操作
点击“增加构建后操作步骤”选择“Deploy war/ear to a container”,“WAR/EAR files”填写"*/.war",Contex path留空,Containers选择Tomcat 8.x,Tomcat URL填写要把war包发布到的那台机器的url,如http://192.168.56.13:8080
Credentials这里需要点击右侧的Add,添加用户名和密码
自动化部署之发布Java项目到tomcat
点击“立即构建”查看控制台信息,如图:
自动化部署之发布Java项目到tomcat

4、查看发布结果

查看linux-node3的webapps目录,会有相对应的war包,并已经进行解压,使用浏览器访问

[root@linux-node3 ~]# ll /usr/local/tomcat/webapps/
total 7160
drwxr-x--- 14 root root    4096 Dec 25 03:08 docs
drwxr-x---  6 root root    4096 Dec 25 03:08 examples
drwxr-x---  5 root root    4096 Dec 25 03:08 host-manager
drwxr-x---  6 root root    4096 Dec 25 04:05 manager
drwxr-x---  3 root root    4096 Dec 25 03:08 ROOT
drwxr-x---  9 root root    4096 Dec 25 04:04 zrlog-1.8.0
-rw-r-----  1 root root 7306944 Dec 25 04:04 zrlog-1.8.0.war

自动化部署之发布Java项目到tomcat