简体   繁体   English

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

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

I know this question is not new.我知道这个问题并不新鲜。 But it seems that there is no definite answer.但似乎没有确定的答案。 This answer from 2012 states that if generated sources are placed under target/generated-sources/<tool> they will be compiled. 2012 年的这个答案指出,如果生成的源代码放在target/generated-sources/<tool>它们将被编译。 ANTLR 4 maven plugin follows this paradigm. ANTLR 4 maven 插件遵循这个范例。 Per documentation, the default value of outputDirectory is: ${project.build.directory}/generated-sources/antlr4 .根据文档, outputDirectory 的默认值为: ${project.build.directory}/generated-sources/antlr4

Now in my case I have a custom tool that generates sources.现在就我而言,我有一个生成源的自定义工具。 I've set its output directory to be at ${project.build.directory}/generated-sources/whatever and it didn't work.我已将其输出目录设置为${project.build.directory}/generated-sources/whatever但它不起作用。 Regarding the whatever part, I've tried to use the id of the goal that generates the sources and even tried to hijack antlr4 name.关于whatever部分,我尝试使用生成源的目标的 id,甚至试图劫持antlr4名称。 No result though.虽然没有结果。

When I try this solution that suggests using mojo build-helper-maven-plugin it compiles as expected.当我尝试这个建议使用 mojo build-helper-maven-plugin解决方案时,它会按预期编译。 But according to maven guide to generating sources it should be working without any helper plugin, shouldn't it?但是根据生成源的 Maven 指南,它应该可以在没有任何帮助插件的情况下工作,不是吗? Am I missing something?我错过了什么吗?


Here is the POM (fragment) configuration that I use to generate the 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>

Your understanding is just a bit incorrect.你的理解有点不对。

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

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.一些不太好实现的插件不会为你做这些,你必须自己添加目录,例如使用 Build Helper Maven Plugin。

As the other answer noted, most plugins typically add the generated code as new source path.正如另一个答案所指出的,大多数插件通常将生成的代码添加为新的源路径。

Ex: See antlr4's Antlr4Mojo.java class.例如:参见antlr4 的 Antlr4Mojo.java类。 Here, the plugin is adding the generated classes to project source by calling addSourceRoot method in execute method.在这里,插件通过在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

So, you can either do this in your custom plugin or use the build-helper-maven-plugin .因此,您可以在自定义插件中执行此操作,也可以使用build-helper-maven-plugin

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

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