3

I'm try to use the maven-dependency-plugin's copy-dependencies goal. I checked its official example with the snippet below.

My problem is: the dependencies are always copied to target\dependency folder, even if I specify an <outputDirectory> node.

Here is the part of my pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
              <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                  <goal>copy-dependencies</goal>
                </goals>
              </execution>
                <configuration>
                      <outputDirectory>${project.build.directory}/aaa</outputDirectory>
                      <overWriteReleases>true</overWriteReleases>
                      <overWriteSnapshots>true</overWriteSnapshots>
                      <overWriteIfNewer>true</overWriteIfNewer>
                </configuration>                  
            </executions>
        </plugin>
    </plugins>
</build>

Question: What I'm doing wrong? Is it possible to declare the output directory outside of the project? For example: c:\temp ?

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
zsom
  • 479
  • 1
  • 5
  • 19

2 Answers2

3

You configured an execution of the maven-dependency-plugin with a configuration only defined within its scope, hence it will only be picked up by the plugin during a mvn package invocation, that is, while performing the package phase and the plugin (executions) having a binding to it.

If you invoke the plugin from command line as following:

mvn dependency:copy-dependencies

It will indeed only use default values, since your configuration will be ignored.

In fact, the default value for the outputDirectory option is indeed:

Default: ${project.build.directory}/dependency

In maven, a plugin configuration can be defined as general configuration (outside of an execution section, applied to all executions and to command line invocations) or per execution (within an execution section, like in your case).


In your case, you probably want the configuration to be valid in both cases, so simply change your plugin section to the following:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <configuration>
                  <outputDirectory>${project.build.directory}/aaa</outputDirectory>
                  <overWriteReleases>true</overWriteReleases>
                  <overWriteSnapshots>true</overWriteSnapshots>
                  <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
            <executions>
              <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                  <goal>copy-dependencies</goal>
                </goals>
              </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Note: we moved up the configuration, from execution scope to plugin (global) scope.


Also note, in the configuration above we kept the execution, which means maven will always execute this plugin goal at each an every mvn package invocation. If you don't want this behavior and only expect to use the command line execution, then you can remove the executions section at all.

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
0

Since Maven 3.3.1 it's also possible (see the note at the very end of Using the executions Tag section):

Since Maven 3.3.1 this is not the case anymore as you can specify on the command line the execution id for direct plugin goal invocation.

to execute copy-dependencies execution directly not modifying your pom at all:

mvn dependency:copy-dependencies@copy-dependencies

Note that of the two copy-dependencies separated by @, the former refers to the plugin goal and the latter refers to the execution id. And general direct invocation of an execution is:

mvn <plugin-prefix>:<goal>@<execution>

See also accepted answer to almost the same question

Alexander Ites
  • 155
  • 2
  • 9