前言
今天碰到一个需求,需要在项目中,引入一个其他项目的SDK包,这个包大概有几十个类文件,如果全部复制进入项目,需要修改的工作量很大,所以考虑打成jar包并引入。
介绍
- 需要打包的项目,以及需要引入的项目均为maven项目
- 工具使用Idea
打包jar
- 首先,如果需要打包成jar,必须有一个Main入口类,如果项目没有,记得建立一个,里面只要包含main方法即可。

image.png
- 然后点击项目,在上面菜单处选择 File--Project Structure...

image.png
- 在左侧选择Artifacts,然后点击+

image.png

image.png
- Main Class 选择 你写的main类,然后点击OK即可

image.png
- 如图,把勾勾打上,然后点击OK

image.png
6.点击菜单 Build --Build Project

image.png
然后在项目的根目录 classes --artifacts 目录即可找到生成的Jar包
引入 jar包
如果需要在另外一个项目中,引入这个jar包,那么,需要如下操作
- 创建目录
在项目的src平级目录新建文件夹 lib (这一步名称随意) - 将刚才生成好的jar包放入该文件夹下。
3.将该文件引入项目
菜单处选择 File--Project Structure...
然后 左侧选择Modules点击 右侧+,如图。

image.png

image.png
- 找到项目目录下的jar包,导入

image.png
- 在pom中引入
注意这里的 directory要和刚才新建的文件目录相同
<build>
<resources>
<resource>
<directory>lib</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<targetPath>BOOT-INF/classes/</targetPath>
</resource>
</resources>
</build>
以上方法会导致问题,最好用
<dependency>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>v0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/xxx.jar</systemPath>
</dependency>
其中xx 随意,为包名
6.结束
然后就可以在项目中使用引入的jar中的方法了。
小礼物走一走,来简书关注我
作者:sT丶
链接:https://www.jianshu.com/p/257dcca702f7
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。