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

后端 未结 3 1536
北恋
北恋 2020-12-17 18:17

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 loca

相关标签:
3条回答
  • 2020-12-17 19:04

    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.

    0 讨论(0)
  • 2020-12-17 19:14

    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.

    0 讨论(0)
  • 2020-12-17 19:15

    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>
    
    0 讨论(0)
提交回复
热议问题