12

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

这不应对传递依赖。

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

4

3 回答 3

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>

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

于 2018-09-13T13:16:45.223 回答
0

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

检查它,特别是依赖集:

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

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

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

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

于 2013-10-15T12:46:33.140 回答
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>
于 2015-09-18T21:50:58.740 回答