找回密码
 立即注册
首页 业界区 业界 MAVEN构建分离依赖JAR

MAVEN构建分离依赖JAR

森萌黠 2025-8-8 16:01:12
MAVEN构建分离依赖JAR

1. 背景说明

在Springboot项目中,项目构建时,默认打包成一个可以执行的jar包.导致单一jar过大.项目部署过程中,需要把依赖的jar包和配置文件都单独存放到指定的文件夹中.
2. 插件配置


  • maven-compiler-plugin 用于编译java代码
  • maven-jar-plugin 用于构建jar包
  • spring-boot-maven-plugin 用于构建Springboot项目
  • maven-assembly-plugin 用于打包项目
3. 编译配置
  1. <plugin>
  2.    <groupId>org.apache.maven.plugins</groupId>
  3.    maven-compiler-plugin</artifactId>
  4.    <configuration>
  5.      
  6.               <path>
  7.                       <groupId>org.projectlombok</groupId>
  8.                       lombok</artifactId>
  9.                       <version>${lombok.version}</version>
  10.       </path>
  11.      </annotationProcessorPaths>
  12.    </configuration>
  13. </plugin>
复制代码
4. 构建jar配置
  1. <plugin>
  2.    <groupId>org.apache.maven.plugins</groupId>
  3.    maven-jar-plugin</artifactId>
  4.    <configuration>
  5.       
  6.         <manifest>
  7.                <useUniqueVersions>false</useUniqueVersions>
  8.          <mainClass>com.herbert.Application</mainClass>
  9.         </manifest>
  10.       </archive>
  11.       <excludes>
  12.               <exclude>*.properties</exclude>
  13.               <exclude>*.yml</exclude>
  14.               <exclude>**/mapper/*.xml</exclude>
  15.       </excludes>
  16.    </configuration>
  17. </plugin>
复制代码
5. 构建springboot项目配置

其中主要配置 ZIP\ 为关键配置,打包后的jar包对应MANIFEST.MF配置的启动类为 Main-Class: org.springframework.boot.loader.PropertiesLauncher
  1. <plugin>
  2. <groupId>org.springframework.boot</groupId>
  3. spring-boot-maven-plugin</artifactId>
  4. <configuration>
  5. <layout>ZIP</layout>
  6. <includes>
  7.         <include>
  8.                 <groupId>non-exists</groupId>
  9.                 non-exists</artifactId>
  10.         </include>
  11. </includes>
  12. <excludes>
  13.         <exclude>
  14.                 <groupId>org.projectlombok</groupId>
  15.                 lombok</artifactId>
  16.         </exclude>
  17. </excludes>
  18. </configuration>
  19. </plugin>
复制代码
5. 项目整体打包配置
  1. <plugin>
  2.    <groupId>org.apache.maven.plugins</groupId>
  3.    maven-assembly-plugin</artifactId>
  4.    <configuration>
  5.       false</appendAssemblyId>
  6.       <descriptors>
  7.               <descriptor>./package.xml</descriptor>
  8.       </descriptors>
  9.       <outputDirectory>${project.basedir}/dist</outputDirectory>
  10.    </configuration>
  11.    <executions>
  12.       <execution>
  13.               <id>make-assembly</id>
  14.               <phase>package</phase>
  15.               <goals>
  16.                       <goal>single</goal>
  17.               </goals>
  18.       </execution>
  19.    </executions>
  20. </plugin>
复制代码
其中 package.xml文件内容如下
  1.         <id>distribution</id>
  2.         <formats>
  3.                 <format>zip</format>
  4.         </formats>
  5.    
  6.         <includeBaseDirectory>false</includeBaseDirectory>
  7.         <fileSets>
  8.                 <fileSet>
  9.                         <directory>src/main/resources/</directory>
  10.                         <outputDirectory>/resources</outputDirectory>
  11.                         <filtered>true</filtered>
  12.                 </fileSet>
  13.                 <fileSet>
  14.                         <directory>${project.basedir}/script</directory>
  15.                         <outputDirectory>/</outputDirectory>
  16.                         <filtered>true</filtered>
  17.                 </fileSet>
  18.                  <fileSet>
  19.                     <directory>${project.basedir}/web/</directory>
  20.                         <outputDirectory>/resources/static</outputDirectory>
  21.                  </fileSet>
  22.         </fileSets>
  23.         <dependencySets>
  24.           <dependencySet>
  25.                         <outputDirectory>/lib</outputDirectory>
  26.                         <scope>runtime</scope>
  27.                         <excludes>
  28.                                 <exclude>${project.groupId}:${project.artifactId}</exclude>
  29.                         </excludes>
  30.                 </dependencySet>
  31.                 <dependencySet>
  32.                         <outputDirectory>/</outputDirectory>
  33.                         <includes>
  34.                                 <include>${project.groupId}:${project.artifactId}</include>
  35.                         </includes>
  36.                 </dependencySet>
  37.         </dependencySets>
  38. </assembly>
复制代码
fileSets/fileSet/filtered 属性为true时,表示在maven复制文件时,可以把maven的占位符替换成对应的值,比如需要打包文件中包含一个startup.bat文件,项目打包的源文件配置如下:
  1. @echo off
  2. title ${project.artifactId}-${project.version}-%date%-%time%-%cd%
  3. java -Dloader.debug=true -jar  ${project.artifactId}-${project.version}.jar
复制代码
项目打包后,输出文件内容会改变成如下:
  1. rem 小游戏 地心侠士 by herbert
  2. @echo off
  3. title herbert.1.0.0-%date%-%time%-%cd%
  4. java -Dloader.debug=true -jar herbert-1.0.0.jar
复制代码
7. 项目启动问题

根据以上配置后,需要动态指定PropertiesLauncher的loader.path来加载分离后的lib,以及其他资源文件.这里提供两个实用的方法

  • 通过 java -D指定参数
    java -jar -Dloader.path=resources,lib,plugin hebert-1.0.0-SNAPSHOT.jar
  • 在jar包同级目录下创建loader.properties文件,并添加如下内容
    loader.path=resources,lib,plugin

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册