maven引用本地jar

    在使用maven进行jar包管理时,经过咱们都是经过maven去下载一些jar包,但有些jar在maven上没有,因此就就可能在本地直接手动加入一些须要用到的外部jar包。但若是咱们用maven package打包就会发现,本地的那些jar是不能被maven识别的,因此就须要解决Maven关于本地jar包的打包处理的问题。java

1. 使用system scopeapache

<dependencies>
    <dependency>
      <groupId>org.sky</groupId>
      <artifactId>my-jar</artifactId>
      <version>1.0</version>
      <scope>system</scope>
   <systemPath>e:\my-jar.jar</systemPath>
    </dependency>
  </dependencies>

编译后出现一个警告:maven

[WARNING] 'dependencies.dependency.systemPath' for org.sky:my-jar:jar should use a variable instead of a hard-coded path e:\my-jar.jar @ line 35, column 16
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.ui

 

原来是 maven 苦口婆心地告诉咱们不要使用诸如 C:\Windows 这样的绝对路径,而应使用 ${java.home} 这样的相对路径(变量),不然下降了编译可重现性,并威胁咱们之后可能再也不支持这种方式。this

后来改为<systemPath>${project.basedir}/lib/my-jar.jar</systemPath>, 结果说找不到z这个jar,无解了。url

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

<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>
            <configuration>
              <descriptorRefs>
                <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,而且可以被在执行的时候被找到。我反正没试过。还有看到网上说添加下面一段。orm

<build> 
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                    <compilerArguments>
                        <extdirs>project-demo\lib</extdirs>
                    </compilerArguments>
                </configuration>
            </plugin>
         </plugins>       
    
</build>

2.使用指令 mvn install:install-file 将jar文件安装到本地仓库ip

语法规范:ci

mvn install:install-file  -Dfile=<path-to-file>  -DgroupId=<group-id>  -DartifactId=<artifact-id>  -Dversion=<version>  -Dpackaging=<packaging>  -DgeneratePom=true  Where: <path-to-file> the path to the file to load     <group-id>   the group that the file should be registered under     <artifact-id>  the artifact name for the file     <version>    the version of the file     <packaging>   the packaging of the file e.g. jar

mvn install:install-file -Dfile=e:\ojdbc6.jar -DgroupId=ojdbc -DartifactId=ojdbc -Dversion=6 -Dpackaging=jar -DgeneratePom=true

 

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>

你的jar包及路径必须严格遵循格式:

/groupId/artifactId/version/artifactId-verion.jar

本例中: lib/org/richard/my-jar/1.0/my-jar-1.0.jar

 

参考地址:http://stackoverflow.com/questions/3642023/having-a-3rd-party-jar-included-in-maven-shaded-jar-without-adding-it-to-local-r

相关文章
相关标签/搜索