feat: 优化打包配置,生成瘦JAR和分离依赖

- 禁用 spring-boot-maven-plugin 的 repackage
- 配置 maven-jar-plugin 指定主类
- assembly.xml 只输出 tar.gz 格式
- 应用 JAR (97KB) 只包含业务代码
- 依赖 JAR (87个) 单独打包到 lib 目录
- 更新 start.sh 使用 -cp 方式启动
- 更新 .gitignore 忽略打包输出
This commit is contained in:
zhangjf 2026-02-24 18:09:04 +08:00
parent ae97de3d69
commit d627b19976
4 changed files with 46 additions and 14 deletions

1
.gitignore vendored
View File

@ -58,6 +58,7 @@ db_backup/
# ==================== Assembly 打包输出 ====================
deploy/worklog-api/
deploy/worklog-api.tar.gz
# ==================== 上传文件 ====================
uploads/

View File

@ -8,6 +8,10 @@ APP_NAME="worklog-api"
APP_JAR="worklog-api-1.0.0.jar"
APP_HOME="/opt/worklog/${APP_NAME}"
APP_JAR_PATH="${APP_HOME}/lib/${APP_JAR}"
MAIN_CLASS="com.wjbl.worklog.WorklogApplication"
# Classpath 配置lib 目录下所有 JAR + conf 目录)
CLASSPATH="${APP_HOME}/lib/*:${APP_HOME}/conf"
# 配置文件加载函数
load_properties() {
@ -83,10 +87,11 @@ fi
# 启动应用
echo "========================================="
echo "启动应用: ${APP_NAME}"
echo "JAR 文件: ${APP_JAR_PATH}"
echo "主类: ${MAIN_CLASS}"
echo "Classpath: ${CLASSPATH}"
echo "========================================="
nohup java ${JVM_OPTS} -jar "${APP_JAR_PATH}" ${SPRING_OPTS} \
nohup java ${JVM_OPTS} -cp "${CLASSPATH}" ${MAIN_CLASS} ${SPRING_OPTS} \
> "${CONSOLE_LOG}" 2>&1 &
PID=$!

View File

@ -112,16 +112,31 @@
<build>
<plugins>
<!-- Spring Boot Maven Plugin - 完全禁用 repackage -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>repackage</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<!-- Maven JAR Plugin - 配置主类 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.wjbl.worklog.WorklogApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

View File

@ -5,26 +5,27 @@
<id>dist</id>
<formats>
<format>dir</format>
<format>tar.gz</format>
</formats>
<!-- 包含基础目录 -->
<includeBaseDirectory>false</includeBaseDirectory>
<!-- 包含基础目录 -->
<includeBaseDirectory>true</includeBaseDirectory>
<!-- 输出目录名 -->
<baseDirectory>worklog-api</baseDirectory>
<fileSets>
<!-- JAR 包输出到 lib 目录 -->
<!-- 应用 JAR 包输出到 lib 目录 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
<include>${project.build.finalName}.jar</include>
</includes>
<excludes>
<exclude>*-sources.jar</exclude>
<exclude>*-javadoc.jar</exclude>
<exclude>*.original</exclude>
</excludes>
</fileSet>
@ -42,7 +43,7 @@
<lineEnding>unix</lineEnding>
</fileSet>
<!-- 配置文件输出到 conf 目录(从 resources 目录获取) -->
<!-- 配置文件输出到 conf 目录 -->
<fileSet>
<directory>${project.basedir}/src/main/resources</directory>
<outputDirectory>conf</outputDirectory>
@ -57,4 +58,14 @@
</fileSet>
</fileSets>
<!-- 依赖 JAR 包输出到 lib 目录 -->
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>true</useTransitiveDependencies>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>