Spring Boot 打包,分离依赖jar,配置文件

在指定 启动模块 的 pom.xml里增加下面的配置

注意修改 指定入口类,也就是(启动类) ,4.2已提交,直接更新就好了。

  <!--打包jar start 分开依赖jar,和配置文件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <!--不打包资源文件-->
                    <excludes>
                        <exclude>*.**</exclude>
                        <exclude>*/*.xml</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!--MANIFEST.MF 中 Class-Path 加入前缀-->
                            <classpathPrefix>lib/</classpathPrefix>
                            <!--jar包不包含唯一版本标识-->
                            <useUniqueVersions>false</useUniqueVersions>
                            <!--指定入口类-->
                            <mainClass>cn.myapps.run.DesignerApp</mainClass>
                        </manifest>
                        <manifestEntries>
                            <!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
                            <Class-Path>./resources/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                </configuration>
            </plugin>

            <!--拷贝依赖 copy-dependencies-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}/lib/
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!--拷贝资源文件 copy-resources-->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                </resource>
                            </resources>
                            <outputDirectory>${project.build.directory}/resources</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!--spring boot repackage,依赖 maven-jar-plugin 打包的jar包 重新打包成 spring boot 的jar包-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--重写包含依赖,包含不存在的依赖,jar里没有pom里的依赖-->
                    <includes>
                        <include>
                            <groupId>null</groupId>
                            <artifactId>null</artifactId>
                        </include>
                    </includes>
                    <layout>ZIP</layout>
                    <!--使用外部配置文件,jar包里没有资源文件-->
                    <addResources>true</addResources>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <!--配置jar包特殊标识 配置后,保留原文件,生成新文件 *-run.jar -->
                            <!--配置jar包特殊标识 不配置,原文件命名为 *.jar.original,生成新文件 *.jar -->
                            <!--<classifier>run</classifier>-->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!--打包jar end-->

添加后运行

mvn clean package 

打包好后

效果如下

  • lib 依赖jar包目录
  • resources 配置文件目录
  • obpm-designer-4.2.0.jar 最终执行的jar包

可以从MANIFEST.MF发现文件关系

在jar包中搜索MANIFEST.MF

Manifest-Version: 1.0
Implementation-Title: spring-boot-template
Implementation-Version: 0.0.1
Built-By: seer
Implementation-Vendor-Id: site.yuyanjia
Class-Path: lib/mysql-connector-java-6.0.6.jar lib/shiro-spring-1.4.0.jar 
中间省略若干配置  
lib/spring-boot-2.0.2.RELEASE.jar ./resources/
Spring-Boot-Version: 2.0.2.RELEASE
Main-Class: org.springframework.boot.loader.PropertiesLauncher
Start-Class: site.yuyanjia.template.Application
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.5.0
Build-Jdk: 1.8.0_40
Implementation-URL: https://projects.spring.io/spring-boot/#/spring-boot-starter-parent/spring-boot-template

后记:发现分开依赖后,有的jar里面的@controller 无法被扫描到。导致方法404错误

   <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-jar-plugin</artifactId>
       <configuration>
           <excludes>
               <exclude>*.**</exclude>
               <exclude>*/*.xml</exclude>
           </excludes>
           <archive>
               <manifest>
                   <!--添加这一项配置 ,即可解决-->
                   <addDefaultImplementationEntries>true</addDefaultImplementationEntries>  
                   <!--添加这一项配置 ,即可解决  end-->
                   
                   <addClasspath>true</addClasspath>
                   <classpathPrefix>lib/</classpathPrefix>
                   <useUniqueVersions>false</useUniqueVersions>
                   <mainClass>cn.myapps.run.DesignerApp</mainClass>
               </manifest>
               <manifestEntries>
                   <Class-Path>./designer_config/</Class-Path>
               </manifestEntries>
           </archive>
           <outputDirectory>${project.build.directory}</outputDirectory>
       </configuration>
</plugin>

参考文章:Spring Boot 打包,分离依赖jar,配置文件