我编写了一个生成源代码的 Maven 插件。这基本上可以正常工作。
问题是,Eclipse 无法将我生成代码的目录识别为附加源文件夹。因此我得到大量的错误说XXX cannot be resolved to a type
。不过,从命令行编译和安装的 Maven 工作正常。
首先,我通过使用解决了这个问题org.codehaus.mojo.build-helper-maven-plugin
。这工作正常。但是,我不喜欢我的插件的用户也需要添加第二个插件。因此,我查看了目标的源代码,add-source
build-helper-maven-plugin
并决定将相关代码直接添加到我的插件中。因此我的插件看起来像这样:
@Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class MyMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
@Parameter(required = true)
private File targetDirectory;
// some more members
@Override
public void execute() throws MojoExecutionException {
// generation of the sources into targetDirectory
project.addCompileSourceRoot(targetDirectory.getAbsolutePath());
}
}
执行期间没有错误,无论是从命令行还是从 Eclipse(使用 Alt+F5 或右键单击 -> Maven -> 更新项目)。但是,无法识别附加的源目录。
我做错什么了吗?还是我需要一个特殊的 m2e 连接器?目前我正在使用生命周期映射插件解决这个 m2e 连接器
<action>
<execute>
<runOnConfiguration>true</runOnConfiguration>
<runOnIncremental>true</runOnIncremental>
</execute>
</action>