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

问题描述 投票:0回答:4

我有一个 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>

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

  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
4个回答
10
投票

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

插件配置:

     <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 工件及其依赖项 被复制到目录中。


0
投票

你可以为此使用 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
投票

这里有一个选项:

  • 创建模块(生产者)以收集依赖项并将它们发布为 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>

0
投票

maven 依赖插件从 2.0 版本开始在

includeGroupIds
目标中包含一个名为
copy-dependencies
的选项。使用该选项(和其他包含选项),您可以仅复制要复制的工件并包含传递依赖项:

<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>
        <includeGroupIds>org.springframework</includeGroupIds>
      </configuration>
    </execution>
  </executions>
</plugin>
© www.soinside.com 2019 - 2024. All rights reserved.