2013-10-15 70 views
9

我有一个maven项目,我将Spring框架库称为依赖关系,我想将弹性框架依赖关系与传递依赖关系复制到指定位置。Maven - 将特定的依赖关系及其传递依赖关系复制到给定位置

我已经通过在Apache的Maven依赖插件指南,我有几个选项,其中不会解决完整的问题。

  1. 副本相依选项
<plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-dependency-plugin</artifactId> 
     <version>2.8</version> 
     <executions> 
      <execution> 
      <id>copy-dependencies</id> 
      <phase>package</phase> 
      <goals> 
       <goal>copy-dependencies</goal> 
      </goals> 
      <configuration> 
       <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory> 
       <overWriteReleases>false</overWriteReleases> 
       <overWriteSnapshots>false</overWriteSnapshots> 
       <overWriteIfNewer>true</overWriteIfNewer> 
      </configuration> 
      </execution> 
     </executions> 
     </plugin> 

这将复制所有的依赖关系,并有transitives给定的位置,我只想要Spring的依赖,有transitives。

  1. 复制特定的文物
<plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-dependency-plugin</artifactId> 
     <version>2.8</version> 
     <executions> 
      <execution> 
      <id>copy</id> 
      <phase>package</phase> 
      <goals> 
       <goal>copy</goal> 
      </goals> 
      <configuration> 
       <artifactItems> 
       <artifactItem> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-web</artifactId> 
        <version>3.2.4.RELEASE</version> 
        <type>jar</type> 
        <overWrite>false</overWrite>     <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory> 
        <destFileName>optional-new-name.jar</destFileName> 
       </artifactItem> 
       </artifactItems> 
       <outputDirectory>${project.build.directory}/wars</outputDirectory> 
       <overWriteReleases>false</overWriteReleases> 
       <overWriteSnapshots>true</overWriteSnapshots> 
      </configuration> 
      </execution> 
     </executions> 
     </plugin> 

这不是应对的传递依赖。

任何解决我的两个问题的解决方案。

+0

这可能是最好使用[复制依赖性(http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html )目标而不是复制目标。 – khmarbaise

回答

-1

你可以使用maven assembly插件。

检查出来并专门依赖集:

http://maven.apache.org/plugins/maven-assembly-plugin/

http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_dependencySet

可以提供输出目录,你可以指定哪些依赖关系就摆在那里

还有一个选项:useTransitiveDependencies。将其设置为true以获得您想要的行为。

+0

-1,因为与此相关的'maven-assembly-plugin'表现完全一样。如果仅在'dependencySet'中仅包含'org.springframework:spring-web'构件,则不管设置了useTransitiveDependencies,只有它会被复制,因为它的所有传递依赖项都不包含在白名单中。这非常烦人。 –

+0

我感到你的痛苦。我认为唯一可行的方法是创建一个子项目或另一个模块,它仅依赖于您想要的项目,然后生成一个包含其deps + transitive的zip。 –

0

这里有一个选项:

  • 创建模块(制片人)收集的依赖,并把它们发布的拉链。
  • 消费用户depencency:解包解压该邮政编码

这是繁琐和排除仍然需要一些采摘樱桃,但要少得多,它可以并行的线程运行。

生产者

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>packaging</groupId> 
    <artifactId>jdbcdrivers</artifactId> 
    <packaging>zip</packaging> 
    <name>jdbcdrivers</name> 
    <dependencies> 
<!-- set up deps for capture and limit their impact on anything which depends upon this module via optional-false --> 
<dependency> 
    <groupId>net.sf.jtds</groupId> 
    <artifactId>jtds</artifactId> 
    <scope>test</scope> 
    <optional>false</optional> 
</dependency> 
<dependency> 
    <groupId>org.apache.hive</groupId> 
    <artifactId>hive-jdbc</artifactId> 
    <scope>test</scope> 
    <optional>false</optional> 
</dependency> 
<dependency> 
    <groupId>postgresql</groupId> 
    <artifactId>postgresql</artifactId> 
    <scope>test</scope> 
    <optional>false</optional> 
</dependency> 

    </dependencies> 

    <build>  
<plugins> 
    <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-assembly-plugin</artifactId> 
    </plugin> 

    <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-antrun-plugin</artifactId> 
    </plugin> 
    <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-dependency-plugin</artifactId> 
    <executions> 
     <execution> 
     <id>dist profile: hive jdbc driver ${baseName}</id> 
     <phase>prepare-package</phase> 
     <goals> 
      <goal>copy-dependencies</goal> 
     </goals> 
     <configuration> 
      <outputDirectory>${project.build.outputDirectory}/lib/addons/jdbcdriver/</outputDirectory> 
      <useBaseVersion>true</useBaseVersion> 
      <excludeTransitive>false</excludeTransitive> 
      <overWriteIfNewer>true</overWriteIfNewer> 
      <includeScope>test</includeScope> 
      <excludeScope>provided</excludeScope> 
      <excludeGroupIds>org.codehaus.groovy,org.apache.ivy,jdk.tools</excludeGroupIds> <!-- you might need to cherry pick excludes if scope doesn't worjk --> 
      <prependGroupId>true</prependGroupId> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 
</plugins> 
    </build> 
</project> 
+0

这是应该开箱即用吗?我得到'未知的包装:zip' – qqilihq

+0

我当然这么认为。在我尝试使用这个maven之后的两年内,可能会取消zip。 Zip不保留权限,所以它不如tar好(当涉及到java时,它不一定保留权限) –