我编写了一个生成代码并将其粘贴在 {root}/target/generated-sources/foo 下的 mojo。当我执行时:
mvn clean install
我得到的错误表明生成的源文件没有包含在构建路径中(生成的文件在那里,但没有在编译阶段被拾取)。我从this answer了解到我需要动态添加 {root}/target/generated-sources/foo 作为 POM 的源目录。问题是,我无法找到有关如何执行此操作的任何信息。
作为备用计划,我打算使用 Build Helper Maven 插件,但我希望尽可能在我的 mojo 中自动执行此操作。
最佳答案
我更喜欢将此添加到我的 Mojo 中:
/**
* The current project representation.
* @parameter expression="${project}"
* @required
* @readonly
*/
private MavenProject project;
/**
* Directory wherein generated source will be put; main, test, site, ... will be added implictly.
* @parameter expression="${outputDir}" default-value="${project.build.directory}/src-generated"
* @required
*/
private File outputDir;
显然,您可以更改 default-value
以匹配您自己的模式。
然后在execute()
方法中:
if (!settings.isInteractiveMode()) {
LOG.info("Adding " + outputDir.getAbsolutePath() + " to compile source root");
}
project.addCompileSourceRoot(outputDir.getAbsolutePath());
关于java - 将 mojo 生成的代码动态添加到源路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11931652/