Springboot 打jar包时下载分离依赖包到指定目录
1.maven 构建
POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 其他配置省略 -->
<build>
<plugins>
<!-- 管理依赖包,可以通过 copy 任务拷贝单个依赖或者 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>target/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
<!-- 打 jar 包用,可以配置包的 MANIFEST -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
<exclude>**/*.yml</exclude>
<exclude>static/**</exclude>
<exclude>templates/**</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 重要配置项 layout,否则通过 loader.path 指定外部依赖包时无法加载 -->
<configuration>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 构建工具 ant 的插件,用来执行 ant 任务,此处用以操作文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<property name="dist">target/distribution</property>
<property name="dist-tmp">target/distribution/tmp</property>
<property name="app-name">${project.artifactId}-${project.version}</property>
<mkdir dir="${dist-tmp}" />
<copy file="target/${app-name}.jar" tofile="${dist-tmp}/${app-name}.jar" />
<unzip src="${dist-tmp}/${app-name}.jar" dest="${dist-tmp}" />
<delete file="${dist-tmp}/${app-name}.jar" />
<zip destfile="${dist}/${app-name}-pages.jar">
<zipfileset dir="${dist-tmp}/META-INF" prefix="META-INF" />
<zipfileset dir="target/classes/static" prefix="static" />
<zipfileset dir="target/classes/templates" prefix="templates" />
</zip>
<move file="target/${app-name}-classes.jar" todir="${dist}" />
<move todir="${dist}/3rd-lib">
<fileset dir="target/lib" />
</move>
<delete dir="${dist-tmp}" />
<copy todir="${dist}">
<fileset dir="target/classes">
<include name="**/*.properties" />
<include name="**/*.xml" />
<include name="**/*.yml" />
</fileset>
</copy>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
打完包后目录结构
- 3rd-lib
- META-INF
- *.yml
- *.xml
- *.properties
- test-0.0.1-SNAPSHOT-classes.jar
- test-0.0.1-SNAPSHOT-pages.jar
运行jar
java -jar -Dloader.path=.,3rd-lib test-0.0.1-SNAPSHOT-classes.jar
2.gradle 构建
dependencies {
implementation 'g:a:v'
}
//copying all dependencies attached to 'compileClasspath' into a specific folder
task downloadDependencies(type: Copy) {
//referring to the 'compileClasspath' configuration
from configurations.compileClasspath
into '3rd-lib'
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

浙公网安备 33010602011771号