1. 打包jar
1.1. 首先,如果需要打包成jar,必须有一个Main入口类,如果项目没有,记得建立一个,里面只要包含main方法即可。

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

1.3. 在左侧选择Artifacts,然后点击+


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

1.5. 如图,把勾勾打上,然后点击OK

1.6.点击菜单 Build --Build Project

1.7.然后在项目的out文件夹下的artifacts 目录即可找到生成的Jar包
2.引入 jar包
如果需要在另外一个项目中,引入这个jar包,那么,需要如下操作
2.1. 创建目录
在项目的src平级目录新建文件夹 lib (这一步名称随意)
2.2. 将刚才生成好的jar包放入该文件夹下。

2. 3.将该文件引入项目
菜单处选择 File--Project Structure...
然后 左侧选择Modules点击 右侧+,如图。


2. 4. 找到项目目录下的jar包,导入

2. 5. 在pom中引入(非maven项目的忽略这一步)
注意这里的 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 随意,为包名
2. 6.结束
然后就可以在项目中使用引入的jar中的方法了。
网友评论