5

This question is for exactly the same solution asked in maven: How to add resources which are generated after compilation phase, but I'm looking for an another solution.

In my plugin I successfully generated some resource files in target/generated-resources/some directory.

Now I want those resource files included in the final jar of the hosting project.

I tried.

final Resource resource = new Resource();
resource.setDirectory("target/generated-resources/some");
project.getBuild().getResources().add(resource);

where the project is defined like this.

@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;

And it doesn't work.

Community
  • 1
  • 1
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184

1 Answers1

1

After the compilation phase, the Maven resources plugin is no longer called. Thus, adding more resources to the build in such a late phase only has cosmetic effects, e.g. that IDEs such as Eclipse recognize the generated-resources folder as a source folder and mark it correspondingly.

You have to manually copy the results from your plugin to the build output folder:

import org.codehaus.plexus.util.FileUtils;

// Finally, copy all the generated resources over to the build output folder because
// we run after the "process-resources" phase and Maven no longer handles the copying
// itself in later phases.
try {
    FileUtils.copyDirectoryStructure(
            new File("target/generated-resources/some"),
            new File(project.getBuild().getOutputDirectory()));
}
catch (IOException e) {
    throw new MojoExecutionException("Unable to copy generated resources to build output folder", e);
}
rec
  • 10,340
  • 3
  • 29
  • 43