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,终究不能深刻成系统的了解。仍是乖乖地把用户手册看起来吧😝

相关文章
相关标签/搜索