我正在將我的 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 的任何地方直接指定該插件。所以什么東西,冥冥中牽引它并使用它,我的猜測是,我必須更新任何這件事是這樣,它在拉的一個版本,maven-compiler-plugin這是與Java兼容11。
那么我怎么知道是什么在吸引它并使用它呢?或者,如果這不是正在發生的事情,那是什么,我該如何解決?
uj5u.com熱心網友回復:
替換<java.version>1.11</java.version>為<java.version>11</java.version>
uj5u.com熱心網友回復:
tl;博士
去掉1.前綴。
<java.version>11</java.version>
細節
Java 9 中的新版本控制方案
從Java 9及更高版本開始,Java 版本編號方案已更改以洗掉永不改變的1.*.
有關官方檔案和解釋,請參閱JEP 223:新版本字串方案,從版本號中洗掉初始 1 元素部分。
參考 JEP 223:
該提案從 JDK 版本號中洗掉了最初的 1 元素。也就是說,它表明 JDK 9 的第一個版本將具有版本號 9.0.0 而不是 1.9.0.0。
近二十年后,很明顯當前版本號方案的第二個元素是 JDK 的事實上的
$MAJOR版本號。
因此你的 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),將來我可能會洗掉第一個元素<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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/357286.html
