maven-dependency-plugin忽略outputDirectory配置 [英] maven-dependency-plugin ignores outputDirectory configuration

查看:1676
本文介绍了maven-dependency-plugin忽略outputDirectory配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我的主要java项目及其所有依赖项创建一个jar文件.所以我在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>

所以我执行mvn dependency:copy-dependencies,它可以将所有依赖项复制到target/dependency而不是dependency-jars,效果很好.有什么想法吗?

解决方案

那很正常:您配置了名为copy-dependenciesmaven-dependency-plugin的特殊执行,但是直接在命令行上调用了目标dependency:copy-dependencies创建一个默认执行,该默认执行与您配置的默认执行不同.因此,不会考虑您的配置.

在Maven中,可以在2个地方配置插件:用于所有执行(在<plugin>级别使用<configuration>)或每个执行(在<execution>级别使用<configuration>). /p>

有几种方法可以解决您的问题:

  • <configuration>移到<execution>之外,并使其对所有执行通用.您将拥有:

    <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>
    

    请注意,与此相关,插件的所有执行都将使用此配置(除非在特定的执行配置中被覆盖).

  • 在命令行上执行特定的执行,即您配置的执行. 从Maven 3.3.1开始,这是可能的然后执行

    mvn dependency:copy-dependencies@copy-dependencies
    

    @copy-dependencies用于引用您要调用的执行的<id>.

  • 将您的执行绑定到Maven生命周期的特定阶段,并使其以正常的生命周期流程执行.在您的配置中,它已经通过<phase>package</phase>绑定到package阶段.因此,调用mvn clean package将起作用并将您的依赖项复制到配置的位置.

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?

解决方案

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.

这篇关于maven-dependency-plugin忽略outputDirectory配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆