4

I created a new JavaFX project with a pom.xml for Maven. Now I want to export it to a runnable JAR. Currently, I'm on IntelliJ IDEA 2017.1.1-2

How would one do this?

This is my current pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <groupId>org.drawpvp</groupId>
    <artifactId>drawpvp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <modelVersion>4.0.0</modelVersion>


    <name>DrawPVP</name>
    <description>Competitive drawing game.</description>
    <properties>
        <!-- Change the nd4j.backend property to nd4j-cuda-7.5-platform or nd4j-cuda-8.0-platform to use CUDA GPUs -->
        <nd4j.backend>nd4j-native-platform</nd4j.backend>

        <java.version>1.8</java.version>
        <nd4j.version>0.8.0</nd4j.version>
        <dl4j.version>0.8.0</dl4j.version>
        <datavec.version>0.8.0</datavec.version>
        <arbiter.version>0.8.0</arbiter.version>
        <rl4j.version>0.8.0</rl4j.version>

    </properties>

    <repositories>
        <repository>
            <id>snapshots-repo</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <distributionManagement>
        <snapshotRepository>
            <id>sonatype-nexus-snapshots</id>
            <name>Sonatype Nexus snapshot repository</name>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.nd4j</groupId>
                <artifactId>nd4j-native-platform</artifactId>
                <version>${nd4j.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- ND4J backend. You need one in every DL4J project. Normally define artifactId as either "nd4j-native-platform" or "nd4j-cuda-7.5-platform" -->
        <dependency>
            <groupId>org.nd4j</groupId>
            <artifactId>${nd4j.backend}</artifactId>
        </dependency>

        <!-- Core DL4J functionality -->
        <dependency>
            <groupId>org.deeplearning4j</groupId>
            <artifactId>deeplearning4j-core</artifactId>
            <version>${dl4j.version}</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.13</version>
        </dependency>

        <dependency>
            <groupId>com.jfoenix</groupId>
            <artifactId>jfoenix</artifactId>
            <version>1.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.datavec</groupId>
            <artifactId>datavec-api</artifactId>
            <version>${datavec.version}</version>
        </dependency>


</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>com.zenjava</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>8.8.3</version>
            <configuration>
                <mainClass>gui.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

How would one export a JAR for this?

Jaden Wang
  • 126
  • 1
  • 9
  • suppose you add `jar` below `drawpvp`and run maven install – Rajith Pemabandu Apr 12 '17 at 00:15
  • returns an error of `no main manifest attribute` after trying to run the .jar it created in the `target` folder. – Jaden Wang Apr 12 '17 at 00:20
  • @JadenWang please see my answer, it might help you more in case you want to use the javafx-maven-plugin – FibreFoX Apr 12 '17 at 09:44
  • @FibreFoX What are the disadvantages to my current pom.xml compared to using the plugin? – Jaden Wang Apr 12 '17 at 12:37
  • Using the `maven-assembly-plugin` does not solve things, when using the `javafx-maven-plugin` you are getting the power to create native binaries and installers in addition to "just create some executable jar", and includes some workarounds for several `javapackager`-bugs – FibreFoX Apr 12 '17 at 13:06

2 Answers2

4

Disclaimer: I'm the maintainer of that javafx-maven-plugin.

To execute the javafx-maven-plugin you have two options:

  1. calling it as part of your normal maven lifecycle mvn package
  2. calling it directly from cli/IDE goal jar of the plugin

Use this when using option 1 (maven lifecycle):

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

Your executable jar-file will be produced at the target/jfx/app/-folder.

You can lookup some examples at the github project: https://github.com/javafx-maven-plugin/javafx-maven-plugin/tree/master/src/it

FibreFoX
  • 2,858
  • 1
  • 19
  • 41
  • You can call `mvn package` when having executions, without you have to call `mvn jfx:jar` as described on the github-project. – FibreFoX Apr 12 '17 at 09:07
  • I tried that many times without much luck. Whenever it started it would crash. It also didn't store dependencies inside the jar. I'll get back to you when I get home. – Jaden Wang Apr 12 '17 at 11:40
  • @JadenWang if you need further assistance, just contact me directly via e-mail ;) always there to help – FibreFoX Apr 12 '17 at 13:07
  • @FibreFoX feel free to correct my solution. we are all learning. Thanks. – Rajith Pemabandu Apr 12 '17 at 14:59
  • I tried this solution but it seems like it's creating an external lib folder. How can I package dependencies into the .jar? – Jaden Wang Apr 12 '17 at 16:00
  • It also seems like it's causing problems with one of the libraries that I'm using whereas the original solution didn't cause that. – Jaden Wang Apr 12 '17 at 16:22
  • All compile-scoped and provided-scoped dependencies are included inside the lib-folder, system-scoped ones are skipped. In case you suspect a but with the javafx-maven-plugin, please open a bug with a reproducable project on the github-page. – FibreFoX Apr 12 '17 at 19:54
  • 1
    @RajithPemabandu javafx-executable jars contain a `Class-Path`-entry, a normal `Main-Class`-entry and a `Permissions`-entry, the JDK contains a tool called `javapackager` which gets used by the javafx-maven-plugin. Your solution is for "normal jar-executables", which do work a bit different, so adjusting your solution would not fit for javafx – FibreFoX Apr 13 '17 at 08:43
0

suppose you modify <build> tag in the pom.xml as below.

<build>
        <plugins>
            <plugin>
                <groupId>com.zenjava</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>8.8.3</version>
                <configuration>
                    <mainClass>gui.Main</mainClass>
                </configuration>
            </plugin>
             <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <configuration>
            <archive>
              <manifest>
                <mainClass>org.drawpvp.MainClass</mainClass>
              </manifest>
            </archive>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
          </configuration>
        </plugin>
        </plugins>
    </build>
Rajith Pemabandu
  • 616
  • 7
  • 14