iamkenos
iamkenos

Reputation: 1566

JavaFX Maven Plugin: com.zenjava - How to copy external resources

I'm relatively new to Maven + JavaFX and I'm trying to produce a JavaFX executable jar file with the com.zenjava maven plugin.

I was following this guide for reference:
https://www.youtube.com/watch?v=wbjW8rYlook

I have the following folder structure for my project:
enter image description here

Now I'm trying to run the config jfx:jar during maven build and was able to produce a jar file but the resources I need are not copied under the target/jfx/app folder.

enter image description here

Basically, I want to copy the entire src/main/resources folder to target/jfx/app/resources. How do I accomplish this?

Some information:
The src/main/resources/ folder will contain different kinds of files that I will need during runtime, (excel files, pdf, htmls...) and not just property files.

Thanks in advance.

========================================================================
UPDATE:
Yuri-M-Dias' answer helped.

Without changing any other setting, I managed to do this by just updating my pom file with:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <targetPath>../jfx/app/resources</targetPath>
        </resource>
    </resources>



    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.zenjava</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>8.7.0</version>
            <configuration>
                <mainClass>me.iamkenos.bayonetta.MainApp</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

This is definitely working but I'm not sure whether this is the best way, given I had to cheat it a bit by using "../" in <targetPath>../jfx/app/resources</targetPath> will wait for other possible answers for the meantime.

Upvotes: 4

Views: 1896

Answers (2)

Oleks
Oleks

Reputation: 1051

When you run the command jfx:jar you will get executable jar file with resources folder inside because you added resources folder to the build path.

If you just copy the entire src/main/resources folder to target/jfx/app/resources folder you will have copies of the same resource files (inside and outside of generated jar file) and if you need to allow a user to edit some of resource files (e.g. *.properties files) your code I guess will rely on the inside files so user changes have no any effect in this case.

That is why you need to split project resources into:

  • Internal (the part of generated jar file e.g. raster graphics and read-only configs)
  • External (located outside the jar file e.g. config files that could be edited by user)

I would suggest to create 3 folders

  • \src\main\java (source code) - part of a build path
  • \src\main\resources (internal) - part of the build path
  • \src\main\config (external)- excluded from the build path

use maven to copy external config folder and build executable jfx jar

<build>
    <resources>
        <resource>
            <directory>src/main/config</directory>
            <targetPath>../jfx/app/config</targetPath>
        </resource>
    </resources>
</build>

<plugins>
    <plugin>
        <groupId>com.zenjava</groupId>
        <artifactId>javafx-maven-plugin</artifactId>
        <version>8.8.3</version>
        <configuration>
            <vendor>${vendor}</vendor>
            <mainClass>${mainClass}</mainClass>
            <allPermissions>true</allPermissions>
        </configuration>
    </plugin>
</plugins>

The finishing touch is configuring symlink path to allow eclipse work in debug mode properly with external resources. You can use Link Shell Extension to do it.

for Windows it might look like

mklink /J C:\...\target\classes C:\...\target\jfx\app\config
LinkToFolder                    OriginalFolder

LinkToFolder is eclipse project folder with compiled classes

Upvotes: 2

Yuri-M-Dias
Yuri-M-Dias

Reputation: 639

You can control Maven's output folders to specific folders using the resources keyword. For example, on my project:

    <resources>
        <resource>
            <directory>src/main/java/view</directory>
            <targetPath>view</targetPath>
        </resource>
    </resources>

I am forcing the contents of the java/view folder to output to the target/classes/view in this case, since it's where my JavaFX images and fxmls are. You can probably do the same for the jfx/app/resources folder.

As for copying the folder, you can take a look at the official maven recommendation.

Upvotes: 4

Related Questions