一、引入第三方jar包
- 首先选中Project Structure模块
- 按以下操作导入jar包至libraries库中,即可使用jar包内容
二、打包项目
- 导入到libraries中后,执行mvn clean install打包是会报错的,此时我们需要把jar包引入到本地maven库中,并添加相应依赖
- 执行以下操作加载jar包
mvn install:install-file -Dfile=Jar包所在位置 -DgroupId=jar包所指的groupid -DartifactId=jar包所指的artifactId -Dversion=版本号 -Dpackaging=jar
- 执行完成后在pom中引入相关依赖
至此,我们可以进行正常打包。
打包完成。
三、运行jar包
执行java -jar xxx.jar
发现报主清单属性错而无法运行,此时我们可以通过可以自己指定main,告知springboot项目主启动类的位置
<build>
<finalName>test</finalName><!-- 导出jar的名字 -->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.0.RELEASE</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>testdemo.demo.DemoApplication</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
- 再次打包后运行
java -jar xxx.jar
出现了Error: A JNI error has occurred, please check your installation and try again
错误
这是由于Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
异常引起的
如果出现此错误是因为我们的打包方式没有生成META-INF这个文件夹,针对这种错误我们需要在Project Structure中进行更改,快捷键Ctrl+Alt+Shift+S,然后进行以下操作
JAR from Modules有两个选项,extract to the target JAR和copy to the output directory and link via manifest,
extract to the target JAR:将第三方jar包打包到目标中,只生成一个jar包
copy to the output directoru and link via manfes:将第三方包copy的目标jar包目录,有多个jar包
META-INF文件位置在src目录下,或者与src同级目录均可
设置好之后再通过Build工具进行下述操作
等待执行完毕,进入项目的out/artifacts目录中再次运行jar包
至此jar包成功运行