背景
分离lib和config目录, 更新时只需要替换到外面的jar即可, 使用spring-boot-maven-plugin
会将所以内容全部打包进去, 如果src/main/resource/下有些文件很大, 打包后的文件会非常大, 而且在jar里更改信息也不方便, 最新的版本信息可以从mvnrepository查看
- 原打包方式
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.15</version>
</plugin>
</plugins>
</build>
分离后的打包方式
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<outputDirectory>${project.build.directory}/${project.artifactId}</outputDirectory>
<archive>
<addMavenDescriptor>true</addMavenDescriptor>
<manifest>
<mainClass>org.example.App</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<useUniqueVersions>false</useUniqueVersions>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Class-Path>config/ .</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.artifactId}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources/config</directory>
<includes>
<exclude>*</exclude>
<exclude>*/**</exclude>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<exclude>*</exclude>
</includes>
</resource>
</resources>
<outputDirectory>${project.build.directory}/${project.artifactId}/config</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
-
目录结构