maven依賴包和依賴倉庫(3)

話題一:如何將第三方依賴包或者本地依賴包導入jar文件中?

能夠參考這篇博客http://blog.csdn.net/wangjian1204/article/details/54563988。html

這裏,我根據本身的想法寫點。java

有三種方法:apache

1、使用maven install 添加到本地倉庫(local repository)bootstrap

這個是最值得推薦的。app

當遇到maven依賴包和依賴倉庫(1)中的問題,maven install 會根據依賴包的pom.xml下載該依賴包所須要的其餘依賴包(這個是猜想,80%,尚未測試過)maven

2、將依賴包添加到項目的一個文件夾中測試

若是將依賴包放到項目的repo文件夾中,須要在根pom.xml中說明ui

<repositories>
    <repository>
        <id>repo.local</id>
        <name>name</name>
        <url>file:${project.basedir}/repo</url> 
  </repository>
</repositories>

添加這段代碼後,maven便會在項目的repo文件下尋找。google

至於dependency的寫法,同樣的。url

3、使用dependency的system scope

首先,將依賴包存放在一個文件夾中。而後,在編寫dependency時,使用system scope,並添加絕對路徑(Maven needs absolute paths )

<dependency>
            <groupId>a.b.c</groupId>
            <artifactId>def</artifactId>
            <version>0.0.1</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/repo/a/b/c/def/0.0.1/def-0.0.1.jar</systemPath>
</dependency>

 

 

話題二:如何將第三方依賴包打包到jar文件中 

關於上面三種方法,若是須要在其餘機器上運行該jar可執行文件,須要將其打包到jar中。至於第三種方法,不是很容易打包到jar中,不推薦。

所以,強烈推薦第一個方法,再次第二種方法。

 

參考https://stackoverflow.com/questions/364114/can-i-add-jars-to-maven-2-build-classpath-without-installing-them/7748177#7748177

連接中詳細說明了如何實現第二種方法。可是,鏈接中指出第一種方法的可執行文件在其餘機器上不能工做。這個有待商榷。

 

話題一中的第一種方法如何打包到jar文件中,參考GATK.3.8.

        <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
<execution> <id>unpack-direct-dependencies</id> <goals> <goal>unpack-dependencies</goal> </goals> <phase>none</phase> <!-- NOTE: Shade include filters do NOT include transient dependencies, only the class directly listed. Thus to fully include all the classes in packages, we must: 1) List the artifacts we want shaded as direct dependencies 2) Run the unpack direct dependencies into the classes directory 3) Shade the classes directory as we normally would. --> <configuration> <excludeTransitive>true</excludeTransitive> <outputDirectory>${project.build.outputDirectory}</outputDirectory> <includeTypes>jar</includeTypes> <includeScope>runtime</includeScope> <!-- Don't unjar the resource bundle, so that shade's AppendingTransformer can merge --> <excludes>${resource.bundle.path}</excludes> </configuration> </execution>
<execution> <id>executable-jar-lib</id> <goals> <goal>copy-dependencies</goal> </goals> <phase>none</phase> <configuration> <outputDirectory>${gatk.executable.directory}/lib</outputDirectory> <includeScope>runtime</includeScope> <useBaseVersion>false</useBaseVersion> </configuration> </execution> </executions> </plugin>

 話題一中的第一種方法和第二種方法應該均可以打包到一個文件中的方法:

<build>
    <plugins>
      <!-- any other plugins -->
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
  </build>

這裏有綜述:

There are three common methods for constructing an uber-JAR:

1.Unshaded. Unpack all JAR files, then repack them into a single JAR.
  Pro: Works with Java's default class loader.
  Con: Files present in multiple JAR files with the same path (e.g., META-INF/services/javax.script.ScriptEngineFactory) will overwrite one another, resulting in faulty behavior.
  Tools: Maven Assembly Plugin, Classworlds Uberjar
2.Shaded. Same as unshaded, but rename (i.e., "shade") all packages of all dependencies.(有點像GATK)
  Pro: Works with Java's default class loader. Avoids some (not all) dependency version clashes.
  Con: Files present in multiple JAR files with the same path (e.g., META-INF/services/javax.script.ScriptEngineFactory) will overwrite one another, resulting in faulty behavior.
  Tools: Maven Shade Plugin
3.JAR of JARs. The final JAR file contains the other JAR files embedded within.
  Pro: Avoids dependency version clashes. All resource files are preserved.
  Con: Needs to bundle a special "bootstrap" classloader to enable Java to load classes from the wrapped JAR files. Debugging class loader issues becomes more complex.
  Tools: Eclipse JAR File Exporter, One-JAR.

 

參見連接:https://stackoverflow.com/questions/1729054/including-dependencies-in-a-jar-with-maven/1729094#1729094

 

關於各類maven的各類插件和插件的相關信息,好比id, goals, executions 和configuration我還須要仔細看用戶手冊。

每次遇到問題就google,終究不能深刻成系統的瞭解。仍是乖乖地把用戶手冊看起來吧😝

相關文章
相關標籤/搜索