Maven 中注释处理的正确生命周期顺序

Proper Lifecycle Order For Annotation Processing In Maven

我目前正在处理一个 java 项目,我需要在该项目中生成和编译 JPA 元模型 classes 作为构建的一部分。我做了一些研究并在这里找到了答案: 这似乎是一个合理的解决方案。问题是,我的项目还包含一些需要与 java 一起编译的 groovy classes。如果我启用 maven-processor-plugin,maven 构建将在遇到依赖于 groovy class 的 java class 时立即失败。查看控制台输出,我可以看到 maven-processor-plugin 在 groovy 编译器之前是 运行,所以那些 groovy classes 没有机会成为已编译。

有谁知道有没有好的方法来处理这个问题?有什么方法可以将编译过程分成几个阶段,以便我可以控制什么时候处理什么?

这是我的 pom.xml 的片段:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>              
            <configuration>
                <showWarnings>false</showWarnings>
                <compilerId>groovy-eclipse-compiler</compilerId>
               <compilerArgument>-proc:none</compilerArgument>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-eclipse-compiler</artifactId>
                    <version>3.6.0-03</version>
                  </dependency>
                  <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-eclipse-batch</artifactId>
                    <version>3.0.7-02</version>
                  </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
           <version>4.5-jdk8</version>
            <executions>
                <execution>
                    <id>process</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <outputDirectory>${project.build.directory}/../src/main/generated-sources/java/jpametamodel</outputDirectory>
                        <processors>
                            <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                        </processors>
                        <overwrite>true</overwrite>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-jpamodelgen</artifactId>                     
                    <version>5.3.13.Final</version>                     
                </dependency>                  
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>          
            <executions>
              <execution>
                <id>add-source</id>
                <phase>generate-sources</phase>
                <goals>
                  <goal>add-source</goal>
                </goals>
                <configuration> 
                  <sources>               
                    <source>${project.build.directory}/../src/main/generated-sources/java/jpametamodel</source>                
                  </sources>                
                </configuration>
              </execution>
            </executions>
          </plugin>       
    </plugins>
</build>

经过反复试验,我终于找到了一个似乎可行的解决方案。 maven-processor-plugin 可以使用 include/exclude 过滤器来限制它查看的文件的范围。我添加了一个包含过滤器,将处理限制在我的域 类。现在,当我构建它时,它可以处理我的注释 类 而不会挂在 groovy 文件上。

我的最终结果是这样的:

          <plugin>
                <groupId>org.bsc.maven</groupId>
                <artifactId>maven-processor-plugin</artifactId>
               <version>4.5-jdk8</version>
                <executions>
                    <execution>
                        <id>process</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                        <includes>
                            <include>com/tura/product/domain/*.java</include>
                        </includes>
                            <outputDirectory>${project.build.directory}/generated-sources/java</outputDirectory>
                            <processors>
                                <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                            </processors>
                            <overwrite>true</overwrite>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-jpamodelgen</artifactId>                     
                        <version>5.3.13.Final</version>                     
                    </dependency>                  
                </dependencies>
            </plugin>