背景:因為項目開發中經常發測試環境,發現使用阿里的插件能一鍵上傳,不用手動上傳比較方便。但是多模塊有多個啟動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