简体   繁体   English

maven-dependency-plugin忽略outputDirectory配置

[英]maven-dependency-plugin ignores outputDirectory configuration

I want to create a jar file with my main java project and all of it's dependencies. 我想用我的主java项目及其所有依赖项创建一个jar文件。 so I created the following plugin definition in the pom file: 所以我在pom文件中创建了以下插件定义:

<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 . 所以我执行mvn dependency:copy-dependencies ,它工作正常,它将所有依赖项复制到target/dependency而不是dependency-jars Any ideas? 有任何想法吗?

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. 这是正常的:您配置了maven-dependency-plugin的特殊执行,名为copy-dependencies ,但是,调用目标dependency:copy-dependencies直接在命令行上的dependency:copy-dependencies创建一个默认执行,这与您配置的默认执行不同。 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). 在Maven中,有两个地方可以配置插件:要么是所有执行(使用<plugin>级别的<configuration> ),要么是每次执行(使用<execution>级别的<configuration> )。

There are several ways to solve your issue: 有几种方法可以解决您的问题:

  • Move the <configuration> outside of the <execution> , and make it general for all executions. <configuration>移动到<configuration> <execution> ,并使其适用于所有执行。 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, ie the one you configured. 在命令行上执行特定的执行,即您配置的执行。 This is possible since Maven 3.3.1 and you would execute 这是可能的,因为Maven 3.3.1并且您将执行

     mvn dependency:copy-dependencies@copy-dependencies 

    The @copy-dependencies is used to refer to the <id> of the execution you want to invoke. @copy-dependencies用于引用要调用的执行的<id>

  • Bind your execution to a specific phase of the Maven lifecycle, and let it be executed with the normal flow of the lifecycle. 将您的执行绑定到Maven生命周期的特定阶段,并让它以生命周期的正常流程执行。 In your configuration, it is already bound to the package phase with <phase>package</phase> . 在您的配置中,它已经与<phase>package</phase>绑定到package阶段。 So, invoking mvn clean package would work and copy your dependencies at the configured location. 因此,调用mvn clean package可以在配置的位置复制您的依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM