【问题标题】:Maven - Copy specific dependency with its transitive dependencies to a give locationMaven - 将特定依赖项及其传递依赖项复制到给定位置
【发布时间】:2013-10-15 11:03:00
【问题描述】:

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

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

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

  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>

这不应对传递依赖。

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

【问题讨论】:

标签: maven dependency-management


【解决方案1】:

这可以通过程序集插件实现。

插件配置:

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

程序集.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>

重要的位是&lt;useTransitiveDependencies&gt;true&lt;/useTransitiveDependencies&gt;和&lt;useTransitiveFiltering&gt;true&lt;/useTransitiveFiltering&gt;,这导致include应用于项目依赖,而不是传递依赖,导致spring-web工件及其依赖复制到目录中。

【讨论】:

    【解决方案2】:

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

    检查一下,特别是依赖集:

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

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

    你可以提供一个输出目录,你可以指定放在那里的依赖项

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

    【讨论】:

    • -1 因为 maven-assembly-plugin 对此的行为完全相同。当仅在dependencySet 中包含org.springframework:spring-web 工件时,就像建议的操作一样,无论设置useTransitiveDependencies,都只会复制它,因为它的所有传递依赖项都不包含在白名单中。这很烦人。
    • 我感受到你的痛苦。我认为唯一可行的方法是创建一个仅依赖于您想要的项目的子项目或另一个模块,然后让它生成一个包含其 deps + 传递的 zip。
    【解决方案3】:

    这里有一个选项:

    • 创建模块(生产者)以收集依赖项并将其发布为 zip。
    • 在消费者用户依赖中:解压缩以解压缩该 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>
    

    【讨论】:

    • 这应该是开箱即用的吗?我收到Unknown packaging: zip
    • 我确实这么认为。自从我尝试这个 maven 取消 zip 以来,有可能在 2 年内。 Zip 不保留权限,因此不如 tar 好(涉及 java 时也不一定保留权限)
    猜你喜欢
    • 1970-01-01
    • 2020-08-06
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多