Maven资源插件在使用copy-resources目标时出错:'resources','outputDirectory'缺失或无效。

20

我想使用maven-resources-plugin来使用copy-resources目标进行一些过滤,但遇到以下错误:

Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:2.5:copy-resources (default-cli) on project bar: The parameters 'resources', 'outputDirectory' for goal org.apache.maven.plugins:maven-resources-plugin:2.5:copy-resources are missing or invalid
为了定位问题,我创建了一个非常简单的pom.xml文件,几乎完全是从http://maven.apache.org/plugins/maven-resources-plugin/examples/copy-resources.html复制而来的,运行它后得到相同的错误。
我使用以下方式调用它:
mvn resources:copy-resources

有任何想法吗?这是测试pom.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>foo</groupId>
  <artifactId>bar</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/extra-resources</outputDirectory>
              <resources>          
                <resource>
                  <directory>src/non-packaged-resources</directory>
                  <filtering>true</filtering>
                </resource>
              </resources>              
            </configuration>            
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

3个回答

35
你遇到的主要问题是直接使用插件目标调用插件。
mvn resources:copy-resources

该命令不一定会创建输出目录,而是应该调用正确的Maven生命周期阶段。

mvn process-resources

要获取完整的生命周期阶段列表,只需运行mvn命令而不加任何参数即可。

通常情况下,调用生命周期阶段而不是直接调用目标(goal)几乎总是更好的选择,因为它保证了满足任何前提条件(例如,在编译测试类之前无法编译被测试的类等)。


1
嗯,你说得对。谢谢---这是我走向Maven能力的又一次小进步。 - Andy Dennie
只是一个提示:在POM中调用目标另一个阶段是正确的,但通常不应该从外部运行非Maven生命周期目标! - childno͡.de
@childno.de,你为什么这么说?在我看来那是不正确的。 - Manfred Moser
因为在大多数情况下,Maven 项目没有设置目标的结果对于大多数开发人员来说是明确可重现的。一些“默认”插件(如 copy-resources)会自动挂钩,但您可以(如问题中所述)添加额外的执行。这正是您所描述的,一个插件的目标,如果您单独运行它,则工作方式不同,但在依赖阶段之前的生命周期中工作。 - childno͡.de
@ManfredMoser,如果他在执行配置中指定了验证阶段,为什么要运行process-resources阶段来执行插件呢? - David
好的。验证会起作用,但通常它会绑定到稍后的阶段... - Manfred Moser

15

请检查@bmargulies的答案是否适用于您。您可以参考这些示例

无论如何,您不需要使用<pluginManagement>来实现此目的。<pluginManagement>在多模块场景中用于促进插件配置的继承。

您需要将configuration移出execution元素。以下代码段有效。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>foo</groupId>
  <artifactId>bar</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <outputDirectory>${basedir}/target/extra-resources</outputDirectory>
          <resources>          
        <resource>
          <directory>src/non-packaged-resources</directory>
          <filtering>true</filtering>
        </resource>
          </resources>              
        </configuration>            
        <executions>
          <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

1
谢谢。我已经尝试了两种方式(在<build>/<resources>中指定资源或者在<build>/<plugins>/<plugin>/<configuration>中指定),它们似乎都可以等效地工作。有没有一种方法比另一种更有优势?后者将信息保留在pom的一个位置,这似乎更好一些。 - Andy Dennie
@AndyD。好问题(也许值得单独提出SO问题,以便其他/更有知识的人可以回答?)前者无论如何都使用“maven资源插件”。插件页面中的一些示例也引用了前者的样式! - Raghuram
我感谢您的反馈,它很有帮助。但是,我将采纳Manfred的答案,因为他正确指出我不需要将配置移出执行元素;我需要直接调用生命周期阶段而不是目标。 - Andy Dennie
我认为这是错误的。你可能会这样做,但这将影响插件处理的所有资源,因为配置不仅仅是针对单个执行的。在这种情况下,应该做相同的事情。 - childno͡.de

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接