maven添加本地包依赖的两种方式

发布时间 2024-01-08 15:17:31作者: 錒飛

maven添加本地包依赖
1、通过 <scope>system</scope> 的依赖方式引入

2、通过mvn install 命令将依赖包添加到本地maven仓库

 

1、<scope>system</scope> 的依赖方式引入
比如项目目录下有lib/errorWordsCore-1.0.jar
需求:maven项目需要引入本地一个jar包。并且打包后需要把该文件引入依赖。

比如项目目录下有lib/errorWordsCore-1.0.jar
将jar包引入pom.xml

        <dependency>
           <groupId>com.hugedata.core</groupId>
            <artifactId>errorword</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/errorWordsCore-1.0.jar</systemPath>
        </dependency>

scope 为system。此时必须提供systemPath即本地依赖路径。表示maven不会去中央仓库查找依赖。要注意的是这个范围是不推荐使用的。

将jar包加入classpth,同样在pom.xml。 打包时把errorWordsCore-1.0.jar一并打包到lib。在assembly的xml文件中,表示把pom范围是system的依赖也打包到lib目录

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.albert.Application</mainClass>
                        <classpathPrefix>lib/</classpathPrefix>
                        <addClasspath>true</addClasspath>
                    </manifest>
                    <manifestEntries>
                        <!-- 在Class-Path下添加配置文件的路径 -->
                        <Class-Path>lib/my-dep-1.0.jar</Class-Path>         
                        <!--这里表示jar路径加入到MANIFEST.MF-->              
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>

 


注意:有时打出来的包里面没有本地包,不知道什么原因,打包后最后打开查看一下

2、mvn install 命令将依赖包添加到本地maven仓库
(1)添加到本地maven仓库

mvn install:install-file -DgroupId=com.fnii.core -DartifactId=errorWordsCore -Dversion=1.0 -Dpackaging=jar -Dfile=errorWordsCore-1.0.jar

DgroupId:组名,就是自己的包名

DartifactId

Dversion:版本

Dfile:指定文件,这里是指定当前目录下

 

 

(2)maven的pom文件中正常添加依赖


发现在pom.xml文件里面可以完美的依赖了