maven-dependency-plugin ignores outputDirectory configuration

[亡魂溺海] 提交于 2019-12-01 06:48:56

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/38815961/maven-dependency-plugin-ignores-outputdirectory-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!