前言
当我们使用maven时,偶有遇到需要引用不在maven repo中的jar包情况,此时一般的做法是在项目根目录下建立lib文件夹,然后copy jar到lib下,右键lib -> Add as library
然后即可在项目中引用,但是仅仅这样是无法在jar中使用的,打成jar包后便会遇到NoClassFound 或者ClassNotFound的错误。本文便介绍三种方式引用外部jar包,并打包进可执行jar文件。
参考:
解决方法install to local repo - 将jar install 到本地mvn repo,
不推荐原因:安装在本地repo中的gav,可能在远程仓库不存在,如果忘记了在远程服务器上执行时,可能会导致错误
Why you shouldn't apply the "Install to Local Repo" approach
When you install a dependency to your local repository it remains there. Your distribution artifact will do fine as long as it has access to this repository. The problem is in most cases this repository will reside on your local machine, so there'll be no way to resolve this dependency on any other machine. Clearly making your artifact depend on a specific machine is not a way to handle things. Otherwise this dependency will have to be locally installed on every machine working with that project which is not any better.
- gav + system scope + spring-boot-maven-plugin includeSystemScope
<dependencies>
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>core</artifactId>
<version>0.3.0</version>
<scope>system</scope>
<systemPath>${pom.basedir}/lib/utils-0.3.0.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- main 方法入口 -->
<mainClass>com.xx.xx.xx</mainClass>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>