gpt4 book ai didi

java - 如何从注释处理器的环境中排除 Maven 依赖项?

转载 作者:行者123 更新时间:2023-12-02 01:57:33 25 4
gpt4 key购买 nike

上下文

我有代码使用 maven-processor-plugin 让 Hibernate 在文件夹 src/main/generated 中生成一些类Java 项目的一部分。我正在准备将项目迁移到 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。

经过进一步调查,我发现Context org.hibernate:hibernate-jpamodelgen:5.4.4.Final 中的类有以下代码:

        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.processing.Generatedjavax.annotation.Generated )。类型。我猜前提是该类型在 Java 9+ 环境中不存在。

但不幸的是,对我来说, javax.annotation:jsr250-api 是由 org.bsc.maven:maven-processor-plugin 间接拉取的,作为我项目构建环境的传递依赖项。

因此,我的问题是:如何从传递给 Hibernate 注释处理器的环境中排除传递依赖项?

我还想知道我是否应该向 Hibernate 或 maven-processor-plugin 项目报告这个问题。

最佳答案

我所做的工作如下所示。

需要注意的是,根插件(本例中为 maven-processor-plugin)和要排除的依赖项(本例中为 jsr250-api)之间需要有一个中间 Artifact 。

我之前的错误是我再次列出了 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>

关于java - 如何从注释处理器的环境中排除 Maven 依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57390658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com