背景:因为项目开发中经常发测试环境,发现使用阿里的插件能一键上传,不用手动上传比较方便。但是多模块有多个启动jar的时候,全局打包太慢,单独打发现报错。这里贴一下我使用这个插件的方式:
附带一个我感觉不错的脚本,使用这个插件把jar包放到服务器目录,然后实现备份发布
#!/bin/bashAPP_NAME=-admin.jar
ENV=sit
APP_HOME=/home/admin # <<< 加:应用目录
NEW_JAR_DIR=/root # 新 JAR 包来源目录
BACKUP_DIR=$APP_HOME/old-jar # 旧 JAR 备份目录(绝对路径更安全)# 切换到 APP_HOME 目录,避免路径错误
cd "$APP_HOME" || {echo "ERROR: 目录 $APP_HOME 不存在,终止执行"exit 1
}usage(){echo "Usage: sh $0 [start|stop|restart|status|-v]"exit 1
}# 检查程序是否在运行
is_exist(){pid=$(ps -ef | grep "$APP_NAME" | grep -v grep | awk '{print $2}')if [ -z "$pid" ]; thenreturn 1elsereturn 0fi
}backup_and_update_jar(){if [ -f "$APP_NAME" ]; thentimestamp=$(date +"%Y%m%d%H%M%S")cp "$APP_NAME" "${APP_NAME%.*}-$timestamp.jar"mv "${APP_NAME%.*}-$timestamp.jar" "$BACKUP_DIR"echo "旧版本已备份到 $BACKUP_DIR/${APP_NAME%.*}-$timestamp.jar"fiif [ -f "$NEW_JAR_DIR/$APP_NAME" ]; thenrm -f "$APP_NAME"mv "$NEW_JAR_DIR/$APP_NAME" "$APP_NAME"echo "使用新版本jar"elseecho "未找到新JAR包 $NEW_JAR_DIR/$APP_NAME,使用旧jar"fi
}start(){is_existif [ $? -eq 0 ]; thenecho "$APP_NAME is already running. pid=$pid"elsenohup /usr/bin/java -Xms512m -Xmx512m -Xmn256m -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=256m \-XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParNewGC -XX:+UseConcMarkSweepGC \-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -Xloggc:gc/gc-$(date +%Y%m%d%H%M%S).log \-jar "$APP_NAME" --spring.profiles.active="$ENV" --jasypt.encryptor.password=Xxzx@0710@ > log 2>&1 &echo "$APP_NAME start success"fi
}stop(){is_existif [ $? -eq 0 ]; thenkill -9 "$pid"echo "$APP_NAME stop success"elseecho "$APP_NAME is not running"fi
}status(){is_existif [ $? -eq 0 ]; thenecho "$APP_NAME is running. Pid is $pid"elseecho "$APP_NAME is NOT running."fi
}v(){echo "access version 1.0"
}restart(){stopbackup_and_update_jarsleep 1start
}case "$1" in"start")start;;"stop")stop;;"restart")restart;;"status")status;;"-v")v;;*)usage;;
esac
以ruoyi这个项目举例:
单独打包admin时会提示类不存在,尽管这个项目类在。
[ERROR] /D:/workspace/zhengshu/ysb-multiport-manage-control/back-control-admin/src/main/java/com/zs/web/controller/multiport/AppCareController.java:[3,54] 程序包com.baomidou.mybatisplus.core.conditions.query不存在
一开始以为是pom里的打包插件配置的不对,调整了一下,发现还是不行。
建议就配一个跳过test插件和springboot插件就行。
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><configuration><skipTests>true</skipTests></configuration></plugin><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.xx.AdminApplication</mainClass><skip>false</skip> <!-- 此模块需要打包 --></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins><finalName>${project.artifactId}</finalName>
发现不是打包插件问题后,gpt给出了答案:
我平常的构建项目习惯是父pom里使用dependencyManagement标注所有的依赖,然后在子模块里引入对应的,admin基本不依赖什么,都是通过依赖传递使用system的。所以会出现这个问题,引以为戒。
附带一个如果不知道类的依赖由于传递依赖,不知道是实际的哪个坐标引入的命令:
mvn dependency:tree -Dverbose -Dincludes=org.apache.commons:commons-collections4