Monday, March 14, 2022

[SOLVED] Why annotation processing run multiple times during maven build

Issue

I'm currently using java annotation processing to generate a lot of code (20 seconds), when I run mvn process-resources it will trigger apt only once however when I use mvn package, mvn jetty:run or mvn gwt:run apt been triggered again after war been packaged. Is there a way to force apt only run once?

I have a profile in my pom.xml

<profile>
    <id>codegen</id>
    <activation>
        <activeByDefault>false</activeByDefault>
        <file>
            <exists>src/main/resources/META-INF</exists>
        </file>
    </activation>
    <dependencies>
        <dependency>
            <groupId>com.regulationworks.core.code-gen</groupId>
            <artifactId>executor</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.bsc.maven</groupId>
                <artifactId>maven-processor-plugin</artifactId>
                <version>2.0.5</version>
                <executions>
                    <execution>
                        <id>generate-jpa-metamodel</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <processors>
                                <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                            </processors>
                            <outputDirectory>${project.build.directory}/generated-sources/metamodel</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>generate-gwt-jpa-proxies</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <processors>
                                <processor>org.hibernate.gwtmodelgen.GwtProxyModelEntityProcessor</processor>
                            </processors>
                            <outputDirectory>${project.build.directory}/generated-sources/entity-proxies</outputDirectory>
                        </configuration>
                    </execution>

                    <execution>
                        <id>code-gen-executor</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <processors>
                                <processor>com.regulationworks.core.gwt.codegen.CodeGenExecutor</processor>
                            </processors>

                            <options>
                                <generateRequestFactory>${core.codegen.requestfactory}</generateRequestFactory>
                                <generateMappingXml>${core.codegen.xmlmapping}</generateMappingXml>
                                <codegen.scaffold>${codegen.scaffold}</codegen.scaffold>
                                <codegen.scaffold.override>${codegen.scaffold.override}</codegen.scaffold.override>
                                <codegen.scaffold.classes>${codegen.scaffold.classes}</codegen.scaffold.classes>
                                <codegen.commands.class.name>${codegen.commands.class.name}</codegen.commands.class.name>
                            </options>
                        </configuration>
                    </execution>
                    <!--<execution>
                               <id>generate-gwt-rf</id>
                               <goals>
                                   <goal>process</goal>
                               </goals>
                               <phase>process-classes</phase>
                               <configuration>
                                   <processors>
                                       <processor>com.google.web.bindery.requestfactory.apt.RfValidator</processor>
                                   </processors>
                                   <options>
                                   <rootOverride>${core.codegen.rfvalidate}</rootOverride>
                                   </options>
                                   <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                               </configuration>
                           </execution>-->
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2</version>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <configuration>
                            <id>VerifyRequestFactoryInterfaces</id>
                            <executable>java</executable>
                            <arguments>
                                <argument>-cp</argument>
                                <classpath />
                                <argument>com.google.web.bindery.requestfactory.apt.ValidationTool</argument>
                                <argument>${project.build.outputDirectory}</argument>
                                <argument>${core.codegen.rfvalidate}</argument>
                            </arguments>
                            <skip>${core.codegen.rfvalidate}</skip>
                        </configuration>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
</profile>

and maven-source-plugin been configured to 'jar-no-fork' (this helped to remove one time when run normal mvn install)

    <plugin>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.1.2</version>
        <executions>
            <execution>
                <goals>
                    <goal>jar-no-fork</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

here is output in console when run "mvn clean package gwt:run"

X:\workspace\Decision\decision-app>mvn clean package gwt:run
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building DecisionWorks App 2.0.2-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for org.opensaml:opensaml:jar:1.1b is missing, no dependency information available
[WARNING] The POM for net.sf.saxon:saxon-dom:jar:8.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ decision-app ---
[INFO] Deleting X:\workspace\Decision\decision-app\target
[INFO]
***********************************************************
 This is where apt executed first time which look right
***********************************************************
[INFO] --- maven-processor-plugin:2.0.5:process (generate-jpa-metamodel) @ decision-app ---
[INFO] Source directory: X:\workspace\Decision\decision-app\target\generated-sources\metamodel added
[INFO] javac option: -cp
[INFO] javac option: ...
[INFO] javac option: -proc:only
[INFO] javac option: -processor
[INFO] javac option: org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor
[INFO] javac option: -d
[INFO] javac option: X:\workspace\Decision\decision-app\target\classes
[INFO] javac option: -s
[INFO] javac option: X:\workspace\Decision\decision-app\target\generated-sources\metamodel
[INFO] diagnostic Note: Hibernate JPA 2 Static-Metamodel Generator 1.0.0.Final
[INFO]
[INFO] --- maven-processor-plugin:2.0.5:process (generate-gwt-jpa-proxies) @ decision-app ---
[INFO] Source directory: X:\workspace\Decision\decision-app\target\generated-sources\entity-proxies added
[INFO] javac option: -cp
[INFO] javac option: ...
[INFO] javac option: -proc:only
[INFO] javac option: -processor
[INFO] javac option: org.hibernate.gwtmodelgen.GwtProxyModelEntityProcessor
[INFO] javac option: -d
[INFO] javac option: X:\workspace\Decision\decision-app\target\classes
[INFO] javac option: -s
[INFO] javac option: X:\workspace\Decision\decision-app\target\generated-sources\entity-proxies
[INFO] diagnostic Note: Hibernate JPA 2 Static-Metamodel Generator 3.7.0-SNAPSHOT
[INFO]
[INFO] --- maven-processor-plugin:2.0.5:process (code-gen-executor) @ decision-app ---
[INFO] Source directory: X:\workspace\Decision\decision-app\target\generated-sources\apt added
[INFO] Adding compiler arg: -Acodegen.scaffold.override=true
[INFO] javac option: -cp
[INFO] javac option: ...
[INFO] javac option: -proc:only
[INFO] javac option: -Acodegen.scaffold.override=true
[INFO] javac option: -processor
[INFO] javac option: com.regulationworks.core.gwt.codegen.CodeGenExecutor
[INFO] javac option: -d
[INFO] javac option: X:\workspace\Decision\decision-app\target\classes
[INFO] javac option: -s
[INFO] javac option: X:\workspace\Decision\decision-app\target\generated-sources\apt
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ decision-app ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 10 resources
[INFO] Copying 17 resources
[INFO] Copying 80 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ decision-app ---
[INFO] Compiling 260 source files to X:\workspace\Decision\decision-app\target\classes
[INFO]
[INFO] --- exec-maven-plugin:1.2:exec (default) @ decision-app ---
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ decision-app ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO] Copying 3 resources
[INFO] Copying 10 resources
[INFO] Copying 17 resources
[INFO] Copying 80 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ decision-app ---
[INFO] Compiling 3 source files to X:\workspace\Decision\decision-app\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.9:test (default-test) @ decision-app ---
[INFO] Surefire report directory: X:\workspace\Decision\decision-app\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
...
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.172 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ decision-app ---
[INFO] Packaging webapp
[INFO] Assembling webapp [decision-app] in [X:\workspace\Decision\decision-app\target\decision]
[INFO] Processing war project
[INFO] Copying webapp webResources [X:\workspace\Decision\decision-app\target] to [X:\workspace\Decision\decision-app\target\decision]
[INFO] Copying webapp webResources [X:\workspace\Decision\decision-app\src/main/webapp/WEB-INF] to [X:\workspace\Decision\decision-app\target\decision]
[INFO] Copying webapp resources [X:\workspace\Decision\decision-app\src\main\webapp]
[INFO] Processing overlay [ id com.regulationworks.core:document-cmis-plugin-config]
[INFO] Webapp assembled in [10118 msecs]
[INFO] Building war: X:\workspace\Decision\decision-app\target\decision.war
[WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored
(webxml attribute is missing from war task, or ignoreWebxml attribute is specified as 'true')
[INFO]
[INFO] --- maven-source-plugin:2.1.2:jar-no-fork (default) @ decision-app ---
[INFO]
[INFO] >>> gwt-maven-plugin:2.3.0:run (default-cli) @ decision-app >>>
***********************************************************
This is where apt executed second time which is a waste of time
***********************************************************
[INFO]
[INFO] --- maven-processor-plugin:2.0.5:process (generate-jpa-metamodel) @ decision-app ---
[INFO] Source directory: X:\workspace\Decision\decision-app\target\generated-sources\metamodel added
[INFO] javac option: -cp
[INFO] javac option: ...
[INFO] javac option: -proc:only
[INFO] javac option: -processor
[INFO] javac option: org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor
[INFO] javac option: -d
[INFO] javac option: X:\workspace\Decision\decision-app\target\classes
[INFO] javac option: -s
[INFO] javac option: X:\workspace\Decision\decision-app\target\generated-sources\metamodel
[INFO] diagnostic Note: Hibernate JPA 2 Static-Metamodel Generator 1.0.0.Final
[INFO]
[INFO] --- maven-processor-plugin:2.0.5:process (generate-gwt-jpa-proxies) @ decision-app ---
[INFO] Source directory: X:\workspace\Decision\decision-app\target\generated-sources\entity-proxies added
[INFO] javac option: -cp
[INFO] javac option: ...
[INFO] javac option: -proc:only
[INFO] javac option: -processor
[INFO] javac option: org.hibernate.gwtmodelgen.GwtProxyModelEntityProcessor
[INFO] javac option: -d
[INFO] javac option: X:\workspace\Decision\decision-app\target\classes
[INFO] javac option: -s
[INFO] javac option: X:\workspace\Decision\decision-app\target\generated-sources\entity-proxies
[INFO] diagnostic Note: Hibernate JPA 2 Static-Metamodel Generator 3.7.0-SNAPSHOT
[INFO]
[INFO] --- maven-processor-plugin:2.0.5:process (code-gen-executor) @ decision-app ---
[INFO] Source directory: X:\workspace\Decision\decision-app\target\generated-sources\apt added
[INFO] Adding compiler arg: -Acodegen.scaffold.override=true
[INFO] javac option: -cp
[INFO] javac option: ...
[INFO] javac option: -proc:only
[INFO] javac option: -Acodegen.scaffold.override=true
[INFO] javac option: -processor
[INFO] javac option: com.regulationworks.core.gwt.codegen.CodeGenExecutor
[INFO] javac option: -d
[INFO] javac option: X:\workspace\Decision\decision-app\target\classes
[INFO] javac option: -s
[INFO] javac option: X:\workspace\Decision\decision-app\target\generated-sources\apt
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ decision-app ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 10 resources
[INFO] Copying 17 resources
[INFO] Copying 80 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ decision-app ---
[INFO] Compiling 60 source files to X:\workspace\Decision\decision-app\target\classes
[INFO]
[INFO] --- exec-maven-plugin:1.2:exec (default) @ decision-app ---
[INFO]
[INFO] <<< gwt-maven-plugin:2.3.0:run (default-cli) @ decision-app <<<
[INFO]
[INFO] --- gwt-maven-plugin:2.3.0:run (default-cli) @ decision-app ---
***********************************************************
finally be able to start the web app, 30 seconds wasted for second apt execution
***********************************************************

----------------------- solution -----------------------

  1. add a "package" profile

    <profile>
        <id>package</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.1.1</version>
                    <configuration>
                        <packagingExcludes>WEB-INF/lib/*-sources.jar, WEB-INF/lib/gwt-user-*.jar</packagingExcludes>
                        <webResources>
                            <resource>
                                <directory>src/main/webapp/WEB-INF</directory>
                                <targetPath>WEB-INF</targetPath>
                                <includes>
                                    <include>**/*.xml</include>
                                </includes>
                                <filtering>true</filtering>
                            </resource>
                        </webResources>
                    </configuration>
                    <executions>
                        <execution>
                            <id>explode-war</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>exploded</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    
  2. use "mvn clean gwt:run -Ppackage" instead of "mvn clean package gwt:run"


Solution

The command you executed is mvn clean package gwt:run. Maven executes the clean lifecycle (clean:clean goal by default). Then it executes package, which runs all goals bound to the package phase and all goals bound to previous phases (generate-sources, process-sources, process-resources, etc.). Finally, it executes the gwt:run goal. If you look at the documentation for the run goal there is one very important line:

Invokes the execution of the lifecycle phase process-classes prior to executing itself.

This means that Maven will run all goals bound to the process-classes phase and all earlier phases again. The apt plugin executions are bound to the generate-sources phase so they repeat, as do resources:resources, compiler:compile, and exec:exec as they are all bound to process-classes or before.

With that in mind: does mvn clean gwt:run work? I know that mvn clean jetty:run works as I expect without duplicating executions, but I've not used gwt before.



Answered By - user944849
Answer Checked By - Senaida (WPSolving Volunteer)