Springboot 打jar包时下载分离依赖包到指定目录

1.maven 构建

POM.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?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

1
java -jar -Dloader.path=.,3rd-lib test-0.0.1-SNAPSHOT-classes.jar

2.gradle 构建

1
2
3
4
5
6
7
8
9
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'
}
 
posted @   小天儿  阅读(1190)  评论(2编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示