Spring Boot 分包打包
默认的打包
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
打包后的结构为
缺点:
1、fat jar很大,外部依赖包也在fat jar里面。不便于管理
2、配置文件也在fat jar里面的,如果修改配置文件需要重新打包(重要)
目标:解决上面的问题,进行将外部依赖和配置文件进行拆分,分包打包
实现(在pom.xml里面自定义打包即可):
移除Spring Boot的默认的打包 Spring Boot plugin
使用maven-jar-plugin 实现打fat jar包(不含有外部依赖和配置文件)
使用maven-dependency-plugin 实现打包外部依赖包。
使用maven-resource-plugin 实现打包配置文件
<build>
<plugins>
<!-- spring-boot-maven-plugin 打包 -->
<!-- <plugin>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
<!-- <version>${spring.boot.version}</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <goals>-->
<!-- <goal>repackage</goal> <!– 将引入的 jar 打入其中 –>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<finalName>
${project.artifactId}
</finalName>
<!--${env.LEARN_HOME}为项目配置的环境变量(target),下同-->
<outputDirectory>${output.jar.file.path}</outputDirectory>
<!-- 将配置文件排除在jar包 -->
<excludes>
<exclude>*.properties</exclude>
<exclude>*.yml</exclude>
<exclude>*.yaml</exclude>
<exclude>*.xml</exclude>
<exclude>*.txt</exclude>
</excludes>
<archive>
<!-- 生成的jar中,包含pom.xml和pom.properties这两个文件 -->
<!-- <addMavenDescriptor>true</addMavenDescriptor>-->
<!-- 生成MANIFEST.MF的设置 -->
<manifest>
<!--这个属性特别关键,当该选项设置为 true 时,Maven 将尝试解析并使用项目依赖的唯一版本。
这意味着如果多个依赖项具有不同的版本,Maven 将选择其中的一个版本,并忽略其他版本。防止多个报错 -->
<useUniqueVersions>false</useUniqueVersions>
<!-- 是否为外部依赖包添加路径, 这些路径会写在MANIFEST文件的Class-Path下 -->
<addClasspath>true</addClasspath>
<!-- MANIFEST.MF 中 Class-Path 各个依赖加入前缀 -->
<!--这个jar所依赖的外部jar包添加classPath的时候的前缀,也就是下面maven-dependency-plugin插件里面配置的依赖包输出路径-->
<classpathPrefix>../${output.dependence.file.path}</classpathPrefix>
<!--指定jar启动入口类 -->
<mainClass>cn.iocoder.yudao.server.YudaoServerApplication</mainClass>
</manifest>
<manifestEntries>
<!-- 主要是静态资源和依赖包,假如你的pom文件里面配置了 <scope>system</scope>,就是你依赖是你本地的
资源,这个时候使用这个插件,classPath里面是不会添加,所以你得手动把这个依赖添加进这个地方 -->
<!--MANIFEST.MF 中 Class-Path 加入自定义路径,多个路径用空格隔开 -->
<!--此处resources文件夹的内容,需要maven-resources-plugin插件补充上-->
<Class-Path>../${output.resource.file.path}</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<!-- 复制依赖的jar包到指定的文件夹里 -->
<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>${output.dependence.file.path}</outputDirectory>
<!-- 是否排除间接依赖,间接依赖也要拷贝 -->
<excludeTransitive>false</excludeTransitive>
<!-- 是否带上版本号 -->
<stripVersion>false</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
<!-- 用于复制指定的文件(配置文件) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<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>
<includes>
<!--将如下格式配置文件拷贝-->
<include>*.properties</include>
<include>*.yml</include>
<include>*.yaml</include>
<include>*.xml</include>
</includes>
</resource>
</resources>
<!--输出路径-->
<outputDirectory>${output.resource.file.path}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
上面的${}里面的都是在properties标签定义好了的
最后点击打包(package)如下图大功告成