Maven - 将特定依赖关系及其传递依赖关系复制到给定位置

时间:2013-10-15 11:03:00

标签: maven dependency-management

我有一个maven项目,我说Spring框架库作为依赖项,我想将Spring框架依赖项与传递依赖项复制到指定的位置。

我已经浏览过apache的maven依赖插件指南,我有几个选项,其中没有它们将解决完整的问题。

  1. 复制依赖项选项
  2. <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>
    

    这会将所有依赖项和传递复制到给定位置,我只想要Spring依赖项和传递。

    1. 复制特定工件
    2. <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>
      

      这不是应对传递依赖。

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

3 个答案:

答案 0 :(得分:2)

程序集插件可以做到这一点。

插件配置:

     <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <descriptors>
                    <descriptor>src/assembly/assembly.xml</descriptor>
                </descriptors>
                <finalName>plugins</finalName> <!--folder name in target directory-->
            </configuration>

            <executions>
                <execution>
                    <id>some-id</id> <!-- must match assembly id in assembly.xml-->
                    <phase>pre-integration-test</phase> <!-- pic -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>

        </plugin>

assembly.xml

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">

    <id>some-id</id>
    <formats>
        <format>dir</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>

    <dependencySets>
        <dependencySet>
            <includes>
                <include>
                    org.springframework:spring-web
                </include>
            </includes>
            <useTransitiveDependencies>true</useTransitiveDependencies>
            <useTransitiveFiltering>true</useTransitiveFiltering>
        </dependencySet>
    </dependencySets>

</assembly>

重要的位是<useTransitiveDependencies>true</useTransitiveDependencies><useTransitiveFiltering>true</useTransitiveFiltering>,这导致include应用于项目依赖项,而不应用于传递性依赖项,从而导致 spring-web工件和它是依赖项,将其复制到目录中。

答案 1 :(得分:0)

您可以使用maven程序集插件。

检查出来,特别是依赖集:

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

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

您可以提供输出目录,并可以指定要放在哪个依赖项

还有一个选项:useTransitiveDependencies。将此设置为true可获得所需的行为。

答案 2 :(得分:0)

这是一个选项:

  • 创建模块(生产者)以收集依赖关系并将其作为zip发布。
  • 消费者用户依赖性中的
  • :解压缩解压缩

这很麻烦,排除仍然需要一些樱桃采摘,但更少,它可以在并行线程中运行。

<强>生产者

<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>