20

I'm trying to use the maven-resources-plugin to do some filtering using the copy-resources goal, and ran into the following error:

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

To isolate the problem, I created a very simple pom.xml, copied pretty nearly verbatim from http://maven.apache.org/plugins/maven-resources-plugin/examples/copy-resources.html, ran it, and got the same error.

I'm invoking it with

mvn resources:copy-resources

Any ideas? Here's the test 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>

Andy Dennie
  • 6,012
  • 2
  • 32
  • 51

3 Answers3

35

The main problem you had is that you are invoking the plugin goal directly using

mvn resources:copy-resources

which does not necessarily create the output directory. Instead call the correct Maven lifecycle phase.

mvn process-resources

For a complete list of the lifecycle phases just run the mvn command without anything..

In general it almost always better to invoke a lifecycle phase rather than a goal directly since it guarantees that any preconditions are met (e.g. cant compile test classes before the classes to be tested..).

Manfred Moser
  • 29,539
  • 13
  • 92
  • 123
  • 1
    Hmm, right you are. Thanks -- one more baby step on my way to Maven competence. – Andy Dennie Jun 08 '12 at 22:49
  • Just a note: it is correct to call the goal in pom in another phase but you should typically not run a non-maven-lifecycle goal from outer! – childno͡.de May 12 '15 at 15:23
  • @childno.de what makes you say so? Imho that is not correct. – Manfred Moser May 12 '15 at 15:24
  • because in most cases maven projects are not set up that the outcome of a goal is clearly reproducable for most developers. Some "default" plugins like this copy-resources does hook automatically but you can (as described in the question) add extra executions. That's exactly what you described, a goal from a plugin that works differently if you run it soley but works in lifecycle with dependends phases before. – childno͡.de May 12 '15 at 16:10
  • @ManfredMoser, if he's specified the validate phase in his execution config, why would he run the process-resources phase to execute the plugin? – David Jun 14 '15 at 20:23
  • Fair enough. validate would work however normally it would be bound to a later phase ... – Manfred Moser Jun 15 '15 at 16:03
15

Check if @bmargulies answer works for you. You can refer to these examples.

In any case, you do not need to use <pluginManagement> to achieve this. <pluginManagement> is used in multi-module scenarios to facilitate inheritance of plugin configurations.

You need to move the configuration out of execution element. The following snippet works.

<?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>
Raghuram
  • 51,854
  • 11
  • 110
  • 122
  • 1
    Thanks. I've tried it both ways (specifying the resources in `/` or alternatively in `///`), and they both seem to work equivalently. Is there any advantage to one vs. the other? The latter keeps the information in one place in the pom, which seems somewhat nicer. – Andy Dennie Jun 07 '12 at 19:49
  • @AndyD. Good question (perhaps warrants a separate SO question - so that other/more knowledgeable folks can respond?) The former uses `maven resource plugin` anyway. Some examples in the plugin page refers to the former style as well! – Raghuram Jun 08 '12 at 04:23
  • I appreciate the feedback, and it was helpful, but I'm awarding the accepted answer to Manfred, as he correctly noted that I didn't need to move the configuration out of the execution element after all; I needed to invoke the lifecycle phase rather than the goal directly. – Andy Dennie Jun 14 '12 at 15:03
  • I think this is wrong. You might do this but this will affect all resources handled by the plugin because configuration is not soley for the single execution. In this case, it should do the same. – childno͡.de May 12 '15 at 15:21
1

Just remove the executions and their configuration. Normally you might want to define resources in <build> > <resources> respectively <build> > <testResources> (see http://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html) directly not within the plugin config using the default lifecycle process-(test-)resources which is automatically hooked by copy-(test-)resources.. Yes, it's a bad example on their page!

childno͡.de
  • 4,679
  • 4
  • 31
  • 57