Bas*_*que 7 java javafx maven maven-compiler-plugin openjfx
我使用org.openjfx:javafx-archetype-simpleMaven 原型启动了我的第一个 JavaFX 项目。
生成的 POM:
\n\n<project xmlns = "http://maven.apache.org/POM/4.0.0"\n xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"\n xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">\n <modelVersion>4.0.0</modelVersion>\n <groupId>com.example.invoicing</groupId>\n <artifactId>Invoicer</artifactId>\n <version>1.0-SNAPSHOT</version>\n <properties>\n <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>\n <maven.compiler.source>14</maven.compiler.source>\n <maven.compiler.target>14</maven.compiler.target>\n </properties>\n <dependencies>\n <dependency>\n <groupId>org.openjfx</groupId>\n <artifactId>javafx-controls</artifactId>\n <version>14</version>\n </dependency>\n </dependencies>\n <build>\n <plugins>\n <plugin>\n <groupId>org.apache.maven.plugins</groupId>\n <artifactId>maven-compiler-plugin</artifactId>\n <version>3.8.1</version>\n <configuration>\n <release>14</release> ???\n </configuration>\n </plugin>\n <plugin>\n <groupId>org.openjfx</groupId>\n <artifactId>javafx-maven-plugin</artifactId>\n <version>0.0.4</version>\n <configuration>\n <mainClass>com.example.invoicing.App</mainClass>\n </configuration>\n </plugin>\n </plugins>\n </build>\n</project>\nRun Code Online (Sandbox Code Playgroud)\n\n\xe2\x9e\xa5插件的<release>14</release>行的目的是什么?<configuration><artifactId>maven-compiler-plugin</artifactId>
我找到了 Maven 编译器插件的文档Compiling Your Java Sources。但它只提到<!-- put your configurations here -->。所以我对这里的具体配置选项一无所知。
编译时使用的是<release>14</release>Java版本吗?是OpenJFX的版本吗?还有别的事吗?
我尝试28随意使用。执行 Maven >install抛出此错误,并显示一条无用的错误消息,不知道发布了什么产品:
\n\n无法在项目 Invoicer 上执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile): 致命错误编译
\n
该release标志相当于为编译器插件指定相同值的源和目标。自 Java-9 以来,它支持-releaseJava 编译器的参数。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>14</source>
<target>14</target>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
注意:出于同样的原因,您可以删除声明为的冗余属性
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
Run Code Online (Sandbox Code Playgroud)
进一步:Java 9 编译器中的 --release 标志是什么? | javac 中的标志-release被引入到Compile for Older Platform Versions中。
要完成您尝试将版本值设置为 的部分的答案28。使用 Maven 版本时 -
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T00:30:29+05:30)
Run Code Online (Sandbox Code Playgroud)
错误消息非常清楚地显示了它应该显示的内容(如果您可以分享问题中的完整日志)
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project forty-bits-of-java:
Fatal error compiling: error: release version 28 not supported
Run Code Online (Sandbox Code Playgroud)
在您的 POM 中,替换两个标签source& ,target如下所示:
<properties>\n <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>\n <!--The following `source` and `target` tags are now replaced by `release` seen further down below.-->\n <!--<maven.compiler.source>14</maven.compiler.source>-->\n <!--<maven.compiler.target>14</maven.compiler.target>-->\n </properties>\nRun Code Online (Sandbox Code Playgroud)\n\xe2\x80\xa6 带有新release标签,放置在 POM 中的更下方:
<build>\n <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->\n <plugins>\n \xe2\x80\xa6\n\n <plugin>\n <artifactId>maven-compiler-plugin</artifactId>\n <version>3.8.1</version>\n <configuration>\n <!--Enable Java language preview features. Specify Java version.-->\n <!--This `release` tag replaced the pair of `source` and `target` tags seen commented-out near top of this POM.-->\n <release>14</release>\n </configuration>\n </plugin>\n\xe2\x80\xa6\nRun Code Online (Sandbox Code Playgroud)\n\xe2\x80\xa6 告诉 Java 编译器您打算部署的 Java 版本。该标记将一个release标志传递给 Java 编译器。编译器会错误地指出尝试使用添加到更高版本的 Java 中的 API 的任何代码。
查看另一个问题:Java 9编译器中的--release标志是什么?
\nNaman和ernest_k的回答都是正确且重要的。但我需要写这个答案来结合它们并展示直接的解决方案。
\n问题在于JEP 247:针对旧平台版本进行编译在 Java 9 中添加了一项新编译器标志的功能,以替换旧的、和标志-release的组合。这填补了一个困扰程序员的漏洞,他们试图在最新的编译器上工作,同时编写仅限于对早期版本的 Java 进行 API 调用的代码。-source-target-bootclasspath
例如,我可能在 Mac 上使用 Java 12 进行编写,但部署到运行 Java 8 的服务器。我希望编译器阻止我意外使用 Java 更高版本中提供的功能。否则,当这些功能在旧版JVM上不可用时,我的应用程序将在编译时成功,但在运行时失败。
\n引用 JEP 的话:
\n\n\n概括
\n
\n\n增强 javac,使其可以编译 Java 程序以在选定的旧版本平台上运行。
\n
\n\n动机
\n
\n\njavac 提供了两个命令行选项 -source 和 -target,分别可以用来选择编译器接受的 Java 语言版本和生成的类文件的版本。然而,默认情况下,javac 针对最新版本的平台 API 进行编译。因此,编译的程序可能会意外使用仅在当前版本的平台中可用的 API。无论传递给 -source 和 -target 选项的值如何,此类程序都无法在旧版本的平台上运行。这是一个长期的可用性痛点,因为用户期望通过使用这些选项,他们将获得可以在 -target 指定的平台版本上运行的类文件。
\n
在Maven驱动的项目中,我们通过在 Maven POM文件上设置标签来将这些标志传递给 Java 编译器。
\n在您的 Maven POM 文件\xe2\x80\x99s 标签层次结构中:
\n<project> \xe2\x80\xa6\n <build> \xe2\x80\xa6\n <pluginManagement> \xe2\x80\xa6\n <plugins> \xe2\x80\xa6\n <plugin> \n <artifactId>maven-compiler-plugin</artifactId>\nRun Code Online (Sandbox Code Playgroud)\n\xe2\x80\xa6nest 包含以下标记层次结构,在其中我们指定所需的 Java 部署版本。
\n<configuration> \n <release>14</release>\nRun Code Online (Sandbox Code Playgroud)\n顺便说一句,如果使用提供“预览”功能的 Java 版本,如果我们希望启用这些预览功能,我们可以进一步嵌套标签和值。
\n<compilerArgs>--enable-preview</compilerArgs>\nRun Code Online (Sandbox Code Playgroud)\n被新标签取代的旧式设置release是一对标签,source和target。这两个可以在 Maven 中设置以传递给 Java 编译器。因此,如果您添加release上面看到的标签,请检查您的 POM 是否具有这对标签。如果找到,请将其删除。
<properties>\n <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>\n <maven.compiler.source>14</maven.compiler.source> Delete if using `release` tag.\n <maven.compiler.target>14</maven.compiler.target> Delete if using `release` tag.\n</properties>\nRun Code Online (Sandbox Code Playgroud)\n以下是 Maven 3.6.3 的完整示例 POM 文件,适用于基本 Java 项目。
\n我们正在使用各种插件和依赖项的所有最新版本。此示例使用Java 15,并启用了Records等预览功能。
\n<?xml version="1.0" encoding="UTF-8"?>\n\n<project xmlns = "http://maven.apache.org/POM/4.0.0"\n xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"\n xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">\n <modelVersion>4.0.0</modelVersion>\n\n <groupId>work.basil.demo</groupId>\n <artifactId>Demo5</artifactId>\n <version>1.0-SNAPSHOT</version>\n\n <name>Demo5</name>\n <url>http://www.example.com</url>\n\n <properties>\n <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>\n <!--The following `source` and `target` tags are now replaced by `release` seen further down below.-->\n <!--<maven.compiler.source>15</maven.compiler.source>-->\n <!--<maven.compiler.target>15</maven.compiler.target>-->\n </properties>\n\n <dependencies>\n <dependency>\n <groupId>org.junit.jupiter</groupId>\n <artifactId>junit-jupiter</artifactId>\n <version>5.7.0-M1</version>\n <scope>test</scope>\n </dependency>\n </dependencies>\n\n <build>\n <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->\n <plugins>\n <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->\n <plugin>\n <artifactId>maven-clean-plugin</artifactId>\n <version>3.1.0</version>\n </plugin>\n <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->\n <plugin>\n <artifactId>maven-resources-plugin</artifactId>\n <version>3.1.0</version>\n </plugin>\n\n <plugin>\n <artifactId>maven-compiler-plugin</artifactId>\n <version>3.8.1</version>\n <configuration>\n <!--Enable Java language preview features. Specify Java version.-->\n <!--This `release` tag replaced the pair of `source` and `target` tags seen commented-out near top of this POM.-->\n <release>15</release>\n <compilerArgs>\n --enable-preview\n </compilerArgs>\n </configuration>\n </plugin>\n\n <plugin>\n <artifactId>maven-surefire-plugin</artifactId>\n <version>3.0.0-M4</version>\n </plugin>\n <plugin>\n <artifactId>maven-jar-plugin</artifactId>\n <version>3.2.0</version>\n </plugin>\n <plugin>\n <artifactId>maven-install-plugin</artifactId>\n <version>3.0.0-M1</version>\n </plugin>\n <plugin>\n <artifactId>maven-deploy-plugin</artifactId>\n <version>3.0.0-M1</version>\n </plugin>\n <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->\n <plugin>\n <artifactId>maven-site-plugin</artifactId>\n <version>3.8.2</version>\n </plugin>\n <plugin>\n <artifactId>maven-project-info-reports-plugin</artifactId>\n <version>3.1.0</version>\n </plugin>\n </plugins>\n </pluginManagement>\n </build>\n</project>\nRun Code Online (Sandbox Code Playgroud)\n您在问题中提到了JavaFX 。请注意,此讨论适用于任何和所有 Maven 驱动的 Java 项目。这包括 JavaFX 项目以及 Jakarta Servlet、控制台应用程序等。这里没有任何特定于 JavaFX 的内容。
\n| 归档时间: |
|
| 查看次数: |
4710 次 |
| 最近记录: |