插件如何添加自己生成的资源?

Jin*_*won 5 maven-plugin maven

这个问题是针对 maven 中提出的完全相同的解决方案:How to add resources which are generated after Compiling Phase,但我正在寻找另一个解决方案。

在我的插件中,我成功地在目录中生成了一些资源文件target/generated-resources/some

现在我希望这些资源文件包含在托管项目的最终 jar 中。

我试过。

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

其中project是这样定义的。

@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
Run Code Online (Sandbox Code Playgroud)

但这不起作用。

rec*_*rec 1

编译阶段之后,不再调用Maven资源插件。因此,在如此晚的阶段向构建添加更多资源只会产生表面效果,例如,Eclipse 等 IDE 将生成的资源文件夹识别为源文件夹并相应地对其进行标记。

您必须手动将结果从插件复制到构建输出文件夹:

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);
}
Run Code Online (Sandbox Code Playgroud)