4

I just made a question about using javafxpackager to make JavaFX jars, you can see it here. My problem was that I couldn't include the classpath in the manifest. Well, while I was waiting for the answer, I tried maven-antrun-plugin instead. It worked nice, and I could run my application with dependencies, BUT (there is always a but) only with the dependencies OUTSIDE my final jar. So it is like that:

FinalJar.jar
lib
  |_{all dependencies here}

My manifest file is pointing to the dependencies via the JavaFX-Class-Path property. If I put the dependencies inside the jar, like I want, it doesn't find my dependencies. Any help?

EDIT: Here's the step to add the dependencies to the jar, its inside the pom.xml:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <configuration>
                        <target>
                            <taskdef name="jfxjar" classname="com.sun.javafx.tools.ant.FXJar"
                                classpathref="maven.plugin.classpath" />
                            <jfxjar
                                destfile="${project.build.directory}/dist/${project.build.finalName}">
                                <fileset dir="${project.build.directory}/classes" />

                                <!-- Adds the dependencies to jar -->
                                <fileset dir="${project.build.directory}/lib/" includes="*.jar" />
                                <application name="${project.name}" mainClass="com.google.code.mzplay.principal.PrincipalFX" />

                                <resources>
                                    <!-- Adds the dependencies to classpath -->
                                    <fileset dir="${project.build.directory}/lib/" includes="*.jar" />
                                </resources>
                            </jfxjar>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.oracle</groupId>
                    <artifactId>ant-javafx</artifactId>
                    <version>${javafx.version}</version>
                    <systemPath>${java.home}/../lib/ant-javafx.jar</systemPath>
                    <scope>system</scope>
                </dependency>
                <dependency>
                    <groupId>com.oracle</groupId>
                    <artifactId>javafx</artifactId>
                    <version>${javafx.version}</version>
                    <systemPath>${java.home}/lib/jfxrt.jar</systemPath>
                    <scope>system</scope>
                </dependency>
            </dependencies>
        </plugin>
Community
  • 1
  • 1
Montolide
  • 773
  • 3
  • 12
  • 27
  • How did you put the dependencies inside the jar? – jewelsea Nov 13 '12 at 17:24
  • There, edit it to show the ant part of the `pom.xml`. Of course, I first copy the jars to the lib folder with `maven-dependency-plugin` – Montolide Nov 13 '12 at 18:10
  • 2
    Have a look at [this thread on packaging JavaFX apps with Maven](https://forums.oracle.com/forums/thread.jspa?threadID=2442108) and see if it helps. – jewelsea Nov 13 '12 at 18:29
  • No i tried this approach but my jar still giving class not found exception – Mubasher Sep 19 '14 at 20:35
  • 1
    @Mubasher posted the final POM, only the build part, see if it helps! – Montolide Sep 23 '14 at 12:05
  • when i use copy-dependencies Goal , it did not work for me so i use this approach and it works. http://stackoverflow.com/questions/25973919/where-substitute-custome-resource-icon-wix-and-wxs-file-for-javafx-2-when-dep Now My Jar is executeable. i also tried with unpack-dependencies as your way and it works. but whole jar as it is bundling in final jar is not working, Now i have new issue that where to place WIX installer conf files. so antrun plugin can pick it. can you help me. sea more detail on above mention link. please my mind is exploding to figuring out the problem. – Mubasher Sep 23 '14 at 16:17

3 Answers3

3

In the end, my "build" part of the POM became this (you can see that it has a weld part also), its been a long time since I used it, so I don't even know if its ok anymore

<build>
    <finalName>JarName</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <includeScope>runtime</includeScope>
                        <outputDirectory>${project.build.directory}/dist/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <configuration>
                        <target>
                            <taskdef name="jfxjar" classname="com.sun.javafx.tools.ant.FXJar"
                                classpathref="maven.plugin.classpath" />
                            <jfxjar
                                destfile="${project.build.directory}/dist/${project.build.finalName}">
                                <fileset dir="${project.build.directory}/classes" />
                                <application name="${project.name}" mainClass="com.google.code.mzplay.principal.WeldJavaFXLauncher" />
                                <resources>
                                    <fileset dir="${project.build.directory}/dist/" includes="lib/*.jar" />
                                </resources>
                            </jfxjar>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.oracle</groupId>
                    <artifactId>ant-javafx</artifactId>
                    <version>${javafx.version}</version>
                    <systemPath>${java.home}/../lib/ant-javafx.jar</systemPath>
                    <scope>system</scope>
                </dependency>
                <dependency>
                    <groupId>com.oracle</groupId>
                    <artifactId>javafx</artifactId>
                    <version>${javafx.version}</version>
                    <systemPath>${java.home}/lib/jfxrt.jar</systemPath>
                    <scope>system</scope>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>2.0</version>
        </plugin>
    </plugins>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-dependency-plugin</artifactId>
                                    <versionRange>[2.0,)</versionRange>
                                    <goals>
                                        <goal>copy-dependencies</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <execute />
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
Montolide
  • 773
  • 3
  • 12
  • 27
  • in maven dependencies plugin when i use copy-dependencies it did not work but when i used 'unpack-dependencies' then it works and i unpack it in ${project.build.directory}/classes – Mubasher Sep 23 '14 at 16:18
1

It is much easier to use maven-shade-plugin. It builds one big fat jar with all dependencies inside. You can use this in combination with javafx-maven-plugin. I also tried different approaches and played around a long time and this solution was the only one which really works. Additionally, it was easy to set up.

Here is what you have to add to your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>your.package.name.Main</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>com.zenjava</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>8.8.3</version>
            <configuration>
                <mainClass>your.package.name.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

Change your package name inside mainClass for shade and also javaFx plugin and you are done. Now you can build your Application as always with mvn package.

Mark Kowalski
  • 264
  • 2
  • 9
0

Do you want to get some native applictation ? like exe or dmg。 It's my solution. make project as a maven project first,and then add some plugins to hit the target; i will share my pom.xml add those two plugins in you pom.xml, and run "mvn jfx:native" in you terminal later.

      <!--  this plugin will copy dependencies into target application-->

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.10</version>
          <executions>
              <execution>
                  <id>copy-dependencies</id>
                  <phase>package</phase>
                  <configuration>
                      <overWriteReleases>false</overWriteReleases>
                      <overWriteSnapshots>false</overWriteSnapshots>
                      <overWriteIfNewer>true</overWriteIfNewer>
                  </configuration>
                  <goals>
                      <goal>copy-dependencies</goal>
                  </goals>
              </execution>
          </executions>
      </plugin>

      <plugin>
          <groupId>com.zenjava</groupId>
          <artifactId>javafx-maven-plugin</artifactId>
          <version>8.8.3</version>
          <configuration>
              <!-- it's javafx Application's Main Class -->
              <mainClass>org.john.Main</mainClass>
              <!-- what's platform , write what kind of target name -->
              <bundler>exe</bundler>
              <!-- tell plugin where the target save-->
              <jfxAppOutputDir>${project.build.directory}/app</jfxAppOutputDir>
              <nativeOutputDir>${project.build.directory}/native</nativeOutputDir>
              <appName>Ticket</appName>
              <vendor>www.kvcoogo.com</vendor>
          </configuration>
      </plugin>