Filter dependencies copied by Maven's copy-dependency?

Naftuli Kay picture Naftuli Kay · May 1, 2011 · Viewed 16.6k times · Source

I need to essentially accomplish the following:

  1. Build my library into a JAR. (Easy, already done.)
  2. Copy my library's dependencies to a local folder, including the main project JAR, excluding dependencies marked as provided.

I can't seem to get the second part finished. Is there a better way to do this than how I'm doing it below? I'm essentially deploying these JARs to a lib directory on a server. Unfortunately, the code below includes all JARs, even provided ones, but doesn't include the project output JAR. Should I be using a different plugin for this?

<?xml version="1.0"?>
<project>
    ...

    <dependencies>
        <dependency>
            <groupId>com.provided</groupId>
            <artifactId>provided-lib</artifactId>
            <version>1.2.3</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.1</version>
        </dependency>

        ...

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>/hello</outputDirectory>
                            <excludeTransitive>true</excludeTransitive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Answer

FrVaBe picture FrVaBe · May 2, 2011

To prevent the pluging to collect provided dependencies you can use @Raghuram solution (+1 for that). I tried also to skip test scoped dependencies and found the issue that it can not be done that simple - as test means 'everything' in the plugin semantic.

So the solution to exclude provided and test scope is to includeScope runtime.

<includeScope>runtime</includeScope>

After collecting the dependencies you can copy the projects jar with the maven-antrun-plugin to the target directory, e.g.:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${java.io.tmpdir}/test</outputDirectory>
                        <includeScope>runtime</includeScope>                        
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <configuration>
                        <tasks>
                        <copy
                            file="${build.directory}/${project.artifactId}-${project.version}.jar"
                            todir="${java.io.tmpdir}/test" />
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I do not know any other solution - beside creating a new pom-dist.xml (maybe <packaging>pom</packaging>) which just holds the dependency to your library and collects all transitive dependencies exclusive test/provided scope. You can execute this with mvn -f pom-dist.xml package if you do not want to provide a whole new project.