6

I have a multimodule Maven project. One subproject hosts XSL/XML resource files. The other project hosts Java code that needs to use these files in its unit tests.

In the dependency's jar, the resources lie in the folder xml-resources.

I found this example and tried to change it for my needs:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.4</version>
  <executions>
    <execution>
      <id>resource-dependencies</id>
      <phase>process-test-resources</phase>
      <goals>
        <goal>unpack-dependencies</goal>
      </goals>
      <configuration>
        <classifier>xml-resources</classifier>
        <outputDirectory>${project.build.directory}/classes/xml-resources</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

This doesn't do anything when I run the process-test-resources phase. Am am sure that there are some errors in there - I do not see where I can specify the dependency the resources should be taken from, and <classifier> does not seem to actually specify the source where the resources should be copied from.

I'm lost here, can somebody tell me how to do this right?

flyx
  • 35,506
  • 7
  • 89
  • 126

1 Answers1

9

Try something like this

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.4</version>
  <executions>
    <execution>
      <id>resource-dependencies</id>
      <phase>process-test-resources</phase>
      <goals>
        <goal>unpack-dependencies</goal>
      </goals>
      <configuration>
        <includeArtifactIds>my-artifact-id</includeArtifactIds>
        <includes>foobar.txt, loremipsum.xml</includes>
        <outputDirectory>${project.build.directory}/classes/xml-resources</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

Have a look at the unpack-dependencies parameters for detailed explanation or further information.

FrVaBe
  • 47,963
  • 16
  • 124
  • 157
  • 2
    The outputDirectory folder in this case should be ../test-classes/.. instead of ../classes/.... – khmarbaise May 10 '12 at 11:04
  • 1
    @khmarbaise valid objection - maybe even better `${project.build.testOutputDirectory}/xml-resources` (see [Super POM](http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Super_POM)) – FrVaBe May 10 '12 at 11:21
  • Thanks for this, copying works now. The files lie in `target/test-classes/xml-resources`. However, I'm not able to load them in my test code via `getClass().getResourceAsStream("/xml-resources/file.xml")`. Is there something else I need to do? – flyx May 10 '12 at 11:42
  • Ah forget it, that was my IDE's fault. – flyx May 10 '12 at 12:29