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

相關文章
相關標籤/搜索