Java 编译文档提示 text blocks are not supported in -source 11 错误

错误的日志信息为:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:3.6.3:jar (attach-javadocs) on project core-java-strings: MavenReportException: Error while generating Javadoc: 
[ERROR] Exit code: 1
[ERROR] C:\WorkDir\Repository\iSharkfly-Docs\java-tutorials\core-java-modules\core-java-strings\src\main\java\com\baeldung\multiline\MultiLineString.java:68: error: text blocks are not supported in -source 11
[ERROR]         return """
[ERROR]                ^
[ERROR]   (use -source 15 or higher to enable text blocks)
[ERROR] 1 error
[ERROR] Command line was: cmd.exe /X /C "C:\Progra~1\Semeru\jdk-17.0.6.10-openj9\bin\javadoc.exe @options @packages"
[ERROR]
[ERROR] Refer to the generated Javadoc files in 'C:\WorkDir\Repository\iSharkfly-Docs\java-tutorials\core-java-modules\core-java-strings\target\apidocs' dir.
[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/MojoExecutionException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <args> -rf :core-java-strings

问题和原因

出现上面错误信息的原因是在Java 代码编译的时候我们使用了一些在 JDK-17 中才有的属性。

但是生成的 Doc 文档却是使用的是 JDK 11。

我们需要做的就是在 maven-javadoc-plugin 插件中指定 JDK 的版本。

对插件添加下面的内容:

                <configuration>
                    <release>${java.version}</release>
                    <encoding>UTF-8</encoding>
                </configuration>

因为我们上面使用了全局配置变量,所以这里用的是变量类型,你也可以在这里直接使用数字 17。

配置参数:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>${maven-javadoc-plugin.version}</version>
                <configuration>
                    <release>${java.version}</release>
                    <encoding>UTF-8</encoding>
                </configuration>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

把上图中的配置参数添加到 POM 文件中。