带有阴影的JavaFX构建,位置是必需的。在哪里看?

问题描述 投票:0回答:1

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

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

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

目录:

在:

Project / Saras / I / Java / Mapkega / I.Java

项目/ src目录/主/资源/ FXML / ui.fxml

日期:

PROJECT /目标/ AppName的-1.0-SNAPSHOT.jar

PROJECT /目标/ AppName的-1.0-快照shaded.jar

项目/目标/班/ 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 javafx maven-shade-plugin
1个回答
2
投票

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

父root = FXMLLoader.load(getClass()。getResource(“fxml / ui.fxml”));

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

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

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

行家遮阳帘插件

假设你的com.potatospy.NewMain类是launcher class,不扩展Application,你的来源(在src/main/java/下)是:

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

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

  • fxml / ui.fxml

如果你想做一个阴影/脂肪罐,而你不修改默认的maven-resources-plugin,这将只使用你的pom中的maven-compile-pluginmaven-shade-plugin插件:

mvn clean package

那么你可以运行你的应用程序:

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

请注意,将应用默认资源插件,并将您的文件添加到target/classes

  • 目标/班/ COM / potatospy / NewMain.class
  • 目标/班/ COM / potatospy / Main.class
  • 目标/班/ COM / potatospy / UIController.class
  • 目标/类/ FXML / ui.fxml

Maven的资源插件

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

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

© www.soinside.com 2019 - 2024. All rights reserved.