This article introduces project compilation involving Maven. Maven is a project management and comprehensive tool that provides developers with a complete life cycle framework. The development team can automatically complete the infrastructure construction of the project. Maven uses a standard directory structure. and default build lifecycle. It is an open source project of Apache, mainly serving the construction of JAVA platform, dependency management, and project management.
Article directory
Maven installation and deployment
1. Download the Maven 3 installation package from the official website or mirror source. Official
website: http://maven.apache.org/download.cgi
Tsinghua mirror: https://mirrors.tuna.tsinghua.edu.cn/apache/maven/
2. Install
the download link for the following version of Maven at the end of the article.
[root@Jenkins ~]# tar xf apache-maven-3.3.9-bin.tar.gz
[root@Jenkins ~]# mv apache-maven-3.3.9 /usr/local/
[root@Jenkins ~]# ln -s /usr/local/apache-maven-3.3.9/ /usr/local/maven
[root@Jenkins ~]# /usr/local/maven/bin/mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /usr/local/maven
Java version: 1.8.0_181, vendor: Oracle Corporation
Java home: /usr/java/jdk1.8.0_181-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.el7.x86_64", arch: "amd64", family: "unix"
3. Edit the /etc/profile file and add the PATH variable at the end
[root@Jenkins ~]# cat /etc/profile
...
export PATH=/usr/local/apache-maven-3.3.9/bin/:$PATH
[root@Jenkins ~]# source /etc/profile
[root@Jenkins ~]# mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /usr/local/apache-maven-3.3.9
Java version: 1.8.0_181, vendor: Oracle Corporation
Java home: /usr/java/jdk1.8.0_181-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.el7.x86_64", arch: "amd64", family: "unix"
Maven common commands
mvn -v 查看版本号
validate(验证): 验证项目正确,并且所有必要信息可用。
compile(编译): 编译项目源码
test(测试): 使用合适的单元测试框架测试编译后的源码。
package(打包): 源码编译之后,使用合适的格式(例如JAR格式)对编译后的源码进行打包。
integration-test(集成测试): 如果有需要,把包处理并部署到可以运行集成测试的环境中去。
verify(验证): 进行各种测试来验证包是否有效并且符合质量标准。
install(安装): 把包安装到本地仓库,使该包可以作为其他本地项目的依赖。
deploy(部署): 在集成或发布环境中完成,将最终软件包复制到远程存储库,以与其他开发人员和项目共享。
mvn clean (清除) : 清除上次编译的结果
mvn package -Dmaven.test.skip=true 跳过测试用例
mvn package #会去maven的中央仓库去下载需要的依赖包和插件到.m2目录下
Jenkins integrates Maven
Requirements: Upload a simple Java project package hello-world.tar.gz to decompress. After decompression, push the code to the gitlab remote warehouse, trigger the Jenkins event through the trigger, Jenkins pulls the code to the Jenkins server, and uses Maven to execute the packaging command. . (The project package download link is at the end of the article)
1. Create a Jenkins project
1. Upload the code to gitlab
Project Object Model is a project object model, a pom.xml file saved in xml format. This file is used to manage source code, configuration files, developer information and roles, issue tracking system, organizational information, project authorization, project URL, Project dependencies, etc., this file is maintained by development, and operation and maintenance personnel do not need to care.
[root@Gitlab ~]# rz -E
rz waiting to receive.
[root@Gitlab ~]# tar xf hello-world-war.tar.gz
[root@Gitlab ~]# cd hello-world-war/
[root@Gitlab hello-world-war]# ls
dist pom.xml README.md src
[root@Gitlab hello-world-war]# git remote # 查看与远程仓库的连接
origin
[root@Gitlab hello-world-war]# git remote remove origin # 删除默认连接
[root@Gitlab hello-world-war]# git remote
[root@Gitlab hello-world-war]#
2. We create a new project in gitlab and submit the code here
3. Add the warehouse information connected to this project and push the code to the remote warehouse
[root@Gitlab hello-world-war]# git remote add origin [email protected]:root/java.git
[root@Gitlab hello-world-war]# git remote -v
origin [email protected]:root/java.git (fetch)
origin [email protected]:root/java.git (push)
[root@Gitlab hello-world-war]# git push -u origin master
Counting objects: 37, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (24/24), done.
Writing objects: 100% (37/37), 4.48 KiB | 0 bytes/s, done.
Total 37 (delta 8), reused 0 (delta 0)
To [email protected]:root/java.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
4. Refresh the gitlab web interface, display the project code file, and copy the project address again
5. Create a new maven project in Jenkins to pull the gitlab code
Compared with free-style projects, this project has one more compilation process.
Make some configurations for the project
Add source code source
You can see an error message at the build, showing some things that need to be configured with maven. Click on the blue position and right-click to open a new tab.
Scroll down on the new tab page to add the Maven home directory
At this time, our build department still displays an error because it is not refreshed. It doesn't matter. We fill in the contents of Goals and options, and clean package first clears and then packages.
If you exit and enter again, the error will not be displayed.
2. Manual run-through project
First run through the project deployment manually, manually upload the project on Jenkins, package the project through Maven, and deploy the code to the web server.
1. Manually upload the project to the Jenkins server
[root@Jenkins ~]# cd hello-world-war/
[root@Jenkins hello-world-war]# ls
dist pom.xml README.md src
[root@Jenkins hello-world-war]# mvn package # 打包项目
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:41 min
[INFO] Finished at: 2023-08-20T18:18:48+08:00
[INFO] Final Memory: 14M/261M
[INFO] ------------------------------------------------------------------------
2. Push the packaged war package to the specified location on the web
. After successfully packaging the project, it is found that there is an additional target directory, and hello-world-war-1.0.0.war is under the directory (if clean is executed, the target directory will be cleared)
[root@Jenkins hello-world-war]# ls
dist pom.xml README.md src target
[root@Jenkins hello-world-war]# ls target/
hello-world-war-1.0.0 maven-archiver
hello-world-war-1.0.0.war
3. Push the war package to the tomcat code directory of the web server
[root@Web01 ~]# ls /soft/tomcat/webapps/ROOT/
asf-logo-wide.svg bg-nav.png index.jsp tomcat.svg
bg-button.png bg-upper.png RELEASE-NOTES.txt WEB-INF
bg-middle.png favicon.ico tomcat.css
[root@Web01 ~]# rm -rf /soft/tomcat/webapps/ROOT/*
[root@Jenkins hello-world-war]# scp target/hello-world-war-1.0.0.war 10.0.0.7:/soft/tomcat/webapps/ROOT/
hello-world-war-1.0.0.war 100% 2402 1.3MB/s 00:00
[root@Web01 ~]# ls /soft/tomcat/webapps/ROOT/
hello-world-war-1.0.0.war
[root@Web01 ~]# cd /soft/tomcat/webapps/ROOT/
[root@Web01 ROOT]# unzip hello-world-war-1.0.0.war
Archive: hello-world-war-1.0.0.war
creating: META-INF/
inflating: META-INF/MANIFEST.MF
creating: WEB-INF/
creating: WEB-INF/classes/
inflating: WEB-INF/web.xml
inflating: index.jsp
creating: META-INF/maven/
creating: META-INF/maven/com.efsavage/
creating: META-INF/maven/com.efsavage/hello-world-war/
inflating: META-INF/maven/com.efsavage/hello-world-war/pom.xml
inflating: META-INF/maven/com.efsavage/hello-world-war/pom.properties
[root@Web01 ROOT]# ls
hello-world-war-1.0.0.war index.jsp META-INF WEB-INF
4. The browser accesses 10.0.0.7:8080 and the interface is successfully displayed.
3. Write the build steps
Write the build steps to submit the code to gitlab and automatically trigger the Jenkins event. After Jenkins is packaged through Maven, the code is deployed to the web server.
We created the Jenkins project above and haven't written the build steps yet. We just wrote clean and package. At this time, when we click Build Now, the target directory will be generated. In other words, the code is pulled from gitlab and then executed on Jenkins. maven compilation process.
[root@Jenkins ~]# ls /var/lib/jenkins/workspace/
freestyle-job git_version java-job
[root@Jenkins ~]# ls /var/lib/jenkins/workspace/java-job/
dist pom.xml README.md src target
But this is not enough. What we want to achieve when building the project is to automatically deploy to the web server, so we add a build step.
1. Add construction steps
Here you can choose to write the code directly in it, or you can choose to put the code in the script and just write an execution script here.
ssh 10.0.0.7 "cd /soft/apache-tomcat-9.0.73/webapps/ && mkdir java_$BUILD_ID"
scp target/*.war 10.0.0.7:/soft/apache-tomcat-9.0.73/webapps/java_$BUILD_ID
ssh 10.0.0.7 "cd /soft/apache-tomcat-9.0.73/webapps/java_$BUILD_ID && unzip *.war && rm -rf *.war"
ssh 10.0.0.7 "cd /soft/apache-tomcat-9.0.73/webapps/ && rm -rf ROOT && ln -s java_$BUILD_ID ROOT"
ssh 10.0.0.7 "/soft/tomcat/bin/shutdown.sh && /soft/tomcat/bin/startup.sh"
2. Click Build Now
[root@Web01 webapps]# ll
total 4
drwxr-x--- 16 root root 4096 Aug 20 19:18 docs
drwxr-x--- 7 root root 99 Aug 20 19:18 examples
drwxr-x--- 6 root root 79 Aug 20 19:18 host-manager
drwxr-xr-x 2 root root 39 Aug 20 19:34 java_2
drwxr-xr-x 4 root root 54 Aug 20 19:37 java_3
drwxr-x--- 6 root root 114 Aug 20 19:18 manager
lrwxrwxrwx 1 root root 6 Aug 20 19:37 ROOT -> java_3
[root@Web01 webapps]# ls ROOT/
index.jsp META-INF WEB-INF
Browser access can also be successfully accessed. At this time, we note that it is v4.0
3. Configure build triggers
Copy hook address
Check the configuration and fill in the branch
Generate token
Copy the URL of the hook and the generated token to the integration page under the settings of the gitlab project, and click Add Web Hook
4. Modify the code in gitlab and submit it to Jenkins for testing
[root@Gitlab hello-world-war]# cat src/main/webapp/index.jsp
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World! v5.0</h1>
<p>
It is now
<%= new java.util.Date() %></p>
<p>
You are coming from
<%= request.getRemoteAddr() %></p>
</body>
[root@Gitlab hello-world-war]# git commit -am "v2.0"
[master 8541df7] v2.0
1 file changed, 1 insertion(+), 1 deletion(-)
[root@Gitlab hello-world-war]# git push -u origin master
Counting objects: 11, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 433 bytes | 0 bytes/s, done.
Total 6 (delta 2), reused 0 (delta 0)
To [email protected]:root/java.git
b0b92ed..8541df7 master -> master
Branch master set up to track remote branch master from origin.
The Jenkins event is automatically triggered, and Jenkins completes the operation of using Maven to compile the code and push it to the web server. At this time, we refresh the browser and find that the version has changed.
Maven installation package download link: https://pan.baidu.com/s/1HPUaEHzaqLV4fk9VvySAew?pwd=im0w
Java project package download link: https://pan.baidu.com/s/1Poll1N7R1O2jz7_RRkAjXg?pwd=wja5I am
koten, 10 years of operation and maintenance experience, continue to share operation and maintenance tips, thank you for reading and paying attention!