捆绑了依赖项的JavaFX jar

时间:2012-11-13 13:44:14

标签: java maven ant javafx-2

我刚刚提出了一个关于使用javafxpackager制作JavaFX jar的问题,你可以看到它here。我的问题是我无法在清单中包含类路径。好吧,当我在等待答案时,我尝试了maven-antrun-plugin。它运行良好,我可以运行我的应用程序与依赖项,但是(总有一个但是)只有依赖项OUTSIDE我的最后一个jar。就像那样:

FinalJar.jar
lib
  |_{all dependencies here}

我的清单文件通过JavaFX-Class-Path属性指向依赖项。如果我把依赖项放在jar中,就像我想要的那样,它找不到我的依赖项。有什么帮助吗?

编辑:以下是将依赖项添加到jar中的步骤,它位于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>

3 个答案:

答案 0 :(得分:3)

最后,我的POM的“构建”部分变成了这个(你可以看到它也有焊接部分),自从我用它以来已经很久了,所以我甚至不知道它是否可以再

<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>

答案 1 :(得分:0)

使用maven-shade-plugin要容易得多。它构建了一个包含所有依赖项的大胖罐。您可以将此项与javafx-maven-plugin结合使用。我也尝试了不同的方法并且玩了很长时间,这个解决方案是唯一真正起作用的解决方案。此外,它很容易设置。

以下是您必须添加到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>

mainClass内更改您的包名称以获取阴影和javaFx插件,您就完成了。现在,您可以使用mvn package来构建应用程序。

答案 2 :(得分:0)

您想获得一些本机应用程序吗?像exe或dmg。 这是我的解决方案。  首先将项目制作为Maven项目,然后添加一些插件以达到目标; 我将共享我的pom.xml,在您的pom.xml中添加这两个插件,然后在您的终端中运行“ mvn jfx:native”。

      <!--  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>