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>

4

3 回答 3

34

您遇到的主要问题是您直接使用调用插件目标

mvn resources:copy-resources

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

mvn process-resources

要获得生命周期阶段的完整列表,只需运行 mvn 命令即可。

一般来说,调用生命周期阶段而不是直接调用目标几乎总是更好,因为它保证满足任何先决条件(例如,不能在要测试的类之前编译测试类......)。

于 2012-06-08T22:10:37.297 回答
14

检查@bmargulies 答案是否适合您。你可以参考这些例子

在任何情况下,您都不需要使用<pluginManagement>来实现这一点。 <pluginManagement>用于多模块场景,方便plugin配置的继承。

您需要将元素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>
于 2012-06-07T04:28:37.300 回答
1

只需删除执行及其配置。通常,您可能希望使用默认生命周期直接在插件配置中分别定义资源(请<build> > <resources>参阅<build> > <testResources>http://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html process-(test-)resources被迷住了copy-(test-)resources.。是的,这是他们页面上的一个坏例子!

于 2015-05-12T15:25:37.093 回答