带有阴影的 JavaFX 构建,位置是必需的。它在看哪里?
JavaFX build with shade, Location is required. Where is it looking?
它仍然没有找到 FXML 位置。我搞砸了
输出目录和
目录和
URL 进入 FXMLLoader 无济于事:
Parent root = FXMLLoader.load(getClass().getResource("fxml/ui.fxml"));
这个应用程序在哪里寻找资源?
目录:
在:
PROJECT/src/main/java/mypackage/Main.java
PROJECT/src/main/resources/fxml/ui.fxml
输出:
PROJECT/target/AppName-1.0-SNAPSHOT.jar
PROJECT/target/AppName-1.0-SNAPSHOT-shaded.jar
PROJECT/target/classes/myPackage/Main.class
PROJECT/target/fxml/ui.fxml
我的 POM:
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>11.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.fxml</include>
</includes>
</resource>
</resources>
</configuration>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release> <!--or <release>10</release>-->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.potatospy.NewMain</mainClass>
</transformer>
</transformers>
<artifactSet>
<excludes>
<exclude>classworlds:classworlds</exclude>
<exclude>junit:junit</exclude>
<exclude>jmock:*</exclude>
<exclude>*:xml-apis</exclude>
<exclude>org.apache.maven:lib:tests</exclude>
<exclude>log4j:log4j:jar:</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
首先,第一个问题与您的资源有关:
Parent root = FXMLLoader.load(getClass().getResource("fxml/ui.fxml"));
如果你使用getResource
你必须使用绝对路径:
Parent root = FXMLLoader.load(getClass().getResource("/fxml/ui.fxml"));
Whosebug 上有很多关于这个问题的问题,这个 answer 是一个非常有效的参考。
maven-shade-plugin
假设您的 com.potatospy.NewMain
class 是一个 不扩展 Application
,并且您的来源(在 src/main/java/
下)是:
- com/potatospy/NewMain.java
- com/potatospy/Main.java
- com/potatospy/UIController.java
您的资源(在 src/main/resources
下)是:
- fxml/ui.fxml
如果你想做一个shade/fat jar,并且你不修改默认的maven-resources-plugin,这将只使用[=来自您的 pom 的 19=] 和 maven-shade-plugin
插件:
mvn clean package
然后你可以运行你的应用:
java -jar target/AppName-1.0-SNAPSHOT.jar
请注意,已应用默认资源插件,您的文件已添加到 target/classes
:
- 目标/classes/com/potatospy/NewMain.class
- 目标/classes/com/potatospy/Main.class
- 目标/classes/com/potatospy/UIController.class
- 目标/classes/fxml/ui.fxml
maven-resource-plugin
但是如果您像在 pom 中所做的那样添加 资源插件,当您运行 它时,您会注意到您的文件被添加到:
- 目标/classes/com/potatospy/NewMain.class
- 目标/classes/com/potatospy/Main.class
- 目标/classes/com/potatospy/UIController.class
- 目标/fxml/ui.fxml
打包完成后,fxml 文件甚至没有添加到 jar 中!
如果您需要包含资源插件,您将需要这样的东西:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.fxml</include>
</includes>
</resource>
</resources>
</configuration>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
</execution>
</executions>
</plugin>
请注意,我已将 classes
添加到输出目录(因此资源包含在其中)。
它仍然没有找到 FXML 位置。我搞砸了 输出目录和 目录和 URL 进入 FXMLLoader 无济于事:
Parent root = FXMLLoader.load(getClass().getResource("fxml/ui.fxml"));
这个应用程序在哪里寻找资源?
目录:
在:
PROJECT/src/main/java/mypackage/Main.java
PROJECT/src/main/resources/fxml/ui.fxml
输出:
PROJECT/target/AppName-1.0-SNAPSHOT.jar
PROJECT/target/AppName-1.0-SNAPSHOT-shaded.jar
PROJECT/target/classes/myPackage/Main.class
PROJECT/target/fxml/ui.fxml
我的 POM:
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>11.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.fxml</include>
</includes>
</resource>
</resources>
</configuration>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release> <!--or <release>10</release>-->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.potatospy.NewMain</mainClass>
</transformer>
</transformers>
<artifactSet>
<excludes>
<exclude>classworlds:classworlds</exclude>
<exclude>junit:junit</exclude>
<exclude>jmock:*</exclude>
<exclude>*:xml-apis</exclude>
<exclude>org.apache.maven:lib:tests</exclude>
<exclude>log4j:log4j:jar:</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
首先,第一个问题与您的资源有关:
Parent root = FXMLLoader.load(getClass().getResource("fxml/ui.fxml"));
如果你使用getResource
你必须使用绝对路径:
Parent root = FXMLLoader.load(getClass().getResource("/fxml/ui.fxml"));
Whosebug 上有很多关于这个问题的问题,这个 answer 是一个非常有效的参考。
maven-shade-plugin
假设您的 com.potatospy.NewMain
class 是一个 Application
,并且您的来源(在 src/main/java/
下)是:
- com/potatospy/NewMain.java
- com/potatospy/Main.java
- com/potatospy/UIController.java
您的资源(在 src/main/resources
下)是:
- fxml/ui.fxml
如果你想做一个shade/fat jar,并且你不修改默认的maven-resources-plugin,这将只使用[=来自您的 pom 的 19=] 和 maven-shade-plugin
插件:
mvn clean package
然后你可以运行你的应用:
java -jar target/AppName-1.0-SNAPSHOT.jar
请注意,已应用默认资源插件,您的文件已添加到 target/classes
:
- 目标/classes/com/potatospy/NewMain.class
- 目标/classes/com/potatospy/Main.class
- 目标/classes/com/potatospy/UIController.class
- 目标/classes/fxml/ui.fxml
maven-resource-plugin
但是如果您像在 pom 中所做的那样添加 资源插件,当您运行 它时,您会注意到您的文件被添加到:
- 目标/classes/com/potatospy/NewMain.class
- 目标/classes/com/potatospy/Main.class
- 目标/classes/com/potatospy/UIController.class
- 目标/fxml/ui.fxml
打包完成后,fxml 文件甚至没有添加到 jar 中!
如果您需要包含资源插件,您将需要这样的东西:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.fxml</include>
</includes>
</resource>
</resources>
</configuration>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
</execution>
</executions>
</plugin>
请注意,我已将 classes
添加到输出目录(因此资源包含在其中)。