一、思想:
通过将业务包和公共包分离,集中管理所有包,打包时只构建业务包减少项目包的大小和传输时间。
为了观测稳定性,暂通过环境区分,较为频繁的联调环境采用该方式,测试、预发、正式暂保持一体化打包方式不变。
二、方法论:
注意:如果是第一次引入依赖(有依赖变动),则需要采用默认方式,先在本地构建打包再解压,将其中lib中的所有jar放于服务器上的lib目录中。
步骤一:配置项目启动类所在的pom如下:
<build>
<plugins>
<!-- 部署通用插件(不区分环境) -->
<plugin>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<!--默认部署环境(测试、预发、正式)-->
<!--一体化打包(所有依赖包都会被打进业务包里)-->
<id>default</id>
<properties>
<project.env>default</project.env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
<profile>
<!--联调-->
<id>dev</id>
<properties>
<project.env>dev</project.env>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>启动类路径</mainClass>
<layout>ZIP</layout>
<includes>
<include>
直接隶属于项目的子包
</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
步骤二:jekins部署命令增加环境
联调环境相比于默认环境需指定dev方式:clean install package -Pdev -Dmaven.test.skip=true
步骤三:修改启动shell脚本指定lib目录
-Dloader.path=$BASE_PATH/lib
三、实践:
因是第一次构建需将项目依赖包都上传至公共管理路径lib,通过maven默认构建解压后上传
步骤一:插件配置
<build>
<plugins>
<!-- 此插件用来生成通用mapper的代码 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/builder/generatorConfig.xml
</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mapper.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<!--默认(测试、预发、正式)-->
<id>default</id>
<properties>
<project.env>default</project.env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
<profile>
<!--联调-->
<id>dev</id>
<properties>
<project.env>dev</project.env>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.runlion.hshb.erp.HshbErpWebApplication</mainClass>
<layout>ZIP</layout>
<includes>
<include>
<groupId>com.runlion.hshb</groupId>
<artifactId>hb-erp-api</artifactId>
</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
步骤二:jekins部署命令增加环境
联调环境相比于默认环境需指定dev方式:clean install package -Pdev -Dmaven.test.skip=true
步骤三:修改启动shell脚本指定lib目录-Dloader.path=$BASE_PATH/lib
#!/usr/bin/env bash
source /etc/profile
source /app/hshb/erp/sh/constant.sh
BASE_PATH=/app/hshb/erp
PROJECT_NAME=hb-erp
JENKINS_JOB_NAME=hshb-ci-erp
PROFILE=$EVN_PROFILE
DEBUG_PORT=9065
TEMP_PATH=$BASE_PATH/artifacts/$PROJECT_NAME
JAR_NAME=$PROJECT_NAME.jar
echo "停止服务..."
PID=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
if [ -n "$PID" ]
then
echo "kill -9 pid:" $PID
kill -9 $PID
fi
echo "拷贝新jar到running目录"
cp $BASE_PATH/artifacts/$JENKINS_JOB_NAME/hb-erp-web-0.0.1-SNAPSHOT.jar $BASE_PATH/running/$JAR_NAME
echo "拷贝新jar的子jar到lib目录"
unzip -d $TEMP_PATH $BASE_PATH/artifacts/$JENKINS_JOB_NAME/hb-erp-web-0.0.1-SNAPSHOT.jar
cp $TEMP_PATH/BOOT-INF/lib/hb-erp-api-0.0.1-SNAPSHOT.jar $BASE_PATH/lib/
rm -rf $TEMP_PATH
echo "清理超过10天的日志文件..."
cd $BASE_PATH/logs
find $PROJECT_NAME/ -mtime +10 -name "*.log" -exec rm -rf {} \;
rm -rf $PROJECT_NAME-running.log
echo "启动服务 ..."
cd $BASE_PATH/running
nohup java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=$DEBUG_PORT,suspend=n -Xms768m -Xmx768m -jar -Dloader.path=$BASE_PATH/lib $JAR_NAME --spring.profiles.active=$PROFILE >> $BASE_PATH/logs/$PROJECT_NAME-running.log 2>&1 &
NEW_PID=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
echo "启动完成,当前服务PID=$NEW_PID"