Java 为什么 Maven 生成的源没有被编译?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19633505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:59:24  来源:igfitidea点击:

Why are Maven generated-sources not getting compiled?

javamavenmaven-3

提问by acsadam0404

I have a plugin which generates sources under the target/generated-sources/wrappersdirectory. It's wired into the generate-sources phase like this:

我有一个插件可以在target/generated-sources/wrappers目录下生成源代码。它像这样连接到 generate-sources 阶段:

<plugin>
    <groupId>mygroupid</groupId>
    <artifactId>myartifactid</artifactId>
    <executions>
        <execution>
        <phase>generate-sources</phase>
        <goals>
            <goal>xml2java</goal>
        </goals>
        </execution>
    </executions>
</plugin>

The problem is, when I use mvn deploythe .classfiles won't be placed in the jar. I see all the .javafiles there, but no .class.

问题是,当我使用mvn deploy这些.class文件时,不会将其放入 jar 中。我看到.java那里的所有文件,但没有.class.

I read all the issues around this problem, but couldn't figure out how to solve the problem. I'm using Maven 3.0.x.

我阅读了有关此问题的所有问题,但无法弄清楚如何解决该问题。我正在使用 Maven 3.0.x。

采纳答案by acsadam0404

The build-helper plugin indeed solved the problem. Thanks @Joe for the comment.

build-helper 插件确实解决了这个问题。感谢@Joe 的评论。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources/wrappers</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

回答by Markus Per?bner

If you have written the plugin by yourself you can programatically add the path with the generated sources to the maven source paths.

如果您自己编写了插件,则可以以编程方式将带有生成源的路径添加到 maven 源路径中。

@Mojo(name = "generate")
public class MyCodegenMojo extends AbstractMojo {
    @Parameter(defaultValue = "${project}")
    private MavenProject project;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException{
        // your generator code

        project.addCompileSourceRoot("path/to/your/generated/sources");
    }

}

For example the raml-jaxrs-codegen plugin uses this technique. See RamlJaxrsCodegenMojo.javafor more details.

例如 raml-jaxrs-codegen 插件就使用了这种技术。有关更多详细信息,请参阅RamlJaxrsCodegenMojo.java