java - 是否可以配置 maven 来编译生成的源代码而不使用插件?

标签 java maven code-generation

我知道这个问题并不新鲜。但似乎并没有明确的答案。 This answer从 2012 年开始,如果生成的源位于 target/generated-sources/<tool> 下它们将被编译。 ANTLR 4 maven plugin遵循这个范式。根据文档,outputDirectory 的默认值为:${project.build.directory}/generated-sources/antlr4 .

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

当我尝试this solution时这建议使用 mojo build-helper-maven-plugin它按预期编译。但根据maven guide to generating sources它应该可以在没有任何帮助插件的情况下工作,不是吗?我错过了什么吗?

<小时/>

这是我用来生成源代码的 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>

最佳答案

你的理解有点不正确。

Nothing automatic, plugins generating source code typically handle that by adding their output directory (something like target/generated-sources/ by convention) as source directory to the POM so that it will be included later during the compile phase.

Some less well implemented plugins don't do that for you and you have to add the directory yourself, for example using the Build Helper Maven Plugin.

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

例如:参见antlr4's Antlr4Mojo.java类(class)。在这里,插件通过在 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

关于java - 是否可以配置 maven 来编译生成的源代码而不使用插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59107462/

相关文章:

java - 如何使用 Java stream api 过滤 map ?

java - 使用 wget 和 Nexus Rest API 进行奇怪的文件命名

java - Apache poi 中的数组公式

java - 动态加载 Spring Integration 组件

java - 是否可以限制 JavaDocs 生成中的特定属性/字段?

java.lang.Exception : No tests found matching Method using Intellij IDEA

sql-server - 经常使用模式绑定(bind)和更新时如何处理数据库模式更新

c# - 使用 C# 实现代码模板

JavaPOET - 只有类有父类(super class),而不是 INTERFACE

java - 在android中排序字符串的arraylist