带有 JavaFX 的 Maven 项目(在 `lib` 中有 jar 文件) [英] Maven project with JavaFX (with jar file in `lib`)

查看:26
本文介绍了带有 JavaFX 的 Maven 项目(在 `lib` 中有 jar 文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个将使用 JavaFX 的 Maven 项目.由于我听说 JavaFX 并非随所有版本的 Java 一起提供,因此我下载了 jfxrt.jar 文件并将其放在我项目的 lib 目录中.

I'm setting up a maven project, that is going to use JavaFX. Since I've heard JavaFX does not come with all versions of Java, I downloaded and put the jfxrt.jar file in a lib directory in my project.

1) 如何指定不应该下载依赖项(即 JavaFX),但该依赖项位于 lib 中?

1) How do I specify that the dependency (ie., JavaFX) should not be downloaded, but which is located in lib?

2) 这是否意味着该项目可以在任何带有 JDK 的机器上构建(不需要 JDK 1.7 - 更新 9+)?

2) Does this then mean the project can be built on any machine with JDK (and not necessary JDK 1.7 - update 9+) ?

推荐答案

2019 最新 JavaFX 版本更新

从 Java 11 开始,JavaFX 已与 JDK 分离,因此 JavaFX 库不再与 JDK 安装自动捆绑在一起(例如它们在 Oracle JDK 8 发行版中).相反,JavaFX 系统现在被设计为一组独立于 JDK 的独立库,可以通过诸如 Maven 之类的工具从存储库下载以供您的应用程序使用.

As of Java 11, JavaFX has been decoupled from the JDK, so JavaFX libraries are no longer automatically bundled together with a JDK install (as they were for instance in the Oracle JDK 8 distribution). Instead, the JavaFX system is now designed as a set of separate libraries independent from the JDK, which can be downloaded from a repository by a tool such as Maven for use by your application.

openjfx.io 有一个关于在 Maven 中使用 JavaFX 11+ 的非常简短的教程:

openjfx.io have a very short tutorial on using JavaFX 11+ with Maven:

本教程推荐使用 OpenJFX JavaFX Maven Plugin 并提供以下示例 maven pom.xml 项目文件用于hello, world"样式应用程序.

This tutorial recommends use of the OpenJFX JavaFX Maven Plugin and provides the following sample maven pom.xml project file for a "hello, world" style application.

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.openjfx</groupId>
  <artifactId>hellofx</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>demo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-controls</artifactId>
      <version>12.0.1</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-maven-plugin</artifactId>
        <version>0.0.2</version>
        <configuration>
          <mainClass>HelloFX</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

请注意,用于 Java 11+ 的 org.openjfx JavaFX maven 插件 是不同的来自用于 Java 8 和 9 的 com.zenjava JavaFX maven 插件,所以请使用适合您的 Java 版本的版本.

Note that the org.openjfx JavaFX maven plugin used for Java 11+ is different from the com.zenjava JavaFX maven plugin used for Java 8 and 9, so use the appropriate one for your Java version.

建议的方法

用于使用 Maven 构建 JavaFX 应用程序:

For building JavaFX apps with Maven:

  1. 在 Java 8 和 9 中使用 com.zenjava maven JavaFX 插件和/或
  2. 使用 Java 8 或 9 并且不要为 JavaFX 指定任何类型的 Maven 依赖项或
  3. 在 Java 11+ 中使用 org.openjfx maven JavaFX 插件.

与 Java 7 不同,Java 8 无需将 JavaFX 设置为 maven 依赖项,因为 JavaFX 位于标准 Java 运行时类路径上(类似于今天的 Swing).

Unlike Java 7, with Java 8 it is unnecessary to set JavaFX as a maven dependency, because JavaFX is on the standard Java runtime classpath (similar to the way Swing is today).

意见

我认为您将 jfxrt.jar 文件放在项目 lib 目录中的方法存在缺陷.

I think the approach in your question of placing the jfxrt.jar file in a project lib directory is flawed.

它有缺陷的原因是:

  1. JavaFX 还包含本机库,因此您也需要包含它们(在 jfxrt.jar 能够找到它们的位置).
  2. 给定版本的 JavaFX 将仅被认证为针对其附带的 JDK 版本运行,因此您放置在项目lib 目录中的 JavaFX 运行时可能无法针对更高版本的 JDK版本,例如 JDK 8.
  3. JDK 8 的未来版本将在 JDK 的默认类路径中包含 JavaFX,因此您可能会与您放置在 lib 目录中的版本发生冲突.
  1. JavaFX also includes native libraries, so you would need to include them as well (in a location where jfxrt.jar would be able to find them).
  2. A given version of JavaFX will only be certified to operate against the version of the JDK it is shipped with, so the JavaFX runtime you place in your project's lib directory may not work against a later JDK version, such as JDK 8.
  3. Future versions of the JDK such as JDK 8 will include JavaFX on the default classpath of the JDK, so you may get conflicts with the version you place in your lib directory.

总而言之,JavaFX 应该被视为 Java 运行时系统的一部分,而不是项目库.

In summary, JavaFX should be treated as part of the Java runtime system, not as a project library.

JavaFX 系统依赖示例

如果您必须使用 Java 7 并且不想使用 JavaFX maven 插件,那么您可以采用不太推荐的方法:

If you must use Java 7 and you don't want to use the JavaFX maven plugin, then you can take the less recommended approach:

  • Reference the JavaFX library from the jdk (not your project lib directory) as a maven system dependency.

此示例仅适用于 Java 7,Java 8 不需要(并且不会按原样运行).Java 8 将 jfxrt.jar 放在 ${java.home}/lib/ext/jfxrt.jar 中,它位于默认的 Java 运行时类路径上,使 Java 8 的系统依赖性无关紧要.

This sample is provided for Java 7 only and is not required (and will not work as is) for Java 8. Java 8, places the jfxrt.jar in ${java.home}/lib/ext/jfxrt.jar, which is on the default Java runtime classpath, making a system dependency for Java 8 irrelevant.

<dependency>
  <groupId>javafx</groupId>
  <artifactId>jfxrt</artifactId>
  <version>${java.version}</version>
  <scope>system</scope>
  <systemPath>${java.home}/lib/jfxrt.jar</systemPath>
</dependency>

如果您使用这样的系统依赖项打包您的应用程序,那么您需要为您的用户编写一些执行指令,以便他们在 Java 7 上运行您的应用程序时可以将 jfxrt.jar 添加到运行时类路径中.这是一个很好的理由不使用这种方法.

If you package your application using a system dependency like this, then you need will need to write some execution instructions for your users so that they can add jfxrt.jar to the runtime classpath when running your application on Java 7. This is a very good reason not to use this approach.

如果你选择使用maven系统依赖方式,你可能还希望使用maven antrun 插件 嵌入对 JavaFX ant 任务的调用本示例.您可能还想使用 maven 执行器插件 来确保您的应用程序是至少针对 Java 的最低要求版本构建,其中包含您的应用程序运行所需的 JavaFX 版本.

If you choose to use the maven system dependency approach, you may also wish to use the maven antrun plugin to embed calls to the JavaFX ant tasks as in this sample. You may also want to use the maven enforcer plugin to ensure that your application is built against at least the minimum required version of Java containing a JavaFX version required by your application to work.

这篇关于带有 JavaFX 的 Maven 项目(在 `lib` 中有 jar 文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆