waynard
waynard

Reputation: 83

Compiling JavaFX jar with all dependencies using Maven

New to Java and Maven.

I am trying to configure my application so that I can generate a jar via cmd line that has all of my dependencies packaged in it.

From what I can tell I am setting up my Pom correctly to use this plugin: https://github.com/javafx-maven-plugin/javafx-maven-plugin

Here are the dependencies I have in my Pom:

    <dependencies>
        <dependency>
            <groupId>se.michaelthelin.spotify</groupId>
            <artifactId>spotify-web-api-java</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11</version>
        </dependency>
    </dependencies>

In my <plugins> block I have the following Maven plugins:

Here is how javafx-maven-plugin is configured:

                <groupId>com.zenjava</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>8.8.3</version>
                <configuration>
                    <mainClass>recommendify.Main</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>create-jfxjar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build-jar</goal>
                        </goals>
                    </execution>
                </executions>

Another SO topic dealing with this issue said to configure the maven-assembly-plugin like so (it is my understanding that this is what packages all the dependencies):

        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <id>create-executable</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <outputDirectory>${test.pack.dir}</outputDirectory>
            <appendAssemblyId>false</appendAssemblyId>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
                <manifest>
                    <mainClass>recommendify.Main</mainClass>
                </manifest>
            </archive>
        </configuration>

With this Maven config I run the following to compile the .jar: mvn clean compile jfx:build-jar

However the .jar that is compiled to my target directory is completely empty besides a META-INF folder which houses MANIFEST.MF.

What exactly am I doing wrong here? Maven gives me the following log message [INFO] Adding 'deploy' directory to Mojo classpath: /Users/adonis/school/recommendify/src/main/deploy. What is Mojo? Am I supposed to be using a deploy package to house my .java files? I ran into an issue earlier where my fxml views package was not being compiled to the classes output directory when using Intellij's run application feature, and after a bit of research I inferred that my fxml should be stored under resources/fxml instead of my aptly named views. This leads me to believe I need a "deploy" directory/package.

Any help would be very much appreciated :)

Upvotes: 1

Views: 7774

Answers (1)

Gnas
Gnas

Reputation: 718

Try using Maven Jar plugin. This is what I have in all of my JavaFX projects:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>Your.Main.Class.Here</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

Upvotes: 2

Related Questions