上下文
我有代码使用 maven-processor-plugin 让 Hibernate 在 Java 项目的文件夹 src/main/generated
中生成一些类。我正在准备在开发环境和运行时环境中将项目迁移到 Java 11。
到目前为止,Hibernate 4.3.10.Final 和 Java 8 一切正常。在安装 JDK 11 并使用 Hibernate 5.4.4.Final 后,我遇到了臭名昭著的javax.annotation.Generated
问题。
由于Hibernate 5.4.4.Final应该与Java 9+兼容,我希望生成的文件切换到javax.annotation.processing.Generated
。不幸的是,旧的限定名称是我仍然可以在导入部分的那些文件中看到的名称。因此,生成的文件的编译将失败。
这是我配置的内容:
- maven-processor-plugin,带有
<releaseVersion>11</releaseVersion>
- 以 pom.xml 为单位的目标级别,带有
<maven.compiler.target>1.11</maven.compiler.target>
- 在pom.xml中发布,
<maven.compiler.release>11</maven.compiler.release>
- 在 Eclipse 中,Java 编译器使用"--release"选项并设置为合规性级别"11"。
- 在系统上,只安装了一个JDK:11.0.4。
经过进一步调查,我意识到org.hibernate:hibernate-jpamodelgen:5.4.4.final中的Context
类具有以下代码:
TypeElement java8AndBelowGeneratedAnnotation =
pe.getElementUtils().getTypeElement( "javax.annotation.Generated" );
if ( java8AndBelowGeneratedAnnotation != null ) {
generatedAnnotation = java8AndBelowGeneratedAnnotation;
}
else {
// Using the new name for this annotation in Java 9 and above
generatedAnnotation = pe.getElementUtils().getTypeElement( "javax.annotation.processing.Generated" );
}
问题
本质上,Hibernate注解处理器试图通过尝试获取对Java 8 javax.annotation.Generated
类型的引用来确定在生成的文件中输出哪个正确的注解(javax.annotation.Generated
和javax.annotation.processing.Generated
(。我想前提是这种类型在Java 9+环境中是不存在的。
但不幸的是,javax.annotation:jsr250-api 被 org.bsc.maven:maven-processor-plugin 间接拉取,作为可传递的依赖项进入我的项目的构建环境。
因此,我的问题是:如何从传递给 Hibernate 注释处理器的环境中排除传递依赖项?
我也想知道这是否是一个我应该向 Hibernate 或 maven-processor-plugin 项目报告的问题。
我所做的工作如下所示。
需要注意的是,在根插件(在本例中为 maven-processor-plugin(和要排除的依赖项(在本例中为 jsr250-api(之间需要有一个中间工件。
我之前的错误是我再次列出 maven-processor-plugin 代替 maven-plugin-api,这当然没有意义。
<pluginManagement>
...
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>${maven-processor-plugin-version}</version>
<configuration>
<releaseVersion>${maven.compiler.release}</releaseVersion>
</configuration>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<outputDirectory>src/main/generated</outputDirectory>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven-plugin-api-version}</version>
<exclusions>
<!--
Exclude this artifact because it defines 'javax.annotation.Generated', which makes the
generated source code incompatible with Java 11.
-->
<exclusion>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</plugin>
...
</pluginManagement>