向maven仓库中手动安装本地jar的三种方法


在Maven项目中使用本地JAR包有三种方法:

1. 使用system scope

复制代码
  <dependencies>
    <dependency>
      <groupId>org.richard</groupId>
      <artifactId>my-jar</artifactId>
      <version>1.0</version>
      <scope>system</scope>
      <systemPath>${project.basedir}/lib/my-jar.jar</systemPath>
    </dependency>
  </dependencies>
复制代码

system scope引入的包,在使用jar-with-dependencies打包时将不会被包含,可以使用resources将本地包打进jar-with-dependencies

复制代码
<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
              <descriptorRefs>
<configuration> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <finalName>xxx-jar-with-dependencies</finalName> </configuration> </execution> </executions> </plugin> </plugins> <resources> <resource> <targetPath>lib/</targetPath> <directory>lib/</directory> <includes> <include>**/my-jar.jar</include> </includes> </resource> </resources> </build>
复制代码

生成的xxx-jar-with-dependencies.jar中,将会包含lib目录以及my-jar.jar,并且能够被在执行的时候被找到。

有的时候这种方法会实效,比如JDBCDriver在声明的时候Class.forName("xxx.Driver")就会说找不到类,用下面两种方法就可以。


2. 将jar包安装到本地repository中

mvn install:install-file -Dfile=my-jar.jar -DgroupId=org.richard -DartifactId=my-jar -Dversion=1.0 -Dpackaging=jar

http://mvnrepository.com/中找到对应的jar:

以 spring-context-support-3.1.0.RELEASE.jar 为例,在 @3图中已经给出这个 jar 包的 groupId,artifactId,version信息,

手动安装的时候这些信息不要改,否则 Maven 项目移植的话,jar 包下载就会失败。顺便把这信息帖下面,方便对照:

< dependency >      < groupId > org.springframework </ groupId >      < artifactId > spring-context-support </ artifactId >      < version > 3.1.0.RELEASE </ version > </ dependency >

Maven 安装 JAR 包的命令是:

mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -DartifactId=上面的artifactId -Dversion=上面的version -Dpackaging=jar

 3. 添加 in project repository

在新机器上执行时就不用运行mvn install:install-file命令了

复制代码
<repository>
    <id>in-project</id>
    <name>In Project Repo</name>
    <url>file://${project.basedir}/lib</url>
</repository>

<dependency>
    <groupId>org.richard</groupId>
    <artifactId>my-jar</artifactId>
    <version>1.0</version>
</dependency>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值