八、部署java项目-创建私有仓库
java的项目是需要编译和打包的;编译和打包用maven完成,所以需安装maven
本次实验,我们采用git私有仓库的形式,涉及到配置密钥
准备工作:
到https://github.com/注册一个账号,创建一个仓库;点击+号,New repository。
设置密钥,点击头像的倒三角,选择setting
在Linux主机上生成ssh公钥:
[root@server ~]# ssh-keygen
[root@server ~]# cat .ssh/id_rsa.pub
把公钥的内容添加到:
添加成功如下:
将仓库克隆到/home目录;初始化并创建测试文件并推送
[root@server ~]# cd /home
[root@server home]# git clone [email protected]:xou6363/test_java
正克隆到 'test_java'...
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
RSA key fingerprint is MD5:16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes //注意输入YES,只输Y老提示联系不上GIT仓库;
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
warning: 您似乎克隆了一个空版本库。
初始化并创建测试文件并推送:
[root@server test_java]# echo 123456 > README.md
[root@server test_java]# git add README.md
[root@server test_java]# git commit -m "first commit"
[master(根提交) 87e2567] first commit
1 file changed, 1 insertion(+)
create mode 100644 README.md
[root@server test_java]# git remote add origin https://github.com/xou6363/test_java.git
fatal: 远程 origin 已经存在。
[root@server test_java]# git push -u origin master
Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
Counting objects: 3, done.
Writing objects: 100% (3/3), 211 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:xou6363/test_java
* [new branch] master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。
九、部署java项目-下载zrlog源码
下载zrlog的源码,并解压:
[root@server home]# wget https://codeload.github.com/94fzb/zrlog/zip/master
[root@server home]# unzip master
[root@server home]# ls
master test_java zrlog-master
把解压后的文件都移到git仓库里,并提交到远程仓库上:
[root@server home]# mv zrlog-master/* test_java/
mv: overwrite ‘test_java/README.md’? r
[root@server home]# cd test_java/
[root@server test_java]# git add .
[root@server test_java]# git commit -m "add zrlog"
[root@server test_java]# git push
Writing objects: 100% (592/592), 1.85 MiB | 839.00 KiB/s, done.
Total 592 (delta 77), reused 0 (delta 0)
remote: Resolving deltas: 100% (77/77), done.
To [email protected]:xou6363/test_java
87e2567..5e7debd master -> master
十、安装tomcat
安装JDK:
yum install -y java-1.8.0-openjdk
[root@client ~]# java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
安装tomcat,下载解压:
[root@client ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.14/bin/apache-tomcat-9.0.14.tar.gz
[root@client ~]#tar zxfv apache-tomcat-9.0.14.tar.gz
[root@client ~]#mv apache-tomcat-9.0.14 /usr/local/tomcat
[root@client ~]#cd /usr/local/
配置环境变量:
[root@client ~]#vim /etc/profile.d/tomcat.sh
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
[root@client local]# chmod +x /etc/profile.d/tomcat.sh
[root@client local]# source /etc/profile.d/tomcat.sh
启动tomcat:
[root@client ~]startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/lib/jvm/java-openjdk/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
Tomcat返回Using JRE_HOME: /usr错误:
编辑 Tomcat根目录/bin/setclasspath.sh ,在文件的开头添加以下
export JAVA_HOME=/usr/lib/jvm/java-openjdk
export JRE_HOME=/usr/lib/jvm/java-openjdk/jr
Tomcat安装成功后,配置tomcat管理用户
修改配置文件:conf/tomcat-users.xml,添加相关用户;在文件末尾加入以下内容
[root@client ~]# vim /usr/local/tomcat/conf/tomcat-users.xml
# 注意以上内容是写在tomcat-users标签内的
配置Tomcat的Context文件,只需要配置白名单ip即可,不然默认只允许本地ip访问:
[root@client ~]# vim /usr/local/tomcat/webapps/manager/META-INF/context.xml
# 这里可以根据你自己的机器ip进行配置
完成以上编辑后,重启tomcat:
[root@client ~]# sh /usr/local/tomcat/bin/shutdown.sh
[root@client ~]# sh /usr/local/tomcat/bin/startup.sh
点击首页的管理页面:
在这个页面我们可以对发布的web项目进行管理,而且Jenkins也需要通过这个管理页面的接口来发布我们的Java项目,所以才需要配置这个管理页面的用户及白名单ip。
十一、部署java项目-安装maven
下载、安装maven,maven要安装在jenkins所在的机子上
下载地址:http://maven.apache.org/download.cgi
下载并解压到/usr/local/目录中
[root@server ~]# cd /usr/local/
[root@server local]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz
[root@server local]# tar xf apache-maven-3.5.4-bin.tar.gz
[root@server local]# ls
apache-maven-3.5.4 apache-maven-3.5.4-bin.tar.gz bin etc games include lib lib64 libexec sbin share src
配置环境变量:
[root@server ~]# vim /etc/profile.d/maven.sh
export MAVEN_PATH=/usr/local/apache-maven-3.5.4
export PATH=$MAVEN_PATH/bin:$PATH
[root@server ~]# chmod +x /etc/profile.d/maven.sh
[root@server ~]# source /etc/profile.d/maven.sh
查看版本信息:
[root@server ~]# mvn --version
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T02:33:14+08:00)
Maven home: /usr/local/apache-maven-3.5.4
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-693.el7.x86_64", arch: "amd64", family: "unix"
jenkins安装maven插件
配置:打开jenkins页面;点击:系统管理–>全局工具配置
安装插件:系统管理 -> 管理插件,检查是否已经安装 Maven Integration和 Deploy to container,若没有安装则需要安装这两个插件:Maven Integration是用于构建Maven项目的,而Deploy to container则是用于发布远程war包的。
安装成功后重启jenkins;如果没有安装Maven Integration的话,构建任务时是没有Maven项目的选项的;
十二、部署java项目-构建job
创建一个maven项目,名称为:test-java
创建成功后,页面跳转到:填写源码管理中的url仓库地址;如果仓库是私有的,必须配置用户密码等信息;
Build设置:Goals and options可以设置成:clean install -D maven.test.skip=true,也可以默认不设置;Root POM保持默认。
构建后操作:添加Editable Email Notification设置;设置后保存构建项目——立即构建;
构建一直卡在这个错误,查看文件是存在的,暂没找到解决方法,如有处理方法请告知,谢谢。
ERROR: No such settings file /usr/local/apche-maven-3.5.4/conf/settings.xml exists
Please verify that your alternate settings file is specified properly and exists in the workspace.