【问题标题】:Proper Lifecycle Order For Annotation Processing In MavenMaven 中注释处理的正确生命周期顺序
【发布时间】:2021-04-27 07:25:53
【问题描述】:

我目前正在开发一个 java 项目,我需要在其中生成和编译 JPA 元模型类作为构建的一部分。我做了一些研究并在这里找到了答案:Generate the JPA metamodel files using maven-processor-plugin - What is a convenient way for re-generation? 这似乎是一个合理的解决方案。问题是,我的项目还包含一些需要与 java.util 一起编译的 groovy 类。如果我启用 maven-processor-plugin,maven 构建将在遇到依赖于 groovy 类的 java 类时立即失败。查看控制台输出,我可以看到 maven-processor-plugin 在 groovy 编译器之前运行,因此那些 groovy 类没有机会被编译。

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

这是我的 pom.xml 的 sn-p:

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

【问题讨论】:

  • 处理器插件是否在您的 Java 源代码上运行?编译之后会发生吗?如果需要先编译,我认为处理器运行时相位控制的选择。

标签: java maven jpa groovy annotations


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-12-04
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    相关资源
    最近更新 更多