繁体   English   中英

是否可以在不使用插件的情况下配置 maven 来编译生成的源代码?

[英]Is it possible to configure maven to compile generated sources wouthout the use of a plugin?

我知道这个问题并不新鲜。 但似乎没有确定的答案。 2012 年的这个答案指出,如果生成的源代码放在target/generated-sources/<tool>它们将被编译。 ANTLR 4 maven 插件遵循这个范例。 根据文档, outputDirectory 的默认值为: ${project.build.directory}/generated-sources/antlr4

现在就我而言,我有一个生成源的自定义工具。 我已将其输出目录设置为${project.build.directory}/generated-sources/whatever但它不起作用。 关于whatever部分,我尝试使用生成源的目标的 id,甚至试图劫持antlr4名称。 虽然没有结果。

当我尝试这个建议使用 mojo build-helper-maven-plugin解决方案时,它会按预期编译。 但是根据生成源的 Maven 指南,它应该可以在没有任何帮助插件的情况下工作,不是吗? 我错过了什么吗?


这是我用来生成源的 POM(片段)配置。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.4.0</version>
    <executions>
        <execution>
            <id>generate-code</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <includeProjectDependencies>false</includeProjectDependencies>
        <includePluginDependencies>true</includePluginDependencies>
        <executableDependency>
            <groupId>com.company.product</groupId>
            <artifactId>CodeGenerator</artifactId>
        </executableDependency>
        <arguments>
            <argument>${basedir}/</argument>
            <argument>${project.build.directory}/generated-sources/generate-code/</argument>
        </arguments>
        <mainClass>com.company.codegeneration.CodeGenerator</mainClass>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>com.company.product</groupId>
            <artifactId>CodeGenerator</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>jar</type>
        </dependency>
    </dependencies>
</plugin>

你的理解有点不对。

没有什么是自动的,生成源代码的插件通常通过将它们的输出目录(按照约定类似于 target/generated-sources/)作为源目录添加到 POM 中来处理这个问题,以便稍后在编译阶段包含它。

一些不太好实现的插件不会为你做这些,你必须自己添加目录,例如使用 Build Helper Maven Plugin。

正如另一个答案所指出的,大多数插件通常将生成的代码添加为新的源路径。

例如:参见antlr4 的 Antlr4Mojo.java类。 在这里,插件通过在execute方法中调用addSourceRoot方法将生成的类添加到项目源中。

    //  Omitted some code
  void addSourceRoot(File outputDir) {
            if (generateTestSources) {
                project.addTestCompileSourceRoot(outputDir.getPath());
            }
            else {
                project.addCompileSourceRoot(outputDir.getPath());
            }
          }


    //  Omitted some code

 @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
    //  Omitted code
        if(project!=null)

        {
            // Tell Maven that there are some new source files underneath the output
            // directory.
            addSourceRoot(this.getOutputDirectory());
        }
}
    //  Omitted some code

因此,您可以在自定义插件中执行此操作,也可以使用build-helper-maven-plugin

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM