4

我刚刚提出了一个关于使用 javafxpackager 制作 JavaFX jar 的问题,你可以在这里看到。我的问题是我无法在清单中包含类路径。好吧,当我在等待答案时,我尝试maven-antrun-plugin了。它工作得很好,我可以使用依赖项运行我的应用程序,但是(总是有一个但是)只有在我的最终 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>
4

3 回答 3

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>
于 2014-09-23T12:04:23.010 回答
1

它更容易使用maven-shade-plugin。它构建了一个包含所有依赖项的大胖 jar。您可以将其与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以获取 shade 和 javaFx 插件,你就完成了。现在您可以像往常一样使用mvn package.

于 2018-04-15T09:18:13.180 回答
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>
于 2018-07-25T03:37:13.063 回答