maven-compiler-plugin 3.6.0 doesn't compile generated sources from annotations
Asked Answered
N

1

1

We just upgraded our JBoss from 6.1.0 to Wildfly 10.1, and made a variety of associated upgrades to modules and artifact versions and so on. In one module this caused our cobertura compiles to fail with a compiler error. I found IllegalStateException in Hibernate metamodel generation with maven and upgraded to maven-compiler-plugin 3.6.0 (from 3.1). This seemed to resolve my problem, but only on a local basis. I can compile the module for cobertura, but it turns out to cause a new problem.

Some of the annotation-generated sources for this module are used by another module, and the class files aren't found. What changed? The generated-sources directory contains the java files, but the classes aren't compiled.

It looked at one point like changing the build-helper phase to generate-sources from process-sources helped, but that failed subsequently.

Is there something else that needs to be changed because of changes between 3.1 and 3.6.0? (I'm not familiar with how to process annotations - I'm just the Cobertura support guy.)

pom file:

<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>

Nomenclator answered 7/3, 2018 at 20:23 Comment(0)
N
1

I resolved this by removing the -proc:none compiler argument from the compiler plugin. With that present none of the generated sources were being compiled at all. With the 3.1 plugin I had to have that, but with 3.6.0 I can't.

I also tried to implement the answer https://mcmap.net/q/1626962/-how-do-i-run-annotation-processing-via-maven-3-3 by making the compilerArg specific to the default-compile phase, but that didn't compile the generated sources. If I didn't reuse the default-compile id the build worked and gave me generated class files, but it ran two compile phases, with the -proc:none one second, which seemed redundant.

The final pom section for the compiler looks like this:

<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>
Nomenclator answered 9/3, 2018 at 15:54 Comment(7)
Re: making the compilerArg specific to default-compile, that's difficult to know what the problem is without seeing the actual configuration that you tried. Did you remove the -proc:none from the maven-compiler-plugin configuration? Did you add a version of 3.6.0 (because the latest version is 3.7.0)? Try making a MCVE of that in a separate question.Tristichous
@Tristichous I added the default-compile section exactly as in your referenced answer, and removed the configuration -proc:none from the top of my section. That didn't work, I still didn't get generated class files. When I used your suggestion but left out the ID the build worked but ran compile twice. I added version 3.6.0 in my master pom and removed the version from about 90 child poms that had 4 different versions among them.Nomenclator
I can't even think about making a MCVE for this. The module I have is 8800 LOC in about 50 files, and I don't know anything about the code to be able to strip out a useful piece. :( Could the build-helper be doing anything here?Nomenclator
I understand, but "that didn't work" is pretty vague to go off of, so it's difficult to provide a specific answer. However, based on what you have stated, I would guess that your build use multiple annotation processors (e.g., one phase that uses annotation processing in the generation of sources, and another phase whose annotation processing acts on the generated sources from the previous phase). That would explain why removing the -proc:none from the default-compile execution works. It doesn't explain why you would see different results from 3.1 and 3.6.0 though.Tristichous
I was going to mark my answer as accepted, but I'll leave it another few days to see if anyone can explain why I have to change the annotation processing parameters between 3.1 and 3.6.0/3.7.0. I wonder if it could have anything to do with the March 2010 1.0.0.Final version of hibernate-jpamodelgen? I may play with upgrading that when I have more time.Nomenclator
Another guess, maybe having the -proc:none in both the plugin configuration and the execution configuration was actually excluding it from the call to javac? Trying out intermediate versions between 3.1 and 3.6.0 would help narrow down exactly what version of the Maven Compiler Plugin introduced the change that broke your build.Tristichous
Just tried upgrading to 3.8.0, but had to downgrade back to 3.1 because my generated sources weren't being compiled.Ondrej

© 2022 - 2024 — McMap. All rights reserved.