springboot maven打包分离 lib resource 配置资源文件
注:
使用maven命令打包,不同的IDE可以一样的操作
在pom文件中,设置两个属性
<properties>
<output.dependence.file.path>lib/</output.dependence.file.path>
<output.resource.file.path>resource/</output.resource.file.path>
</properties>
设置build节点。这样在pom文件目录执行mvn clean package命令,就能在target文件夹输出打包结果了:
依赖的jar会输出到output.dependence.file.path指定的文件夹,配置文件输出到output.resource.file.path 指定的文件夹
通过pom文件使用mvn package 一建打包
lib:放第三方JAR
resources:存配置文件
<plugins>
<!-- 打JAR包,不包含依赖文件;显式剔除配置文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!--不打包资源文件,剔除配置文件-->
<excludes>
<exclude>*.**</exclude>
<exclude>*/*.xml</exclude>
<exclude>*.properties</exclude>
<exclude>*.yml</exclude>
<exclude>*/*.properties</exclude>
<exclude>*/*.yml</exclude>
</excludes>
<archive>
<manifest>
<!--将classpath添加到依赖描述 -->
<addClasspath>true</addClasspath>
<!--MANIFEST.MF 中 Class-Path 加入前缀-->
<!--lib文件夹内容,需要 maven-dependency-plugin插件补充-->
<classpathPrefix>lib/</classpathPrefix>
<!--jar包不包含唯一版本标识-->
<useUniqueVersions>false</useUniqueVersions>
<!--指定入口类-->
<mainClass>site.yuyanjia.template.Application</mainClass>
</manifest>
<manifestEntries>
<!--MANIFEST.MF 中 Class-Path 加入资源文件目录,加入自定义路径,多个路径用空格隔开-->
<!--此处resources文件夹的内容,需要maven-resources-plugin插件补充上-->
<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>
<includes>
<include>*.properties</include>
<include>*.yml</include>
<include>*/*.properties</include>
<include>*/*.properties</include>
</includes>
</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>non-exists</groupId>
<artifactId>non-exists</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>
<!--maven打包时,跳过测试-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>