将 Maven 插件升级到 Java 11
Upgrading Maven plugins to Java 11
我正在将我的 Maven 构建的 Java 8 应用程序升级到 Java 11。在我的 POM 中我指定:
<properties>
<java.version>1.11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
当我使用正常的 Maven 构建调用构建我的应用程序时:
mvn verify -Plocal -Dspring-boot.run.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
我得到:
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< example.com:myapp-svc >------------------------
[INFO] Building myapp-svc 1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-checkstyle-plugin:3.1.2:check (validate) @ myapp-svc ---
[WARNING] Old version of checkstyle detected. Consider updating to >= v8.30
[WARNING] For more information see: https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/upgrading-checkstyle.html
[INFO] Starting audit...
Audit done.
[INFO] You have 0 Checkstyle violations.
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.7:prepare-agent (default) @ myapp-svc ---
[INFO] argLine set to -javaagent:/Users/myuser/.m2/repository/org/jacoco/org.jacoco.agent/0.8.7/org.jacoco.agent-0.8.7-runtime.jar=destfile=/Users/myuser/workspace/myapp-svc/target/jacoco.exec
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ myapp-svc ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 6 resources
[INFO] Copying 154 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ myapp-svc ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 456 source files to /Users/myuser/workspace/myapp-svc/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.076 s
[INFO] Finished at: 2021-11-12T12:52:52-05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project myapp-svc: Fatal error compiling: error: invalid target release: 1.11 -> [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
所以看起来 maven-compiler-plugin:3.8.1
插件与 Java 11 不兼容。问题是,我没有在我的 POM 中的任何地方直接指定该插件。所以某个地方正在拉动它并使用它,我的猜测是我必须更新任何 that 东西,以便它拉入 maven-compiler-plugin
的一个版本 是兼容Java11.
那么我怎么知道是什么在拉入和使用它呢?或者,如果那不是正在发生的事情,那是什么以及如何解决它?
将<java.version>1.11</java.version>
替换为<java.version>11</java.version>
tl;博士
删除 1.
前缀。
<java.version>11</java.version>
详情
Java9+
中的新版本控制方案
从 Java 9 开始及之后,Java 版本编号方案被更改以删除永不更改的 1.*
.
有关官方文档和说明,请参阅 JEP 223: New Version-String Scheme 部分 从版本号中删除初始 1 元素。
引用 JEP 223:
This proposal drops the initial 1 element from JDK version numbers. That is, it suggests that the first release of JDK 9 will have the version number 9.0.0 rather than 1.9.0.0.
After nearly twenty years it's clear that the second element of the current version-number scheme is the JDK's de facto $MAJOR
version number.
因此您的 POM 应该使用:
<java.version>11</java.version>
<maven.compiler.release>
顺便说一句,如果您将与编译器、构建和测试相关的 POM 的其他部分更新到最新版本,则可以替换一对元素 <maven.compiler.source>
和 <maven.compiler.target>
仅此一个元素:<maven.compiler.release>
.
因此您的代码段:
<properties>
<java.version>1.11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
…变成这样:
<properties>
<java.version>11</java.version>
<maven.compiler.release>${java.version}</maven.compiler.release>
</properties>
而在我自己的作品中,我什至没有 java.version
元素。我只使用这个片段,将 UTF-8 指定为字符编码,将 Java 17 指定为编译和构建版本。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
</properties>
随着 UTF-8 成为跨平台的新 Java 18 默认值(参见 JEP 400: UTF-8 by Default),将来我可能会删除第一个元素 <project.build.sourceEncoding>
。
<properties>
<maven.compiler.release>17</maven.compiler.release>
</properties>
这里是我修改后的版本 Apache Maven Quickstart Archetype,作为一个完整的例子。
<?xml version="1.0" encoding="UTF-8"?>
<!-- Modified by Basil Bourque as of 2021-11 to use the latest versions. -->
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>work.basil.example</groupId>
<artifactId>Quickie</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Quickie</name>
<url>http://www.basil.work</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
我正在将我的 Maven 构建的 Java 8 应用程序升级到 Java 11。在我的 POM 中我指定:
<properties>
<java.version>1.11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
当我使用正常的 Maven 构建调用构建我的应用程序时:
mvn verify -Plocal -Dspring-boot.run.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
我得到:
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< example.com:myapp-svc >------------------------
[INFO] Building myapp-svc 1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-checkstyle-plugin:3.1.2:check (validate) @ myapp-svc ---
[WARNING] Old version of checkstyle detected. Consider updating to >= v8.30
[WARNING] For more information see: https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/upgrading-checkstyle.html
[INFO] Starting audit...
Audit done.
[INFO] You have 0 Checkstyle violations.
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.7:prepare-agent (default) @ myapp-svc ---
[INFO] argLine set to -javaagent:/Users/myuser/.m2/repository/org/jacoco/org.jacoco.agent/0.8.7/org.jacoco.agent-0.8.7-runtime.jar=destfile=/Users/myuser/workspace/myapp-svc/target/jacoco.exec
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ myapp-svc ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 6 resources
[INFO] Copying 154 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ myapp-svc ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 456 source files to /Users/myuser/workspace/myapp-svc/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.076 s
[INFO] Finished at: 2021-11-12T12:52:52-05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project myapp-svc: Fatal error compiling: error: invalid target release: 1.11 -> [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
所以看起来 maven-compiler-plugin:3.8.1
插件与 Java 11 不兼容。问题是,我没有在我的 POM 中的任何地方直接指定该插件。所以某个地方正在拉动它并使用它,我的猜测是我必须更新任何 that 东西,以便它拉入 maven-compiler-plugin
的一个版本 是兼容Java11.
那么我怎么知道是什么在拉入和使用它呢?或者,如果那不是正在发生的事情,那是什么以及如何解决它?
将<java.version>1.11</java.version>
替换为<java.version>11</java.version>
tl;博士
删除 1.
前缀。
<java.version>11</java.version>
详情
Java9+
中的新版本控制方案从 Java 9 开始及之后,Java 版本编号方案被更改以删除永不更改的 1.*
.
有关官方文档和说明,请参阅 JEP 223: New Version-String Scheme 部分 从版本号中删除初始 1 元素。
引用 JEP 223:
This proposal drops the initial 1 element from JDK version numbers. That is, it suggests that the first release of JDK 9 will have the version number 9.0.0 rather than 1.9.0.0.
After nearly twenty years it's clear that the second element of the current version-number scheme is the JDK's de facto
$MAJOR
version number.
因此您的 POM 应该使用:
<java.version>11</java.version>
<maven.compiler.release>
顺便说一句,如果您将与编译器、构建和测试相关的 POM 的其他部分更新到最新版本,则可以替换一对元素 <maven.compiler.source>
和 <maven.compiler.target>
仅此一个元素:<maven.compiler.release>
.
因此您的代码段:
<properties>
<java.version>1.11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
…变成这样:
<properties>
<java.version>11</java.version>
<maven.compiler.release>${java.version}</maven.compiler.release>
</properties>
而在我自己的作品中,我什至没有 java.version
元素。我只使用这个片段,将 UTF-8 指定为字符编码,将 Java 17 指定为编译和构建版本。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
</properties>
随着 UTF-8 成为跨平台的新 Java 18 默认值(参见 JEP 400: UTF-8 by Default),将来我可能会删除第一个元素 <project.build.sourceEncoding>
。
<properties>
<maven.compiler.release>17</maven.compiler.release>
</properties>
这里是我修改后的版本 Apache Maven Quickstart Archetype,作为一个完整的例子。
<?xml version="1.0" encoding="UTF-8"?>
<!-- Modified by Basil Bourque as of 2021-11 to use the latest versions. -->
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>work.basil.example</groupId>
<artifactId>Quickie</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Quickie</name>
<url>http://www.basil.work</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>