maven打包java --jar
1.安装jdk
2.安装maven --安装非常简单 网上教程很多,此步骤忽略--
3.配置jdk/maven环境变量
# java_jdk export JAVA_HOME=/usr/java/jdk1.8.0_131 export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin export PATH=/usr/maven/bin/:$PATH # java_maven export MAVEN_HOME=/usr/maven export PATH=${PATH}:${MAVEN_HOME}/bin
4.配置maven私服地址 阿里仓库地址
添加阿里仓库地址:
Maven 仓库默认在国外, 国内使用难免很慢,我们可以更换为阿里云的仓库。
修改 maven 根目录下的 conf 文件夹中的 settings.xml 文件,在 mirrors 节点上,添加内容如下:159行左右
<mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors>
Jenkins上配置全局 私服:
需要安装Managed files插件
Manage jenkins -->Managed files -->Add a new Config -->Maven settings.xml
maven工程项目中设置
5.git拉取项目代码/并进入到项目代码目录
git clone http://192.168.16.20/batar-entrance-guard/entrance-guard.git
cd entrance-guard/entrance-guard
6.进行maven编译Java项目 jar
mvn clean install -Dmaven.test.skip=true
7.后台启动程序
nohup /usr/bin/java -jar $SERVICE_PATH/$SERVICE_NAME.jar --server.port=9921 --spring.profiles.active=dev >>/dev/null 2>&1 &
Jenkins结合shell自动部署:
构建一个maven工程任务
shell脚本案例:
case $Status in Deploy) { echo "Status:$Status" echo "Version:$BUILD_NUMBER" SERVICE_NAME="entrance-guard-manage" SERVICE_PATH="/project/batar-entrance-guard/entrance-guard" SERVICE_BACKUP="/backup/batar-entrance-guard/entrance-guard" BUILD_ID=DONTKILLME PIDS=`ps -ef |grep java | grep 9921 | awk '{print $2}'` if [ "$PIDS" != "" ];then /usr/bin/kill -9 $PIDS fi cd $SERVICE_PATH cp -rf $WORKSPACE/entrance-guard/entrance-guard-manage/target/$SERVICE_NAME.jar $SERVICE_PATH nohup /usr/bin/java -jar $SERVICE_PATH/$SERVICE_NAME.jar --server.port=9921 --spring.profiles.active=dev >>/dev/null 2>&1 & #备份 mv $WORKSPACE/entrance-guard/entrance-guard-manage/target/$SERVICE_NAME.jar $SERVICE_BACKUP/$BUILD_NUMBER.jar } ;; Rollback) { echo "Status:$Status" echo "Version:$Version" SERVICE_NAME="entrance-guard-manage" SERVICE_PATH="/project/batar-entrance-guard/entrance-guard" SERVICE_BACKUP="/backup/batar-entrance-guard/entrance-guard" BUILD_ID=DONTKILLME rm -rf $SERVICE_PATH/$SERVICE_NAME.jar cp $SERVICE_BACKUP/$Version.jar $SERVICE_PATH/$SERVICE_NAME.jar PIDS=`ps -ef |grep java | grep 9921 | awk '{print $2}'` if [ "$PIDS" != "" ];then /usr/bin/kill -9 $PIDS fi cd $SERVICE_BACKUP nohup /usr/bin/java -jar $SERVICE_PATH/$SERVICE_NAME.jar --server.port=9921 --spring.profiles.active=dev>>/dev/null 2>&1 & } ;; *) exit ;; esac