Search code examples
javamavenmaven-dependency-plugin

maven: copy resource from dependent jar to target folder


I need to copy a resource file (menu.xml) from the root of a dependent jar file to the root of the output directory of my current project, before the tests are executed during the build. The file must be available for the tests but also later for the main program using ...getClassLoader().getResourceAsStream("menu.xml").

I'm trying the solution suggested in this question but it's not working.

This is how the pom file looks like:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>resource-dependencies</id>
        <phase>process-test-resources</phase>
        <goals>
          <goal>unpack-dependencies</goal>
        </goals>
        <configuration>
          <type>jar</type>
          <includeArtifactIds>other-jar</includeArtifactIds>
          <includes>menu.xml</includes>
          <outputDirectory>${project.build.outputDirectory}</outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>

When I execute mvn clean process-test-resources I see the following output:

[INFO] --- maven-resources-plugin:2.7:testResources (default-testResources) @ something ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-dependency-plugin:2.8:unpack-dependencies (resource-dependencies) @ something ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.709 s
[INFO] Finished at: 2016-05-05T15:01:06-03:00
[INFO] Final Memory: 30M/458M
[INFO] ------------------------------------------------------------------------

The file menu.xml is not copied to the target folder. How can I see what could be possibly wrong? I tried to run maven on debug log level and could see that the configuration was parsed correctly, but the plugin doesn't log any additional information of what is happening.

...
[DEBUG]   (f) includeArtifactIds = other-jar
[DEBUG]   (s) includes = menu.xml
...
[DEBUG]   (f) outputAbsoluteArtifactFilename = false
[DEBUG]   (s) outputDirectory = c:\Dev\workspaces\config\something\target\classes
...
[DEBUG]   (f) project = MavenProject: com.something:something:3-SNAPSHOT @ c:\Dev\workspaces\config\something\
pom.xml
[DEBUG] -- end configuration --
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.014 s
[INFO] Finished at: 2016-05-05T15:09:29-03:00
[INFO] Final Memory: 27M/328M
[INFO] ------------------------------------------------------------------------

Solution

  • A nice way to copy files from your dependency is to use Goal unpack :

    you can mention your files /directories in dir/** --> include everything from a directory

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <id>resource-dependencies</id>
                <phase>compile</phase>
                <goals>
                    <goal>unpack</goal>
                </goals>
                <configuration>
                    <artifactItems>
                    <artifactItem>
                        <groupId>group_id</groupId>
                        <artifactId>artifact_id</artifactId>
                        <version>1.0.0</version>
                        <type>jar</type>
                        <overWrite>true</overWrite>
                        <outputDirectory>${project.build.directory}</outputDirectory>
                    </artifactItem>
                    </artifactItems>
                    <includes>directory_to_include/**</includes>
                    <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    I attached goal unpack to the phase compile. Customise as per your need.