ufk ufk - 4 months ago 29
Java Question

maven-dependency-plugin ignores outputDirectory configuration

I want to create a jar file with my main java project and all of it's dependencies. so I created the following plugin definition in the pom file:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- exclude junit, we need runtime dependency only -->
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>


so I execute
mvn dependency:copy-dependencies
, it works fine that it copies all the dependencies to
target/dependency
instead of
dependency-jars
. Any ideas?

Answer

That is normal: you configured a special execution of the maven-dependency-plugin, named copy-dependencies, however, invoking the goal dependency:copy-dependencies directly on the command line creates a default execution, which is different than the one you configured. Thus, your configuration isn't taken into account.

In Maven, there are 2 places where you can configure plugins: either for all executions (using <configuration> at the <plugin> level) or for each execution (using <configuration> at the <execution> level).

There are several ways to solve your issue:

  • Move the <configuration> outside of the <execution>, and make it general for all executions. You would have:

    <plugin>
      <artifactId>maven-dependency-plugin</artifactId>
      <version>2.5.1</version>
      <configuration>
        <!-- exclude junit, we need runtime dependency only -->
        <includeScope>runtime</includeScope>
        <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
      </configuration>
    </plugin>
    

    Note that, with this, all executions of the plugin will use this configuration (unless overriden inside a specific execution configuration).

  • Execute on the command line a specific execution, i.e. the one you configured. This is possible since Maven 3.3.1 and you would execute

    mvn dependency:copy-dependencies@copy-dependencies
    

    The @copy-dependencies is used to refer to the <id> of the execution you want to invoke.

  • Bind your execution to a specific phase of the Maven lifecycle, and let it be executed with the normal flow of the lifecycle. In your configuration, it is already bound to the package phase with <phase>package</phase>. So, invoking mvn clean package would work and copy your dependencies at the configured location.