【问题标题】:Mojo add eclipse source folderMojo 添加 eclipse 源文件夹
【发布时间】:2017-05-11 16:26:11
【问题描述】:

我编写了一个生成源代码的 maven 插件。 这基本上可以正常工作。

问题是,Eclipse 无法将我生成代码的目录识别为附加源文件夹。因此我收到大量错误说XXX cannot be resolved to a type。不过,从命令行编译和安装的 maven 工作正常。

首先我使用org.codehaus.mojo.build-helper-maven-plugin 解决了这个问题。这工作正常。但是,我不喜欢我的插件的用户也需要添加第二个插件。因此,我查看了build-helper-maven-plugin 中的source code of the add-source goal,并决定将相关代码直接添加到我的插件中。因此我的插件看起来像这样:

@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 连接器?目前我正在使用 lifecycle-mapping plugin 解决这个 m2e 连接器

<action>
    <execute>
        <runOnConfiguration>true</runOnConfiguration>
        <runOnIncremental>true</runOnIncremental>
    </execute>
</action>

【问题讨论】:

    标签: java m2e maven-mojo


    【解决方案1】:

    虽然您的插件向项目添加了额外的源目录,但 Eclipse 无法识别。您可以配置 Eclipse 应该执行您的目标,但您不能告诉 Eclipse 添加额外的源目录。

    一些插件,例如build-helper,可以添加额外的源目录,但需要相应的m2e扩展名。没有适用于所有插件的通用 m2e。

    您有以下选择:

    1. 使用build-helper-maven-plugin。我同意这是愚蠢的
    2. 编写自己的 m2e 扩展。比选项 1 差得多。
    3. 为插件生成的源代码使用单独的 maven 模块。在这样的模块中,您可以定义&lt;sourceDirectory&gt;${project.build.directory}/generate-sources&lt;/..&gt;。这种分离是有意义的:生成的代码通常具有与常规代码不同的性质。
    4. 什么都不做,并要求开发人员手动添加额外的源文件夹。这看起来很原始,但有一个优点 - 在 [右键单击 -> Maven -> 更新项目] 上不会删除手动添加的源文件夹

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 2016-11-20
      • 1970-01-01
      • 2014-06-28
      • 1970-01-01
      • 2012-01-13
      • 2015-05-15
      相关资源
      最近更新 更多