出现的场景
- 在使用使用开源项目 jimmer-examples-main 的时候,导入项目 mvn clean install的时候出现下边的错误
问题详情
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ service ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ service ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 7 source files to C:\Users\admin\Desktop\temp\2024.09.12\jimmer-examples-main\java\jimmer-sql\service\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] In order to parse the `@org.babyfish.jimmer.client.FetchBy` annotations that decorate generic type parameters, please make sure the java compiler version is 11 or higher (`source.version` and `target.version` can still remain `1.8`). However, once compilation is complete, you can still use Java 8 to deploy and run the project. If you want to suppress this error(Note, this will lead to generating incorrect client code such as openapi and typescript), please add the argument `-Ajimmer.client.ignoreJdkWarning=true` to java compiler by maven or gradle
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for jimmer-sql 0.8.155:
[INFO]
[INFO] jimmer-sql ......................................... SUCCESS [ 0.138 s]
[INFO] model .............................................. SUCCESS [ 1.771 s]
[INFO] repository ......................................... SUCCESS [ 0.575 s]
[INFO] runtime ............................................ SUCCESS [ 0.404 s]
[INFO] service ............................................ FAILURE [ 0.750 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.768 s
[INFO] Finished at: 2024-09-13T07:26:57+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project service: Compilation failure
[ERROR] In order to parse the `@org.babyfish.jimmer.client.FetchBy` annotations that decorate generic type parameters, please make sure the java compiler version is 11 or higher (`source.version` and `target.version` can still remain `1.8`). However, once compilation is complete, you can still use Java 8 to deploy and run the project. If you want to suppress this error(Note, this will lead to generating incorrect client code such as openapi and typescript), please add the argument `-Ajimmer.client.ignoreJdkWarning=true` to java compiler by maven or gradle
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR] mvn <args> -rf :service
Process finished with exit code 1
快速解决问题方案
核心配置
<compilerArgs>
<arg>-Ajimmer.client.ignoreJdkWarning=true</arg>
</compilerArgs>
完整的代码
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- <version>3.10.1</version>-->
<version>3.8.1</version> <!-- 确保使用支持Java 11的插件版本 -->
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgs>
<arg>-Ajimmer.client.ignoreJdkWarning=true</arg>
</compilerArgs>
<!-- <release>11</release> <!– 使用Java 11的特性进行编译 –>-->
<annotationProcessorPaths>
<path>
<groupId>org.babyfish.jimmer</groupId>
<artifactId>jimmer-apt</artifactId>
<version>${jimmer.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
</plugin>
</plugins>
</build>
解决完之后的效果图
问题原因
在处理使用 @org.babyfish.jimmer.client.FetchBy
注解来装饰泛型类型参数的Java项目时,确实需要确保Java编译器的版本至少是11,尽管你的源代码(source.version
)和目标字节码(target.version
)版本可以设置为1.8以兼容Java 8环境。这是因为某些Java 11引入的特性(如类型注解的改进)可能对于正确解析这些注解是必要的。
然而,编译后的代码(即字节码)仍然可以在Java 8环境中运行,因为Java平台的设计保证了向后兼容性。这意味着,只要你的代码在编译时没有使用Java 11及以上版本特有的API,那么它就可以在Java 8环境中执行。
解决方案
-
升级Java编译器版本:
确保你的构建工具(如Maven或Gradle)配置为使用Java 11或更高版本的JDK进行编译。这通常涉及到设置JAVA_HOME
环境变量,并在构建配置文件中指定JDK版本。 -
保持源代码和目标字节码版本:
你可以在Maven的pom.xml
或Gradle的build.gradle
文件中设置maven-compiler-plugin
或java
插件的source
和target
属性为1.8,以确保生成的字节码与Java 8兼容。Maven示例:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <!-- 确保使用支持Java 11的插件版本 --> <configuration> <source>1.8</source> <target>1.8</target> <release>11</release> <!-- 使用Java 11的特性进行编译 --> </configuration> </plugin>
注意:
<release>
标签是Maven Compiler Plugin 3.6.0及以上版本引入的,用于指定用于编译的JDK版本,并自动设置-source
和-target
参数。但请注意,并非所有IDE都支持<release>
标签,且它可能不适用于所有情况。 -
抑制JDK版本警告:
如果你确定即使使用Java 11的特性进行编译也不会影响生成的客户端代码(如OpenAPI和TypeScript代码)的正确性,并且你希望忽略这个警告,你可以通过Maven或Gradle向Java编译器添加-Ajimmer.client.ignoreJdkWarning=true
参数。Maven示例(在
maven-compiler-plugin
配置中添加compilerArgs
):<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <compilerArgs> <arg>-Ajimmer.client.ignoreJdkWarning=true</arg> </compilerArgs> </configuration> </plugin>
但请注意,这样做可能会隐藏潜在的问题,因此建议仅在完全了解后果的情况下使用。
总之,确保使用正确的JDK版本进行编译,同时保持与旧版Java环境的兼容性,是处理此类问题的关键。