Maven使用本地jar文件的两种方法

方法一:

将本地的jar文件安装到本地repository中,具体步骤如下:
1、准备好本地的jar文件
2、使用如下命令安装jar文件
      mvn install:install-file -Dfile=abc.jar
     -DgroupId=com.mycompany.myproduct -DartifactId=abc
     -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true 
    其中,-Dfile制定jar文件的位置。执行完该命令后,会在本地repository(通常是$HOME/.m2目录)中出现一个目录(com),在其下会有一个abc-1.0.jar的文件和一个自动产生的pom文件。
3、安装完之后,在另一个应用程序中使用它的时候,只要在pom文件中指定相应的dependency就可以,如:
    

<dependencies>
          <dependency>
          <groupId>com.mycompany.myproduct</groupId>
          <artifactId>abc</artifactId>
          <version>1.0</version>
       </dependency>
</dependencies>

方法二:

该方法与前面的方法不同之处在于它不会安装任何库文件,只是在应用程序中的pom文件中指定jar文件的目录
即可,在pom中的dependency中如下设置:

    

<dependencies>
          <dependency>
          <groupId>com.mycompany.myproduct</groupId>
          <artifactId>abc</artifactId>
          <version>1.0</version>
          <scope>system</scope>
          <systemPath>/home/root/abc.jar</systemPath>
       </dependency>
</dependencies>
 

其中的scope被设置成system,默认是compile,指出dependency不是去repository中查找,而是在系统目录
中查找。systemPath指定了jar文件的绝对路径。

也可以把jar放到项目路径下

<dependency>
          <groupId>jackson-core</groupId>
          <artifactId>jackson-core</artifactId>
          <version>2.0</version>
          <scope>system</scope>
          <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/jackson-core-asl-1.8.1.jar</systemPath>
</dependency>
 

猜你喜欢

转载自liyonghui160com.iteye.com/blog/2090351