为什么要分离打包
- 在往服务器上传时可以节省时间,同时也可以直接修改配置文件
如果不分离打包,一个项目jar包一般在大几十M,上传时比较费劲
分离打包,可以将依赖jar和配置文件打包到不同文件夹下,项目jar中不包含依赖jar,jar包大小会变得很小,只包含源码。
pom内容
<resources>
<!-- 资源过滤-->
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>mapper/**</include>
<include>application.yml</include>
<include>application-dev.yml</include>
<include>bootstrap.yml</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.asiainfo.cem.CemUserJourneyApplication</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>
<version>2.8</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<!-- <goal>copy-dependencies</goal>-->
<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-dev.yml</include>
<include>bootstrap.yml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<!-- 分环境过滤关闭 -->
<filtering>false</filtering>
<includes>
<include>*</include>
<include>mapper/**</include>
<!-- <include>application.yml</include>-->
<!-- <include>application-dev.yml</include>-->
<!-- <include>bootstrap.yml</include>-->
</includes>
</resource>
</resources>
<!-- 输出路径 -->
<outputDirectory>${project.build.directory}/resources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- 分离打包结束 -->