Jenkins-部署java代码项目
Posted 一颗豆子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jenkins-部署java代码项目相关的知识,希望对你有一定的参考价值。
实验环境:
Jenkins:192.168.1.12
tomcat:192.168.1.7
一、新建远程代码Java项目仓库
说明:这边测试是在coding上注册账户,建立远程仓库,coding地址:https://coding.net/login,也可以使用gitlub和github作为远程仓库,自由选择。
1:新建test-java项目
2:在Jenkins机器上生成秘钥对
[root@Jenkins ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
a6:08:32:2e:1d:a6:85:bb:7e:64:dd:92:f7:60:b2:67 root@ghs
The key’s randomart image is:
+–[ RSA 2048]—-+
| |
| |
| |
| . |
|+ = . o S |
|.O = * B |
|+.+ . B o |
|.. . . E . |
|o.. o |
3:将id_rsa.pub公钥内容复制到远程仓库上
[root@jenkins .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDy68qc7DtK09hWzrEv0jJXI28sluvpOK8oGR7Dhz2JDvbAxZ2jalipalIAxt8Hg3GbqYrzJIi6MTEYMbCKA7/XJ07dDfGvvwEnEyZb9fp1xgVzFCe5Vx9K5Qll/FpP0XtLK69QGMjrA1dsG1DRPyGqAySlYLo66TziSTG/RYI6NEPf40i7qaBQXobTNXj9Rv0b4fkJXlWHMZ+4zvo37UhhsgRt2SKJRupoKJpbONf3+H1u35KSsu5D5Qd4u7RCRJW2JGj9zyJt5VUiCUzQkyYiHRwarETC0HHGaqgFQltUJBC4SGkYVmYp3PDKZfhYNBE+JFvSJxkdz3Gyj63O9Nqz root@host1
4:Jenkins机器上创建java项目目录,并操作初始化和克隆远程仓库
[root@jenkins ~]# mkdir /home/java
[root@jenkins test-java]# git clone git@e.coding.net:ghs-test/test-java.git
正克隆到 \'test-java\'...
warning: 您似乎克隆了一个空版本库。
[root@jenkins test-java]# cd test-java
[root@jenkins test-java]# echo "# test-java" >> README.md
[root@jenkins test-java]# git add README.md
[root@jenkins test-java]# git commit -m "first commit"
[master(根提交) c286ffe] first commit
1 file changed, 1 insertion(+)
create mode 100644 README.md
[root@jenkins test-java]# git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 213 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@e.coding.net:ghs-test/test-java.git
* [new branch] master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。
二、下载zrlog的Java包
1:Jenkins上下载zrlog
[root@jenkins test_java]# wget https://codeload.github.com/94fzb/zrlog/zip/master
[root@jenkins test_java]# ls
master README.md
2:unzip解压
[root@jenkins test_java]# unzip master
3:提交到远程仓库
[root@jenkins test-java]# git add .
[root@jenkins test-java]# git commit -m "add zrlog"
[root@jenkins test-java]# git push -u origin master
Counting objects: 606, done.
Compressing objects: 100% (554/554), done.
Writing objects: 100% (605/605), 1.89 MiB | 0 bytes/s, done.
Total 605 (delta 80), reused 0 (delta 0)
remote: Resolving deltas: 100% (80/80), done.
To git@e.coding.net:ghs-test/test-java.git
c286ffe..84a7318 master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。
4:远程仓库上查看是否存在zrlog
三、安装maven编译打包
maven官网下载地址:https://maven.apache.org/download.cgi
1:下载maven二进制包
[root@jenkins src]# wget https://www-eu.apache.org/dist/maven/maven-3/3.6.2/source/apache-maven-3.6.2-bin.tar.gz
2:解压
[root@jenkins src]# tar zxvf apache-maven-3.6.2-bin.tar.gz
3:移动到/usr/local目录下
[root@jenkins src]# mv apache-maven-3.6.2 /usr/local/maven
4:执行mvn -v,检查maven是否配置成功
[root@jenkins src]# /usr/local/maven/bin/mvn -v
Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-27T23:06:16+08:00)
Maven home: /usr/local/maven
Java version: 1.8.0_222, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b10-1.el7_7.x86_64/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.27.2.el7.x86_64", arch: "amd64", family: "unix"
四、配置tomcat
1:设置tomcat管理界面用户和密码,用户admin,密码abc-123。
[root@tomcat ~]# vim /usr/local/tomcat/conf/tomcat-users.xml
加入以下内容
<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="abc-123" roles="admin,manager,admin-gui,admin-script,manager-gui,manager-script,manager-jmx,manager-status" />
2:开放管理界面访问IP限制,让Jenkins可以访问管理界面并部署
[root@tomcat src]# vim /usr/local/tomcat/webapps/manager/META-INF/context.xml
修改为以下内容
allow="127\\.\\d+\\.\\d+\\.\\d+|::1|0:0:0:0:0:0:0:1|192.168.1.*" />
五、配置Jenkins
1:安装完成后,到Jenkins web页面配置maven
2:设置jdk路径,这边是存在在/usr/local/目录下
3:构建Maven项目前,需先安装Maven Integration 和Deploy to container这两个插件
六、构建job任务,实现部署Java代码项目
1:构建test_java项目job
2:配置git,username:git用户、Private key选择Enter dirctly,私钥和你Git服务器中存放的公钥是配对的。
3:设置POM,Root POM为pom.xml, Goals and options 填写 clean install -D maven.test.skip=true
Post Steps、构建设置保持默认。
4:构建后操作,点击“增加构建后操作步骤—选择Deploy war/ear to a container选项
5:WAR/EAR files”填写“**/*.war”,Contex path留空,Containers选择需要发布对应的tomcat,这边是9.0版本,Cretentials配置tomcat的管理界面用户名和密码
6:Tomcat URL填写要把war包发布到的那台机器的url,如http://192.168.1.13
7:保存之后,返回项目首页,点击执行biud now开始部署,即可发布到远程机器上
以上是关于Jenkins-部署java代码项目的主要内容,如果未能解决你的问题,请参考以下文章
jenkins+tomcat+ant+java+linux 自动部署项目