Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 852
Isuru Avatar asked Oct 15 '13 11:10

Isuru


People also ask

Where can I find Maven transitive dependencies?

You can get this information in the Maven Tool Window. First go to View → Tool Windows → Maven, to make sure that the Maven window is visible. The top-level elements in the tree are your direct dependencies, and the child elements are the transitive dependencies.

How does Maven resolve transitive dependencies?

Transitive Dependencies. Maven avoids the need to discover and specify the libraries that your own dependencies require by including transitive dependencies automatically. This feature is facilitated by reading the project files of your dependencies from the remote repositories specified.

How do you exclude all transitive dependencies of a Maven dependency?

Exclude the transitive dependencyOpen the dependency POM and find the transitive dependency you want to exclude. Copy groupId and artifactId . In your project POM, underneath your active dependency, enter exclusions and using code completion paste the copied info of the dependency you want to exclude.


1 Answers

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.

like image 107
František Hartman Avatar answered Oct 13 '22 20:10

František Hartman