Maven - Copy specific dependency with its transitive dependencies to a give location

北慕城南 提交于 2019-12-05 18:32:37

问题


I have a maven project which I have say spring framework libraries as dependencies, I want to copy spring framework dependencies with there transitive dependencies to a location specified.

I have gone through maven dependency plugin guides at apache, I have several options where non of them will solve the complete problem.

  1. copy dependencies option
<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>

This will copy all the dependencies and there transitives to a given location, I want only spring dependencies and there transitives.

  1. copying specific artifacts
<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>

This is not coping the transitive dependencies.

Any solution which solve my both problems.


回答1:


This is possible with the assembly plugin.

Plugin configuration:

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

The important bits are <useTransitiveDependencies>true</useTransitiveDependencies> and <useTransitiveFiltering>true</useTransitiveFiltering>, which cause the include to be applied to project dependencies, but not to transitive dependencies, resulting in spring-web artifact and it's dependencies to be copied to the directory.




回答2:


You can use the maven assembly plugin for this.

Check it out and specifically the dependency set:

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

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

You can provide an output directory and you can specify which dependencies to put in there

There is also an option: useTransitiveDependencies. Set this to true to get the behaviour you want.




回答3:


Here's an option:

  • create module (producer) to collect dependencies and publish them as a zip.
  • in consumer user depencency:unpack to unpack that zip

It is cumbersome and the exclusions still need some cherry picking, but much less and it could be run in parallel threads.

Producer

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


来源:https://stackoverflow.com/questions/19379613/maven-copy-specific-dependency-with-its-transitive-dependencies-to-a-give-loca

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!