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 打包输出 ==================== # ==================== Assembly 打包输出 ====================
deploy/worklog-api/ deploy/worklog-api/
deploy/worklog-api.tar.gz
# ==================== 上传文件 ==================== # ==================== 上传文件 ====================
uploads/ uploads/

View File

@ -8,6 +8,10 @@ APP_NAME="worklog-api"
APP_JAR="worklog-api-1.0.0.jar" APP_JAR="worklog-api-1.0.0.jar"
APP_HOME="/opt/worklog/${APP_NAME}" APP_HOME="/opt/worklog/${APP_NAME}"
APP_JAR_PATH="${APP_HOME}/lib/${APP_JAR}" 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() { load_properties() {
@ -83,10 +87,11 @@ fi
# 启动应用 # 启动应用
echo "=========================================" echo "========================================="
echo "启动应用: ${APP_NAME}" echo "启动应用: ${APP_NAME}"
echo "JAR 文件: ${APP_JAR_PATH}" echo "主类: ${MAIN_CLASS}"
echo "Classpath: ${CLASSPATH}"
echo "=========================================" 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 & > "${CONSOLE_LOG}" 2>&1 &
PID=$! PID=$!

View File

@ -112,16 +112,31 @@
<build> <build>
<plugins> <plugins>
<!-- Spring Boot Maven Plugin - 完全禁用 repackage -->
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <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> <configuration>
<excludes> <archive>
<exclude> <manifest>
<groupId>org.projectlombok</groupId> <addClasspath>true</addClasspath>
<artifactId>lombok</artifactId> <classpathPrefix>lib/</classpathPrefix>
</exclude> <mainClass>com.wjbl.worklog.WorklogApplication</mainClass>
</excludes> </manifest>
</archive>
</configuration> </configuration>
</plugin> </plugin>

View File

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