为什么要分离打包
我们在使用maven为springboot项目打包时会发现打完的jar包会很大,而实际上里面绝大部分的大文件为静态资源和第三方依赖的jar包,而动辄百兆的jar包每次发布时候就很头痛了,上传服务器时由于带宽的限制每次都非常慢,其次如果只需要改静态文件,但还得重新打包发布并重启,很是麻烦,所以分离打包主要是为了解决2个痛点:
发布时上传时间太久
修改静态文件不用重启服务
1.方式一:分离resources 如配置文件,静态资源
<build>
<finalName>${project.artifactId}-${profiles.active}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.yml</include>
<include>application-${profiles.active}.yml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>templates/**</include>
<include>static/**</include>
<include>mapper/**</include>
<include>freemark/**</include>
<include>spy.properties</include>
</includes>
</resource>
</resources>
<plugins>
<!-- 分离打包开始 -->
<!-- 跳过测试代码 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- maven打包成jar的插件 -->
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- 把target/classes/下的文件导入到jar内 -->
<classesDirectory>target/classes/</classesDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<archive>
<manifest>
<!-- 指定程序入口 -->
<mainClass>com.XXXXXX.XXXXXX</mainClass>
<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<!-- 服务依赖的jar包放在lib目录下 -->
<classpathPrefix>lib/</classpathPrefix>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<!--MANIFEST.MF 中 Class-Path 加入资源文件目录 -->
<Class-Path>./resources/</Class-Path>
</manifestEntries>
</archive>
<!-- jar包中不包括以下文件 -->
<excludes>
<exclude>mapper/**</exclude>
<exclude>static/**</exclude>
<exclude>templates/**</exclude>
<exclude>**/*.yml</exclude>
<exclude>**/*.xml</exclude>
<exclude>**/*.properties</exclude>
</excludes>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</plugin>
<!-- 分离打包关键代码 maven-dependency-plugin -->
<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>
<type>jar</type>
<includeTypes>jar</includeTypes>
<!-- 存放服务依赖的jar包,存放在服务相同目录的lib文件夹下 -->
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- 分离打包关键代码 maven-resources-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文件 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 分环境过滤开启 -->
<filtering>true</filtering>
<includes>
<include>application.yml</include>
<!-- 分环境配置文件不用可删除 -->
<!-- <include>application-${profiles.active}.yml</include> -->
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<!-- 分环境过滤关闭 -->
<filtering>false</filtering>
<includes>
<include>templates/**</include>
<include>static/**</include>
<include>mapper/**</include>
<include>freemark/**</include>
<include>spy.properties</include>
</includes>
</resource>
</resources>
<!-- 输出路径 -->
<outputDirectory>${project.build.directory}/resources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- 分离打包结束 -->
</plugins>
</build>
2.方式二只分离三方jar包,静态资源和配置文件依然放在我们的逻辑业务jar中。
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.sql</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>templates/**</include>
<include>static/**</include>
</includes>
</resource>
</resources>
<plugins>
<!-- 分离打包开始 -->
<!-- 跳过测试代码 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<!-- 指定程序入口 -->
<mainClass>com.XXXXXXX.XXXXXXXX</mainClass>
<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<!-- 服务依赖的jar包放在lib目录下 -->
<classpathPrefix>lib/</classpathPrefix>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</plugin>
<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>
<type>jar</type>
<includeTypes>jar</includeTypes>
<!-- 存放服务依赖的jar包,存放在服务相同目录的lib文件夹下 -->
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
以上配置做完后,就可以打包
然后将 将lib、resources(如果采用方式一)、我的业务.jar这三个文件夹发布服务器上,注意三个目录要在同一个文件夹下面。
不需要添加任何参数就可以启动
java -jar 我的业务逻辑.jar