【问题标题】:maven-compiler-plugin 3.6.0 doesn't compile generated sources from annotationsmaven-compiler-plugin 3.6.0 不编译从注释生成的源
【发布时间】:2018-03-07 20:23:03
【问题描述】:

我们刚刚将 JBoss 从 6.1.0 升级到 Wildfly 10.1,并对模块和工件版本等进行了各种相关的升级。在一个模块中,这导致我们的 cobertura 编译失败并出现编译器错误。我找到了IllegalStateException in Hibernate metamodel generation with maven 并升级到 maven-compiler-plugin 3.6.0(从 3.1 开始)。这似乎解决了我的问题,但仅限于本地。我可以为 cobertura 编译模块,但结果却导致了一个新问题。

此模块的一些注释生成的源被另一个模块使用,并且找不到类文件。发生了什么变化? generated-sources 目录包含 java 文件,但未编译类。

它曾考虑将 build-helper 阶段更改为从 process-sources 帮助生成源,但随后失败了。

由于 3.1 和 3.6.0 之间的更改,是否还有其他需要更改的地方? (我不熟悉如何处理注释 - 我只是 Cobertura 支持人员。)

pom 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<description>The JPA entities for the Element Manager</description>

<artifactId>em-model</artifactId>
<groupId>com.myprod.em</groupId>

<parent>
    <artifactId>em</artifactId>
    <groupId>com.myprod</groupId>
    <version>3.5.0.0.0-SNAPSHOT</version>
</parent>

<packaging>jar</packaging>

<build>
    <plugins>

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

        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <groupId>org.apache.maven.plugins</groupId>
            <configuration>
                <finalName>em-model</finalName>
            </configuration>
        </plugin>

        <plugin>
               <artifactId>maven-compiler-plugin</artifactId>
               <configuration>
                  <compilerArgument>-proc:none</compilerArgument>
               </configuration>
               <executions>
                  <execution>
                     <id>run-annotation-processors-only</id>
                     <phase>generate-sources</phase>
                     <configuration>
                        <compilerArgument>-proc:only</compilerArgument>                            
                     </configuration>
                     <goals>
                        <goal>compile</goal>
                     </goals>
                  </execution>
               </executions>  
        </plugin>

    </plugins>        
</build>

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.el</artifactId>
        <version>3.0.1-b08</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.spec.javax.ejb</groupId>
        <artifactId>jboss-ejb-api_3.2_spec</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Since hibernate validator is used in unit tests, 
         these JBoss logging deps are needed -->        
    <dependency>
        <groupId>org.jboss.slf4j</groupId>
        <artifactId>slf4j-jboss-logmanager</artifactId>
        <scope>provided</scope>    
    </dependency>
     <dependency>
        <groupId>org.jboss.logmanager</groupId>
        <artifactId>jboss-logmanager</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.8.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
        <scope>provided</scope>
    </dependency>    
    <dependency>
        <groupId>com.myco.csp</groupId>
        <artifactId>nrp_jpa</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.myco.cim</groupId>
        <artifactId>cs_cim_jpa</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-jpamodelgen</artifactId>
           <version>1.0.0.Final</version>
           <scope>provided</scope>
    </dependency>

    <!-- Test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
    </dependency>
    <dependency>
        <groupId>com.myco.logging</groupId>
        <artifactId>logging-client</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>apache-log4j</groupId>
        <artifactId>log4j</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.myprod.prodCommon</groupId>
        <artifactId>unit-test-utils</artifactId>
        <version>${project.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

【问题讨论】:

    标签: java maven annotations hibernate-annotations maven-compiler-plugin


    【解决方案1】:

    我通过从编译器插件中删除 -proc:none 编译器参数解决了这个问题。就目前而言,根本没有编译任何生成的源代码。使用 3.1 插件我必须拥有它,但使用 3.6.0 我不能。

    我还尝试通过使compilerArg 特定于默认编译阶段来实现答案https://stackoverflow.com/a/35045416/4756238,但这并没有编译生成的源代码。如果我不重用默认编译 id,构建工作并给了我生成的类文件,但它运行了两个编译阶段,-proc:none 一秒钟,这似乎是多余的。

    编译器的最终 pom 部分如下所示:

    <plugin>
       <artifactId>maven-compiler-plugin</artifactId>
       <executions>
         <execution>
           <id>run-annotation-processors-only</id>
           <phase>generate-sources</phase>
           <configuration>
             <compilerArgument>-proc:only</compilerArgument>
           </configuration>
           <goals>
             <goal>compile</goal>
           </goals>
         </execution>
       </executions>
    </plugin>
    

    【讨论】:

    • Re:使compilerArg 特定于default-compile,如果不查看您尝试的实际配置,很难知道问题出在哪里。您是否从maven-compiler-plugin 配置中删除了-proc:none?有没有添加3.6.0的版本(因为最新版本是3.7.0)?尝试在单独的问题中提出MCVE
    • @heenenee 我完全按照您引用的答案添加了 default-compile 部分,并从我的部分顶部删除了配置 -proc:none 。那没有用,我仍然没有得到生成的类文件。当我使用您的建议但遗漏了构建的 ID 时,构建工作但运行了两次编译。我在我的主 pom 中添加了 3.6.0 版本,并从大约 90 个子 pom 中删除了该版本,其中有 4 个不同的版本。
    • 我什至无法考虑为此制作 MCVE。我拥有的模块是大约 50 个文件中的 8800 LOC,我对能够剥离有用部分的代码一无所知。 :( build-helper 可以在这里做任何事情吗?
    • 我明白,但是“那行不通”是相当模糊的,所以很难提供一个具体的答案。但是,根据您所说的,我猜您的构建使用多个注释处理器(例如,一个阶段在源生成中使用注释处理,另一个阶段的注释处理作用于前一阶段生成的源) .这可以解释为什么从 default-compile 执行中删除 -proc:none 是有效的。它没有解释为什么你会看到来自 3.13.6.0 的不同结果。
    • 我打算将我的答案标记为已接受,但我会再等几天看看是否有人能解释为什么我必须在 3.1 和 3.6.0/3.7 之间更改注释处理参数.0.我想知道它是否与 2010 年 3 月的 1.0.0.Final 版本的 hibernate-jpamodelgen 有关?当我有更多时间时,我可能会升级它。
    猜你喜欢
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 2018-11-25
    • 2014-10-04
    相关资源
    最近更新 更多