maven引用外部jar包 - import external lib/jars using maven

前言

当我们使用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>
maven-assembly-plugin (待完成)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,403评论 19 139
  • 开篇点题 Maven是项目管理工具,主要对项目进行管理,通过安装和配置maven,就可以在项目中使用了; 构建:何...
    啊勤小伙子阅读 1,755评论 0 0
  • Maven简介 官网:http://maven.apache.org/[http://maven.apache.o...
    苦难_69e0阅读 4,017评论 0 3
  • 简介 概述 Maven 是一个项目管理和整合工具 Maven 为开发者提供了一套完整的构建生命周期框架 Maven...
    闽越布衣阅读 9,810评论 6 39
  • 逅弈 转载请注明原创出处,谢谢! 以前的日子 以前我们写代码时,jar包都默认放在一个叫 /lib 的目录下,然后...
    逅弈阅读 8,097评论 3 45