如何通过Maven下载适配各操作系统的JavaFX依赖库?
默认JavaFX的Maven依赖会根据你当前构建环境的操作系统自动拉取对应平台的原生包,所以你在Windows环境下只拉到Windows版本是正常现象。
要获取全平台依赖可以用两种方式:
方式1:手动为每个依赖添加对应平台的classifier属性
你需要给每个用到的JavaFX依赖分别添加3个平台的配置,示例如下:
<!-- Windows 版本 --> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>15.0.1</version> <classifier>win</classifier> </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-graphics</artifactId> <version>15.0.1</version> <classifier>win</classifier> </dependency> <!-- Linux 版本 --> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>15.0.1</version> <classifier>linux</classifier> </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-graphics</artifactId> <version>15.0.1</version> <classifier>linux</classifier> </dependency> <!-- Mac 版本 --> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>15.0.1</version> <classifier>mac</classifier> </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-graphics</artifactId> <version>15.0.1</version> <classifier>mac</classifier> </dependency>
配置后重新执行mvn clean install,Maven就会把三个平台的依赖全部拉取到本地。
方式2:使用javafx-maven-plugin统一配置(更适合多平台打包场景)
如果你需要分平台打独立包,建议配合插件和Maven Profile使用,不需要重复写多份依赖配置:
- 首先保留你原本的无classifier的依赖配置即可
- 在pom.xml中添加插件配置,同时为不同平台定义Profile:
<profiles> <!-- Windows 打包Profile --> <profile> <id>win</id> <properties> <javafx.platform>win</javafx.platform> </properties> </profile> <!-- Linux 打包Profile --> <profile> <id>linux</id> <properties> <javafx.platform>linux</javafx.platform> </properties> </profile> <!-- Mac 打包Profile --> <profile> <id>mac</id> <properties> <javafx.platform>mac</javafx.platform> </properties> </profile> </profiles> <build> <plugins> <plugin> <groupId>org.openjfx</groupId> <artifactId>javafx-maven-plugin</artifactId> <version>0.0.8</version> <configuration> <mainClass>你的主类全路径</mainClass> </configuration> </plugin> </plugins> </build>
需要打对应平台包时,执行对应Profile的构建命令即可,比如打Windows包就执行mvn clean javafx:jlink -P win,插件会自动拉取对应平台的依赖并构建包含对应JavaFX库的独立分发包。
内容的提问来源于stack exchange,提问作者MarekChr
相关产品推荐
相关产品推荐

