33

我需要在我的项目源上运行注释处理器。注释处理器不应成为项目的传递依赖项,因为它仅用于注释处理而没有其他需要。

这是我用于此的完整(非工作)测试 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>test</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Test annotations</name>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <hibernate-jpamodelgen.version>1.2.0.Final</hibernate-jpamodelgen.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>6.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <annotationProcessors>
            <annotationProcessor>
              org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</annotationProcessor>
          </annotationProcessors>
          <debug>true</debug>
          <optimize>true</optimize>
          <source>1.6</source>
          <target>1.6</target>
          <compilerArguments>
            <AaddGeneratedAnnotation>true</AaddGeneratedAnnotation>
            <Adebug>true</Adebug>
          </compilerArguments>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>${hibernate-jpamodelgen.version}</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

我在插件配置中明确定义org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor为注释处理器以进行测试,并且我知道它不应该是必需的。

我遇到的问题是hibernate-jpamodelgen依赖项没有添加到编译器类路径中,因此找不到注释处理器并且构建失败。

根据这个答案,我尝试将依赖项添加为构建扩展(不确定我是否理解那些应该是什么!),如下所示:

<extensions>
  <extension>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
    <version>${hibernate-jpamodelgen.version}</version>
  </extension>
</extensions>

这也不会添加hibernate-jpamodelgen到编译器类路径中。

到目前为止,我发现唯一有效的是将依赖项添加到该<dependencies>部分的项目中。这有一个不幸的副作用,hibernate-jpamodelgen即我想避免之后添加作为传递依赖项。

我以前的工作设置使用maven-processor-plugin插件来实现我想要的。但是,eclipse m2e 不支持此插件,并且最新版本的maven-compiler-pluginnow 可以正确处理多个编译器参数,因此我宁愿使用后者。

4

7 回答 7

36

annotationProcessorPaths选项可以在最新版本的 Maven 编译器插件中使用:

<pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <annotationProcessorPaths>
                    <annotationProcessorPath>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-jpamodelgen</artifactId>
                        <version>5.2.6.Final</version>
                    </annotationProcessorPath>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

这样,处理器就与实际的项目依赖项分开了。如果为项目启用了注释处理,Eclipse M2E 插件也会选择此选项。

于 2017-01-21T09:42:13.523 回答
26

将依赖项添加为可选依赖项( <optional>true</optional>)。这将在编译下添加依赖项,但会阻止它成为传递依赖项:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-jpamodelgen</artifactId>
  <version>${hibernate-jpamodelgen.version}</version>
  <optional>true</optional>
</dependency>

如果您要在此模块中创建一个包含所有依赖项的工件(如 .war),则可以使用 .war <scope>provided</scope>。这既可以防止依赖项具有传递性,也可以防止依赖项包含在模块生成的工件中。

于 2013-01-14T16:53:59.317 回答
11

对于 JDK 10,我真的不得不有点疯狂才能让它工作,希望有人觉得这很有用

<jaxb.version>2.3.0</jaxb.version>
<maven.hibernate.version>5.3.2.Final</maven.hibernate.version>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven.compiler.version}</version>
    <goals>
        <goal>compile</goal>
    </goals>
    <configuration>
        <outputDirectory>${project.build.directory}/generated-sources/annotations</outputDirectory>
        <annotationProcessorPaths>
            <annotationProcessorPath>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-jpamodelgen</artifactId>
                <version>${maven.hibernate.version}</version>
            </annotationProcessorPath>
            <annotationProcessorPath>
                <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api</artifactId>
                <version>${jaxb.version}</version>
            </annotationProcessorPath>
        </annotationProcessorPaths>
        <annotationProcessors>
            <annotationProcessor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</annotationProcessor>
        </annotationProcessors>
        <compilerArgs>
            <arg>-AaddGeneratedAnnotation=false</arg>
        </compilerArgs>
        <compilerArguments>
            <AaddGeneratedAnnotation>false</AaddGeneratedAnnotation>
            <Adebug>true</Adebug>
        </compilerArguments>
        <failOnError>true</failOnError>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>${maven.hibernate.version}</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>${jaxb.version}</version>
            <type>jar</type>
        </dependency>
    </dependencies>
</plugin>
于 2018-07-18T19:14:25.657 回答
10

问题出在3.*版本的maven-compiler-plugin. 它的行为与2.*版本有点不同。特别是,似乎maven-compiler-plugin3.*没有将其依赖项和构建扩展添加到类路径,因为它使用 javax.tools 工具来运行编译过程。要恢复旧行为,maven-compiler-plugin您应该使用新的配置属性forceJavacCompilerUse

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
    <forceJavacCompilerUse>true</forceJavacCompilerUse>
</configuration>
....
</plugin>
于 2014-07-02T12:12:44.697 回答
4

请看一下jpa-metamodels-with-maven

对于更多的访问者,我发现 maven-compiler-plugin 3.x 系列有一些显着的变化。

我就是这样做的。(我是你链接的那个)

关键是我的解决方案不适用于那些 3.x 系列的 maven-compiler-plugin。

<project ...>

  <build>

    <extensions>
      <extension>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jpamodelgen</artifactId>
        <version>1.3.0.Final</version>
      </extension>
    </extensions>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5.1</version> <!-- See this? -->
      </plugin>
    </plugins>

  </build>
</project>
于 2013-09-23T07:01:54.947 回答
2

不确定您遇到了哪种构建错误,但这是我的情况:我在 Idea 中遇到以下编译错误: Annotation processor 'org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor' not found error

但是,当从 Maven 编译时,一切都很好。

所以,我的问题是我在 Idea 设置中的配置错误。特别是,Idea 似乎以某种方式检测到处理器并将其放入模块处理器配置文件的设置中。在这里讨论。

我将其修复如下:

  1. 转到想法 > 设置 > 注释处理器。
  2. 对于每个处理器配置文件,请确保:
    • Enable annotation processing 是 Yes;
    • 右侧列表中没有您有错误的注释处理器 FQ 名称 (ei "JPAMetaModelEntityProcessor")。如果它在那里列出,只需选择并单击“-”减号按钮即可将其删除。
于 2015-07-03T12:51:20.937 回答
-1

我认为这是在配置文件中包含此类依赖项以解决此类问题的更好方法。

<profile>
    <id>XXX-profile</id>
    <dependencies>
         <dependency>
             // XXX artifact path
         </dependency>
    </dependencies>
</profile>
于 2015-09-18T15:11:40.517 回答