GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 添加maven打包 · wodeu2/PassJava-Learning@123b6dc

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • docs
    • 03.SpringBoot
      • 01.自定义Redis_Starter.md
      • 02.maven_thirdpart_jar.md
      • 08.SpringBoot-Mybatis逻辑删除.md
      • 09.Object划分.md
      • 09.Object划分md
      • spring-boot-05-data-jdbc.md
      • spring-boot-06-data-druid.md
      • spring-boot-07-data-mybatis.md
    • 05.Interview/03.Database
      • MySQL1.md
    • 99.tools
      • 91.Maven配置.md
    • README.md
    • _sidebar.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎…/03.SpringBoot学习篇/01.自定义Redis_Starter.md‎ ‎docs/03.SpringBoot/01.自定义Redis_Starter.md‎docs/03.SpringBoot学习篇/01.自定义Redis_Starter.md renamed to docs/03.SpringBoot/01.自定义Redis_Starter.md docs/03.SpringBoot学习篇/01.自定义Redis_Starter.md renamed to docs/03.SpringBoot/01.自定义Redis_Starter.md

Copy file name to clipboard
  • Display the source diff
  • Display the rich diff
File renamed without changes.
Collapse file

‎docs/03.SpringBoot/02.maven_thirdpart_jar.md‎

Copy file name to clipboard
+247Lines changed: 247 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
# 记一次 Maven 打包后,第三方无法使用的排查记录
2+
3+
> 原文首发公众号:悟空聊架构
4+
>
5+
> 发布日期:2022-12-06
6+
>
7+
> https://mp.weixin.qq.com/s/8VcaW1KuNVs-Sn8gQVU0jg
8+
9+
![](http://cdn.jayh.club/uPic/640BsGUpu.png)
10+
11+
这是悟空的第 173 篇原创文章
12+
13+
官网:www.passjava.cn
14+
15+
你好,我是悟空。
16+
17+
本文主要内容如下:
18+
19+
![图片](http://cdn.jayh.club/uPic/640-20221207084759377FiD4qe.png)目录
20+
21+
## 前言
22+
23+
最近遇到一个需求:
24+
25+
写一个工具类的 JAR 包,然后提供给第三方调用其中的类方法。(前提:第三方无法共用我们项目的私有仓库)
26+
27+
期间遇到了一些问题:
28+
29+
- 第三方引入 JAR 包后,无法 import。
30+
- 第三方引入 JAR 包后,缺少 JAR 包中的其他依赖。
31+
32+
本篇做个记录,希望能帮助到其他小伙伴。
33+
34+
本篇既然涉及到 Maven,这里先总结下 Maven 的常用命令。对 Maven 命令比较熟悉的同学可以跳过这小节。
35+
36+
## 一、Maven 常见命令
37+
38+
当我们创建好一个 Maven 工程时,IDEA 开发工具的右侧就会自动出现 Maven 命令。
39+
40+
Maven 操作
41+
42+
我们用鼠标双击下就可以运行了,也可以通过命令行来执行
43+
44+
45+
46+
下面介绍这几种命令的区别。
47+
48+
### clean(常用)
49+
50+
删除项目路径下的 target 文件,但是不会删除本地的maven仓库已经生成的 JAR 文件。
51+
52+
53+
54+
### validate
55+
56+
验证工程正确性,所需信息是否完整。
57+
58+
### compile
59+
60+
编译。会在你的项目路径下生成一个target目录,在该目录中包含一个classes文件夹,里面全是生成的class文件。
61+
62+
### test
63+
64+
执行单元测试。
65+
66+
### package(常用)
67+
68+
将工程文件打包为指定的格式,例如 JAR,WAR 等(看你项目的 pom文件,里面packaging 标签就是来指定打包类型的)。
69+
70+
这个命令会在你的项目路径下一个target目录,并且拥有compile命令的功能进行编译,同时会在target目录下生成项目的 jar/war文件。
71+
72+
![图片](http://cdn.jayh.club/uPic/640-20221207084759495swaPgF.png)
73+
74+
如果a项目依赖于b项目,打包b项目时,只会打包到b项目下target下,编译a项目时就会报错,因为找不到所依赖的b项目,说明a项目在本地仓库是没有找到它所依赖的b项目,这时就用到 install 命令。
75+
76+
### verify
77+
78+
核实,主要是对 package 检查是否有效、符合标准。
79+
80+
### install(常用)
81+
82+
将包安装至本地仓库,以让其它项目依赖。
83+
84+
该命令包含了 package 命令功能,不但会在项目路径下生成 class 文件和 jar 包,同时会在你的本地maven仓库生成 jar 文件,供其他项目使用(如果没有设置过maven本地仓库,一般在用户 **/.m2** 目录下。如果 a 项目依赖于 b 项目,那么 install b 项目时,会在本地仓库同时生成 pom 文件和 jar文件,解决了上面打包 package出错的问题)。
85+
86+
![图片](http://cdn.jayh.club/uPic/640-202212070847596234mylGM.png)
87+
88+
### build
89+
90+
建造。功能类似compile,区别是对整个项目进行编译。
91+
92+
与 compile区别及特点:是对整个工程进行彻底的重新编译,而不管是否已经编译过。
93+
94+
Build过程往往会生成发布包,这个具体要看对 IDE 的配置了,Build在实际中应用很少,因为开发时候基本上不用,发布生产时候一般都用ANT等工具来发布。Build 因为要全部编译,还要执行打包等额外工 作,因此时间较长。
95+
96+
### site
97+
98+
生成项目的站点文档。
99+
100+
### deploy(常用)
101+
102+
部署。将 jar 包部署到远程仓库,通常是私有仓库。而且包含了 install 命令的功能。
103+
104+
## 二、打包后,无法 import?
105+
106+
下面介绍一下我用常规打包方式遇到的问题。
107+
108+
我通过 IDEA 工具创建了一个 SpringBoot 项目,然后 pom.xml 文件中会自动引入一个打包插件,如下图所示:
109+
110+
111+
112+
然后我执行 maven package 命令,会在项目的 target 目录生成一个 JAR 包。如下图所示:
113+
114+
115+
116+
然后我做了以下事情:
117+
118+
- 把这个 JAR 包拷贝出来,发给了第三方。
119+
- 让第三方拷贝到他们自己的本地项目中。这里是在项目的根目录创建了一个 libs 目录,然后将 jar 包放到 libs 目录中。
120+
121+
- 让第三方在 pom 依赖中引入这个依赖包。
122+
123+
124+
125+
scope 指定为 system,表示引入指定路径(systemPath配置)下的 JAR 包。
126+
127+
看起来这么做没问题了,但是当我们 import 这个 JAR 包下的类时,就会报错。如下图所示:
128+
129+
![图片](http://cdn.jayh.club/uPic/640-20221207084759733tUlUza.png)
130+
131+
**很奇怪,这里为什么会报错呢??**
132+
133+
先看下这个 JAR 包是否引入了。如下所示,可以看到确实是正确引入了,没有报错。
134+
135+
![图片](http://cdn.jayh.club/uPic/640-202212070847598356FccKa.png)
136+
137+
通过 research,发现这个打包插件打出来的 JAR 包,是供执行的,也就是可以通过 java -jar 命令来运行这个 JAR 包,并不能给第三方来引用使用。
138+
139+
**解决方案:**换一个打包插件 `maven-compiler-plugin`。
140+
141+
```
142+
<plugin>
143+
<groupId>org.apache.maven.plugins</groupId>
144+
<artifactId>maven-compiler-plugin</artifactId>
145+
<version>3.8.1</version>
146+
<configuration>
147+
<source>1.8</source>
148+
<target>1.8</target>
149+
<encoding>UTF-8</encoding>
150+
</configuration>
151+
</plugin>
152+
```
153+
154+
再次打包发给第三方,发现 import 不报错了。
155+
156+
但是又报另外一个错,我们接着往下看。
157+
158+
## 三、缺少其他 Jar 包依赖?
159+
160+
报错信息如下:
161+
162+
```
163+
java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64
164+
```
165+
166+
通过这个信息,可以想到是不是我提供的 JAR 包中引入了这个 commons-codec 依赖,而 JAR 包文件中又不包含这个依赖。
167+
168+
看下这个 JAR 文件的大小,只有 14 KB, 而 commons-codec 的包大小为 339 KB,说明这个 JAR 包确实不包含 common-codec 依赖。
169+
170+
![图片](http://cdn.jayh.club/uPic/640-20221207084800039trOMwv.png)
171+
172+
解决方案:
173+
174+
- 将其他依赖包打入到这个 JAR 包里面(推荐)。
175+
- 第三方自己引入其他依赖包。(麻烦了第三方,要第三方一个个引入)
176+
177+
**那如何将依赖的包打进这个 JAR 包里面呢?**
178+
179+
这里还要引入一个打包插件:maven-assembly-plugin,如下所示。(省略了部分标签)
180+
181+
```
182+
<!--添加依赖 maven-assembly-plugin -->
183+
<dependency>
184+
<groupId>org.apache.maven.plugins</groupId>
185+
<artifactId>maven-assembly-plugin</artifactId>
186+
<version>3.0.0</version>
187+
</dependency>
188+
189+
<!--添加插件 maven-assembly-plugin -->
190+
<plugin>
191+
<groupId>org.apache.maven.plugins</groupId>
192+
<artifactId>maven-assembly-plugin</artifactId>
193+
<version>2.4.1</version>
194+
<configuration>
195+
<!--打包时,包含所有依赖的jar包-->
196+
<descriptorRefs>
197+
<descriptorRef>jar-with-dependencies</descriptorRef>
198+
</descriptorRefs>
199+
</configuration>
200+
</plugin>
201+
```
202+
203+
然后还要用插件打包的方式:assembly:assembly
204+
205+
![图片](http://cdn.jayh.club/uPic/640-202212070848001469cC97D.png)
206+
207+
然后target目录下会多出一个包,带了一个后缀:jar-with-dependencies
208+
209+
```
210+
decryption-0.0.1-SNAPSHOT-jar-with-dependencies.jar
211+
```
212+
213+
这个包的文件的大小比较大,有 15.4 M。
214+
215+
发给第三方再次引入后,不再报错了。
216+
217+
218+
219+
我们来看下这个包里面有什么东西,在 META-INF/maven 目录下可以看到 commons-codec 依赖包,说明确实将这个依赖包打进去了。
220+
221+
![图片](http://cdn.jayh.club/uPic/640-20221207084800337BPutq9.png)
222+
223+
而之前打的包,是没有这个目录的。
224+
225+
![图片](http://cdn.jayh.club/uPic/640-202212070848005565ISrKf.png)
226+
227+
至此,排查结束。
228+
229+
参考资料:
230+
231+
https://blog.csdn.net/Shangxingya/article/details/114810454
232+
233+
https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
234+
235+
236+
237+
\- END -
238+
239+
## **关于我**
240+
241+
多年互联网开发经验,擅长微服务、分布式、架构设计。目前在一家大型上市公司从事基础架构和性能优化工作。
242+
243+
InfoQ 签约作者、蓝桥签约作者、阿里云专家博主、51CTO 红人。
244+
245+
欢迎加入悟空的私密星球,提供 PassJava项目答疑、微服务知识答疑、面试修改等。(下方扫码加入)
246+
247+
![](http://cdn.jayh.club/uPic/640-20221207084801113EYEiNR.png)
Collapse file

‎…ringBoot学习篇/08.SpringBoot-Mybatis逻辑删除.md‎ ‎….SpringBoot/08.SpringBoot-Mybatis逻辑删除.md‎docs/03.SpringBoot学习篇/08.SpringBoot-Mybatis逻辑删除.md renamed to docs/03.SpringBoot/08.SpringBoot-Mybatis逻辑删除.md docs/03.SpringBoot学习篇/08.SpringBoot-Mybatis逻辑删除.md renamed to docs/03.SpringBoot/08.SpringBoot-Mybatis逻辑删除.md

Copy file name to clipboard
  • Display the source diff
  • Display the rich diff
File renamed without changes.
Collapse file

‎docs/03.SpringBoot学习篇/09.Object划分.md‎ ‎docs/03.SpringBoot/09.Object划分.md‎docs/03.SpringBoot学习篇/09.Object划分.md renamed to docs/03.SpringBoot/09.Object划分.md

Copy file name to clipboard
  • Display the source diff
  • Display the rich diff
File renamed without changes.
Collapse file

‎docs/03.SpringBoot学习篇/09.Object划分md‎ ‎docs/03.SpringBoot/09.Object划分md‎docs/03.SpringBoot学习篇/09.Object划分md renamed to docs/03.SpringBoot/09.Object划分md

Copy file name to clipboard
File renamed without changes.
Collapse file

‎…pringBoot学习篇/spring-boot-05-data-jdbc.md‎ ‎…3.SpringBoot/spring-boot-05-data-jdbc.md‎docs/03.SpringBoot学习篇/spring-boot-05-data-jdbc.md renamed to docs/03.SpringBoot/spring-boot-05-data-jdbc.md docs/03.SpringBoot学习篇/spring-boot-05-data-jdbc.md renamed to docs/03.SpringBoot/spring-boot-05-data-jdbc.md

Copy file name to clipboard
  • Display the source diff
  • Display the rich diff
File renamed without changes.
Collapse file

‎…ringBoot学习篇/spring-boot-06-data-druid.md‎ ‎….SpringBoot/spring-boot-06-data-druid.md‎docs/03.SpringBoot学习篇/spring-boot-06-data-druid.md renamed to docs/03.SpringBoot/spring-boot-06-data-druid.md docs/03.SpringBoot学习篇/spring-boot-06-data-druid.md renamed to docs/03.SpringBoot/spring-boot-06-data-druid.md

Copy file name to clipboard
  • Display the source diff
  • Display the rich diff
File renamed without changes.
Collapse file

‎…ngBoot学习篇/spring-boot-07-data-mybatis.md‎ ‎…pringBoot/spring-boot-07-data-mybatis.md‎docs/03.SpringBoot学习篇/spring-boot-07-data-mybatis.md renamed to docs/03.SpringBoot/spring-boot-07-data-mybatis.md docs/03.SpringBoot学习篇/spring-boot-07-data-mybatis.md renamed to docs/03.SpringBoot/spring-boot-07-data-mybatis.md

Copy file name to clipboard
  • Display the source diff
  • Display the rich diff
File renamed without changes.
Collapse file

‎docs/05.Interview/03.Database/MySQL1.md‎

Copy file name to clipboardExpand all lines: docs/05.Interview/03.Database/MySQL1.md
+2Lines changed: 2 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ select 的数据列只用从索引中就能够取得,不必从数据表中读
128128

129129
![](http://cdn.jayh.club/blog/20210906/HkguNxMjBAlC.png?imageslim)
130130

131+
注意:8.0.13 可以使用跳跃索引,bc 和 c 可以使用到索引。
131132

133+
官网:https://dev.mysql.com/doc/refman/8.0/en/range-optimization.html#range-access-skip-scan%EF%BC%89%EF%BC%9A
132134

133135
## 七、说下 MySQL 回表?
134136

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.