【问题标题】:JavaFX build with shade, Location is required. Where is it looking?带有阴影的 JavaFX 构建,位置是必需的。它在哪里寻找?
【发布时间】:2019-04-07 01:35:24
【问题描述】:

它仍然没有找到 FXML 位置。我已经搞砸了 输出目录和 目录和 进入 FXMLLoader 的 URL 无济于事:

Parent root = FXMLLoader.load(getClass().getResource("fxml/ui.fxml"));

此应用程序在哪里寻找资源?

目录:

在:

PROJECT/src/main/java/mypackage/Main.java

项目/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

项目/目标/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>

【问题讨论】:

  • 您为什么要添加和覆盖maven-resource-plugin 默认行为?
  • @JoséPereda 我从 apache maven 资源页面获取。看来我必须以这种方式指定我的资源目录。如果我删除覆盖,它仍然不知道位置

标签: maven javafx maven-shade-plugin


【解决方案1】:

对于初学者来说,第一个问题是您的资源:

父根 = FXMLLoader.load(getClass().getResource("fxml/ui.fxml"));

如果你使用getResource,你必须使用绝对路径:

Parent root = FXMLLoader.load(getClass().getResource("/fxml/ui.fxml"));

StackOverflow 上有很多关于这个问题的问题,这个answer 是一个非常有效的参考。

ma​​ven-shade-plugin

假设您的 com.potatospy.NewMain 类是不扩展 Applicationlauncher class,并且您的来源(在 src/main/java/ 下)是:

  • com/potatospy/NewMain.java
  • com/potatospy/Main.java
  • com/potatospy/UIController.java

您的资源(在src/main/resources 下)是:

  • fxml/ui.fxml

如果你想做一个阴影/胖罐子,并且你不要修改默认的 maven-resources-plugin,这将只使用 maven-compile-pluginmaven-shade-plugin 插件从你的pom:

mvn clean package

然后你就可以运行你的应用了:

java -jar target/AppName-1.0-SNAPSHOT.jar

注意默认资源插件被应用,你的文件被添加到target/classes

  • target/classes/com/potatospy/NewMain.class
  • target/classes/com/potatospy/Main.class
  • target/classes/com/potatospy/UIController.class
  • 目标/类/fxml/ui.fxml

ma​​ven-resource-plugin

但是如果你像在 pom 中那样添加资源插件,当你运行它时,你会注意到你的文件被添加到:

  • target/classes/com/potatospy/NewMain.class
  • target/classes/com/potatospy/Main.class
  • target/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 添加到输出目录(因此资源包含在其中)。

【讨论】:

  • 非常感谢您提供的信息。我认为在通过其他线程多次解决了这个 Location null 问题之后,还有一件事......我真的不明白为什么修复工作有效。无论如何,我会研究你的答案以尽可能多地学习,以便我可以开始帮助其他人!
猜你喜欢
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
  • 2019-05-02
  • 2019-07-22
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
  • 2023-02-09
相关资源
最近更新 更多