maven部署方案之分离业务包

一、思想:

通过将业务包和公共包分离,集中管理所有包,打包时只构建业务包减少项目包的大小和传输时间。

为了观测稳定性,暂通过环境区分,较为频繁的联调环境采用该方式,测试、预发、正式暂保持一体化打包方式不变。

二、方法论:

注意:如果是第一次引入依赖(有依赖变动),则需要采用默认方式,先在本地构建打包再解压,将其中lib中的所有jar放于服务器上的lib目录中。(可通过步骤二增加一个默认环境的打包方式代替)

步骤一:配置项目启动类所在的pom如下:
<build>
    <plugins>
        
        <plugin> 
        plugin>
    plugins>
build>
 
<profiles>
    <profile>
        
        
        <id>defaultid>
        <properties>
            <project.env>defaultproject.env>
        properties>
        <activation>
            <activeByDefault>trueactiveByDefault>
        activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
            plugins>
        build>
    profile>
 
    <profile>
        
        <id>devid>
        <properties>
            <project.env>devproject.env>
        properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <configuration>
                        <mainClass>启动类路径mainClass>
                        <layout>ZIPlayout>
                        <includes>
                            <include>
                              直接隶属于项目的子包
                            include>
                        includes>
                    configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackagegoal>
                            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默认构建解压后上传
maven部署方案之分离业务包_第1张图片

步骤一:插件配置
<build>
    <plugins>
        
        <plugin>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-maven-pluginartifactId>
            <version>1.3.2version>
            <configuration>
                <configurationFile>${basedir}/src/main/resources/builder/generatorConfig.xml
                configurationFile>
                <overwrite>trueoverwrite>
                <verbose>trueverbose>
            configuration>
            <dependencies>
                <dependency>
                    <groupId>mysqlgroupId>
                    <artifactId>mysql-connector-javaartifactId>
                    <version>8.0.23version>
                    <scope>runtimescope>
                dependency>
                <dependency>
                    <groupId>com.baomidougroupId>
                    <artifactId>mybatis-plus-boot-starterartifactId>
                    <version>${mapper.version}version>
                dependency>
            dependencies>
        plugin>
    plugins>
 
build>
<profiles>
    <profile>
        
        <id>defaultid>
        <properties>
            <project.env>defaultproject.env>
        properties>
        <activation>
            <activeByDefault>trueactiveByDefault>
        activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
            plugins>
 
        build>
    profile>
    <profile>
        
        <id>devid>
        <properties>
            <project.env>devproject.env>
        properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <configuration>
                        <mainClass>com.runlion.hshb.erp.HshbErpWebApplicationmainClass>
                        <layout>ZIPlayout>
                        <includes>
                            <include>
                                <groupId>com.runlion.hshbgroupId>
                                <artifactId>hb-erp-apiartifactId>
                            include>
                        includes>
                    configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackagegoal>
                            goals>
                        execution>
                    executions>
                plugin>
            plugins>
        build>
    profile>
profiles>
步骤二:jekins部署命令增加环境

联调环境相比于默认环境需指定dev方式:clean install package -Pdev -Dmaven.test.skip=true
maven部署方案之分离业务包_第2张图片

步骤三:修改启动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
mv $TEMP_PATH/BOOT-INF/lib/*   $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"

你可能感兴趣的:(maven,mybatis,java)