在maven项目中使用本地jar包的4种方法

本文讲述在maven项目中使用本地jar包的4种方法:

手动添加jar到本地maven库

第一种方法是使用mvn命令将jar包添加到本地方法库,方法如下:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version>

参数如下:

  • <path-to-file>: Path to the JAR to install
  • <group-id>: Group id of the JAR to install
  • <artifact-id>: Artifact id of the JAR to install
  • <version>: Version of the JAR
    例子如下:
mvn install:install-file -DgroupId=com.example  -DartifactId=auth -Dversion=1.0.0 -Dpackaging=jar -Dfile=Athena-1.0.0-jar-with-dependencies.jar

使用 maven-install-plugin

这个方法是在pom.xml中使用maven-install-plugin,在“initialize”阶段安装jar包,可以把jar包放在一个指定的路径下,例子如下:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                        <configuration>
                            <packaging>jar</packaging>
                            <artifactId>Athena</artifactId>
                            <groupId>com.example.zodiac</groupId>
                            <version>1.0.0</version>
                            <file>${basedir}/lib/Athena-1.0.0-jar-with-dependencies.jar</file>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>

    </build>

经过各种尝试,发现一种情况:在一个pom.xml文件中使用maven-install-plugin的同时定义对该jar包的依赖,这种方式编译无法通过。
变通方式是在root module的pom.xml中使用maven-install-plugin,然后在子module中使用dependency,例子如下:

        <dependency>
            <groupId>com.example.zodiac</groupId>
            <artifactId>Athena</artifactId>
            <version>1.0.0</version>
            <type>jar</type>
        </dependency>

在dependency中使用system scope

这种方法比较简单,直接上例子:

      <dependency>
            <groupId>com.example.zodiac</groupId>
            <artifactId>Athena</artifactId>
            <version>1.0.0</version>
            <type>jar</type>
           <scope>system</scope>
          <systemPath>${basedir}/lib/Athena-1.0.0-jar-with-dependencies.jar</systemPath>
        </dependency>

创建一个本地maven库

这种方法简单粗暴,在pom.xml文件中定义一个新的maven库,然后根据maven的存储方式将jar包放在指定的路径下:

<repositories>
        <repository>
            <id>local</id>
            <name>local</name>
            <url>file:///${pom.basedir}/lib/</url>
            <layout>default</layout>
            <snapshots><enabled>false</enabled></snapshots>
        </repository>
    </repositories>

然后将jar包放在路径:${pom.basedir}/lib/com/example/zodiac/Athena/1.0.0/Athena-1.0.0-.jar
ps:由于Athena-1.0.0-jar-with-dependencies.jar命名不符合maven的规范,所以需要将Athena-1.0.0-jar-with-dependencies.jar重命名为Athena-1.0.0-.jar或者修改依赖jar的version为:1.0.0-jar-with-dependencies

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,973评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,970评论 6 342
  • 简介 概述 Maven 是一个项目管理和整合工具 Maven 为开发者提供了一套完整的构建生命周期框架 Maven...
    闽越布衣阅读 4,346评论 6 39
  • 一个真正的男人注定要经历一场孤独的战争,不需要理解,不需要安慰,受了伤用舌头把血迹舔干,继续前行。 凡是渴望被理解...
    悟道修行阅读 409评论 0 1
  • 无欲而为阅读 190评论 0 0