JavaFX jar with dependencies bundled

半世苍凉 提交于 2020-01-03 17:10:17

问题


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>

回答1:


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>



回答2:


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.




回答3:


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>


来源:https://stackoverflow.com/questions/13361804/javafx-jar-with-dependencies-bundled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!