【问题标题】:Why are Maven generated-sources not getting compiled? [duplicate]为什么 Maven 生成的源没有被编译? [复制]
【发布时间】:2013-11-07 03:34:43
【问题描述】:

我有一个在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>

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

我阅读了有关此问题的所有问题,但不知道如何解决该问题。我正在使用 Maven 3.0.x。

【问题讨论】:

  • 链接问题的答案也提到了相同的build-helper-maven-plugin。几乎所有适用于 Maven 2 的东西也适用于 Maven 3。这就是为什么所有这些 maven-X tagging is nonsense

标签: java maven maven-3


【解决方案1】:

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>

【讨论】:

    【解决方案2】:

    如果您自己编写了插件,您可以通过编程将生成源的路径添加到 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");
        }
    
    }
    

    例如 raml-jaxrs-codegen 插件使用这种技术。详情请见RamlJaxrsCodegenMojo.java

    【讨论】:

    • 非常感谢您的重要提示!它为我节省了很多时间!
    猜你喜欢
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 2015-03-09
    • 2018-06-19
    • 2013-03-05
    • 2017-10-15
    相关资源
    最近更新 更多