楔子
springboot項目打包本地jar到 檔案中,其他依賴的jar打包到 單獨目錄中。
打包本地jar
<dependency>
<groupId>com.sun.jna</groupId>
<artifactId>jna</artifactId>
<version>1.0</version>
<systemPath>${basedir}/libs/jna.jar</systemPath>
<scope>system</scope>
</dependency>
<dependency>
<groupId>com.sun.jna.examples</groupId>
<artifactId>jna-examples</artifactId>
<version>1.0</version>
<systemPath>${basedir}/libs/examples.jar</systemPath>
<scope>system</scope>
</dependency>
<!--無法分離的jar包如果jar包 直接打入源碼jar包| jar 包在項目lib目錄下 -->
<resource>
<directory>${project.basedir}/libs</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
整體pom檔案
<build>
<resources>
<!-- Resource Filter -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--指定資源的位置-->
<resource>
<directory>src/main/resources</directory>
</resource>
<!--無法分離的jar包如果jar包不大可以選擇直接打入源碼jar包-->
<resource>
<directory>${project.basedir}/libs</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</resources>
<plugins>
<!--打包jar-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!--不打包資源檔案-->
<excludes>
<exclude>*.**</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!--MANIFEST.MF 中 Class-Path 加入字首-->
<classpathPrefix>lib/</classpathPrefix>
<!--jar包不包含唯一版本辨別-->
<useUniqueVersions>true</useUniqueVersions>
<!--指定入口類-->
<mainClass>com.Application</mainClass>
</manifest>
<manifestEntries>
<!--MANIFEST.MF 中 Class-Path 加入資源檔案目錄-->
<!--本地依賴包需要手動 加入Class-Path ,否則無法找到-->
<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>
</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>null</groupId>
<artifactId>null</artifactId>
</include>
</includes>
<layout>ZIP</layout>
<!--使用外部配置檔案,jar包裡沒有資源檔案-->
<addResources>true</addResources>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
打包效果如下