Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a plugin add its own generated resources?

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.

like image 855
Jin Kwon Avatar asked Nov 10 '22 03:11

Jin Kwon


1 Answers

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);
}
like image 104
rec Avatar answered Nov 15 '22 06:11

rec