一、前言
Maven是一個(gè)項(xiàng)目管理和綜合工具蛉签,Maven提供給開發(fā)人員構(gòu)建一個(gè)完整的生命周期框架胡陪。許多 Apache Jakarta 項(xiàng)目發(fā)文時(shí)使用 Maven , Maven能夠很方便的幫你管理項(xiàng)目報(bào)告正蛙,生成站點(diǎn)督弓,管理JAR文件 营曼。
二乒验、在Jenkins主機(jī)上安裝Maven
1、下載軟件
cd /tmp
wget https://mirrors.bfsu.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz
2蒂阱、解壓壓縮包
tar xf apache-maven-3.3.9-bin.tar.gz -C /opt
ln -s apache-maven-3.3.9 maven
3锻全、添加命令到環(huán)境變量
echo "export PATH=$PATH:/opt/maven/bin" >>/etc/profile
source /etc/profile
4、將依賴源倉(cāng)庫(kù)地址改為阿里云地址
vim /opt/maven/conf/settings.xml
#在158行下方加入以下命令
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
5录煤、使用一個(gè)簡(jiǎn)單Java代碼測(cè)試
tar xf hello-world-war.tar.gz
cd hello-world-war/
mvn clean
mvn package
#當(dāng)有target目錄生成及成功
三鳄厌、使用Nexus私服作為依賴源倉(cāng)庫(kù)
1、下載安裝JDK
https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/rpm/centos7-x86_64/Packages/ #清華源JDK下載地址
rpm -ivh jdk-8u181-linux-x64.rpm
2妈踊、下載安裝Nexus
官網(wǎng)地址: https://www.sonatype.com/download‐oss‐sonatype
tar xf nexus-3.23.0-03-unix.tar.gz
ln -s nexus-3.23.0-03 nexus
3了嚎、啟動(dòng)Nexus
/opt/nexus/bin/nexus start
4、進(jìn)入網(wǎng)頁(yè)初始化
密碼存儲(chǔ)文件/opt/sonatype-work/nexus3/admin.password
四廊营、修改Maven依賴源地址為Nexus
1歪泳、修改Maven配置文件
vim /opt/maven/conf/settings.xml
#在132行下方加入以下命令
<server>
<id>my-nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>my-nexus-snapshot</id>
<username>admin</username>
<password>admin123</password>
</server>
#在158行下方加入以下命令
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://10.0.0.9:8081/repository/maven-public/</url>
</mirror>
#在247行下方加入以下命令
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<url>http://10.0.0.9:8081/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://10.0.0.9:8081/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
#在256行下方加入以下命令
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
2、測(cè)試是否是通過(guò)Nexus地址下載
cd /tmp/hello-world-war
mvn clean
mvn package
五露筒、Jenkins創(chuàng)建Maven項(xiàng)目
1呐伞、在GitLab上新建一個(gè)倉(cāng)庫(kù)
2、Jenkins創(chuàng)建Maven項(xiàng)目
3慎式、配置Maven項(xiàng)目
六伶氢、推送Java代碼到GitLab
git clone git@10.0.0.200:dev/java.git
cd /tmp
tar xf jeesns.tar.gz
mv jeesns/* /root/java/
git add .
git commit -m "new_code"
git push origin master
七趟径、部署一個(gè)Tomcat環(huán)境測(cè)試
軟件下載地址:https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.57/bin/apache-tomcat-8.5.57.tar.gz
1、下載安裝JDK
cd /opt
rpm -ivh jdk-8u181-linux-x64.rpm
2癣防、下載解壓壓縮包
cd /opt
tar xf apache-tomcat-8.5.57.tar.gz
ln -s apache-tomcat-8.5.57 tomcat
3蜗巧、優(yōu)化Tomcat啟動(dòng)加速
vim /usr/java/jdk1.8.0_181-amd64/jre/lib/security/java.security
修改117行為:securerandom.source=file:/dev/urandom
4、啟動(dòng)服務(wù)
/opt/tomcat/bin/startup.sh
5蕾盯、安裝MariaDB數(shù)據(jù)庫(kù)
yum -y install mariadb-server
systemctl start mariadb
systemctl enable mariadb
mysqladmin -u root password '123456'
mysql -u root -p
create database jeesns;
6惧蛹、將代碼文件的數(shù)據(jù)庫(kù)信息導(dǎo)入數(shù)據(jù)庫(kù)
[root@jenkins ~]# cd /var/lib/jenkins/workspace/Maven/jeesns-web/database/
[root@jenkins database]# scp jeesns.sql 172.16.1.7:/tmp
[root@web01 opt]# mysql -u root -pxxxxxx jeesns </tmp/jeesns.sql
八、Jenkins設(shè)置項(xiàng)目構(gòu)建命令
九刑枝、測(cè)試項(xiàng)目是否成功
十香嗓、Maven項(xiàng)目Java代碼上線腳本
vim /server/scripts/jenkins.sh
#!/bin/bash
CODEDIR=/opt/tomcat/webapps
IP=172.16.1.7
TAR_CODE(){
cd ${WORKSPACE}/target
scp *.war ${IP}:${CODEDIR}/${git_version}.war
}
JY_CODE(){
ssh ${IP} "cd ${CODEDIR} && unzip ${git_version}.war -d ${git_version}"
}
CREATE_LN(){
ssh ${IP} "cd ${CODEDIR} && rm -rf ROOT && ln -s ${git_version} ROOT"
}
RESTART(){
ssh ${IP} "/opt/tomcat/bin/shutdown.sh && /opt/tomcat/bin/startup.sh"
}
main(){
if [[ "${choice}" == "publish" ]];then
if [[ "$GIT_COMMIT" == "GIT_PREVIOUS_SUCCESSFUL_COMMIT" ]];then
echo "構(gòu)建失敗,該版本已發(fā)布"
else
TAR_CODE
JY_CODE
CREATE_LN
RESTART
fi
else
CREATE_LN
RESTART
fi
}
main