Note content: Deploying Java projects through Jenkins
Note Date: 2018-02-28
- 26.9 Deployment Java Project-Create a private warehouse
- 26.1 deployment of the Java project-download Zrlog source code
- 26.11 Installing the configuration tomcat
- 26.12 deployment of the Java project-installing MAVEN
- 26.13 Installing plugins
- 26.14 Build Job
- 26.15 Release War Package
Deploy a Java project-Create a private warehouse
Jenkins is most often used to deploy Java projects, and Java projects have a feature that needs to be compiled and packaged, and generally compiled and packaged with MAVEN, so MAVEN needs to be installed in the system environment.
In this experiment, we use the form of a Git private repository, which involves configuring the key to Zrlog as a Java project that needs to be deployed for this experiment.
Lab Environment:
- 192.168.77.128 as an on-line server for projects with jdk1.8+tomcat8.5 environment
- 192.168.77.130 Jenkins Server
Preparatory work:
- To apply for a private git repository on a free code hosting platform, I use a free hosting platform in China: code cloud
- Generate a key pair, put the public key to the GIT server, that is, the configuration of the Repository key authentication, the general operation will not demonstrate
Clone a created private repository:
[[email protected] ~]# cd /home/[[email protected] /home]# git clone [email protected]:Zero-One/Test-Zrlog.git[[email protected] /home]# cd Test-Zrlog/[[email protected] /home/Test-Zrlog]# lsREADME.md[[email protected] /home/Test-Zrlog]# vim README.md // 随便更改一下内容[[email protected] /home/Test-Zrlog]# git add .[[email protected] /home/Test-Zrlog]# git commit -m "first"[[email protected] /home/Test-Zrlog]# git push
Push success means no problem.
Deploy Java project-download Zrlog source code
Download the Zrlog source code and unzip it:
[[email protected] /home/Test-Zrlog]# cd ../[[email protected] /home]# wget https://codeload.github.com/94fzb/zrlog/zip/master[[email protected] /home]# du -sh master 2.1M master[[email protected] /home]# unzip master
Then move the extracted files to the Git repository and submit them to the remote repository:
[[email protected] /home]# mv zrlog-master/* Test-Zrlog/mv: overwrite ‘Test-Zrlog/README.md’? y[[email protected] /home]# cd Test-Zrlog/[[email protected] /home/Test-Zrlog]# git add .[[email protected] /home/Test-Zrlog]# git commit -m "zrlog source"[[email protected] /home/Test-Zrlog]# git push
Configure Tomcat
JDK + tomcat needs to be installed on the online server, where JDK is best not to use OPENJDK, OPENJDK is prone to problems, I use the JDK version 1.8,tomcat version is 8.23.
Once the JDK+TOMCAT environment is ready and Tomcat is working properly, you will also need to configure the user files for Tomcat:
[[email protected] ~]# vim /usr/local/tomcat/conf/tomcat-users.xml # 在文件末尾加入以下内容 <role rolename="admin"/> # role配置角色 <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="your_password" roles="admin,manager,admin-gui,admin-script,manager-gui,manager-script,manager-jmx,manager-status" /> # user配置用户及用户的角色</tomcat-users> # 注意以上内容是写在tomcat-users标签内的
Then configure the tomcat context file, only need to configure the whitelist IP, or the default is to allow only local IP access:
[[email protected] ~]# vim /usr/local/tomcat/webapps/manager/META-INF/context.xml<Context antiResourceLocking="false" privileged="true" > <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192.168.77.*" /> # 这里可以根据你自己的机器ip进行配置 <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/></Context>
After completing the above edits, restart Tomcat:
[[email protected] ~]# sh /usr/local/tomcat/bin/shutdown.sh[[email protected] ~]# sh /usr/local/tomcat/bin/startup.sh
Open your browser to the Tomcat Web page and click on "Manager WebApp" to go to the admin page:
You will then go to the Administration page:
On this page we can manage the published Web project, and Jenkins needs to publish our Java project through the interface of this admin page, so we need to configure the user and whitelist IP for this administration page.
Deploy a Java project-install Maven
Because we need maven to help us compile and package The Java project, this step is to install MAVEN, note that this step is done on the Jenkins server, official website:
https://maven.apache.org/download.cgi
Download and unzip:
[[email protected] ~]# cd/usr/local/[[email protected]/usr/local]# wget http://mirror.bit.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz[[email Protected]/usr/local]# tar-zxvf apache-maven-3.5.2-bin.tar.gz [[email protected]/usr/local]# RM-RF apache-maven-3.5.2-bin.tar.gz[[email protected]/usr/local]# ls apache-maven-3.5.2/bin boot conf lib LICENSE not ICE readme.txt[[email protected]/usr/local]# apache-maven-3.5.2/bin/mvn--versionapache maven 3.5.2 ( 138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18t15:58:13+08:00) Maven Home:/usr/local/apache-maven-3.5.2java version:1.8.0_111, Vendor:oracle Corporationjava Home:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.111-1.b15.el7_2.x86_64/jredefault LOCALE:ZH_CN, Platform Encoding:utf-8os name: "Linux", Version: "3.10.0-327.el7.x86_64", Arch: "AMD64", Family: "Unix" [[email Protected]/usr/local]#
After the installation is complete, configure it on the Web page of Jenkins, the global tools configuration, System Management, Jenkins page, and so on:
Configure the path of the Maven Setting.xml file first:
Then pull down and find the MAVEN column and configure the MAVEN installation path:
After clicking on "Saven", our MAVEN is configured.
Installing plugins
System Management, manage plug-ins, check if Maven integration and Deploy to container are installed, and if not, install both plugins:
Installation Complete:
MAVEN integration is used to build a MAVEN project, while deploy to container is used to publish a remote war package.
If MAVEN integration is not installed, the task is built without the MAVEN Project option:
Restart the Jenkins service:
Systemctl Restart Jenkins
Then click on "New Task", then you will find the option of a MAVEN project, which proves that the MAVEN plugin was successfully installed:
Build Job
Click "New Task" to create a MAVEN project:
Fill in the project information:
Source management, configure a remote private git repository:
Build triggers, build environments, and Pre steps to keep the default sections,
Then configure the build column, and the build process is compiled with the source code and packaged:
The next post steps and build settings also remain the default.
Post-build operations need to be configured, this step I'm just configuring the mail notification, the purpose is to test the successful construction of the MAVEN project, the test will be successful after the release of the war package:
Once you've configured the save, you can click Build now to build this MAVEN project, which I've built successfully:
And the mailbox was successfully notified by email:
When you go to the console output page, you'll see a lot of maven-related stuff downloaded because it's the first time you've built a MAVEN project:
The second, third build of the MAVEN project will not download so many things, such as our test build MAVEN project is successful, then we can publish the war package remotely.
Publish a War Package
Back to the project configuration, after the build operation that column click "Add Build After Action" select "Deploy war/ear to a container":
Containers Select the Tomcat 8.x,tomcat URL to fill in the URL of the machine to which you want to publish the war package:
Credentials here you need to click Add on the right, adding the user name and password:
Click Save when you are finished configuring.
Then we can build the task, and here I am building the success:
And also received an email notification:
At this point to the target machine, see if the war package has been posted on tomcat:
[[email protected] ~]# ls /usr/local/tomcat/webapps/docs examples host-manager manager ROOT zrlog-1.9.0
As above, you can see that the war has been successfully posted to tomcat.
The browser also successfully logged on to the Zrlog:
So far, our Java project has been published successfully.
Deploying Java Projects through Jenkins