【问题标题】:How to Export JAR (JavaFX and Maven) using IntelliJ IDEA如何使用 IntelliJ IDEA 导出 JAR(JavaFX 和 Maven)
【发布时间】:2017-04-12 00:09:31
【问题描述】:

我为 Maven 创建了一个带有 pom.xml 的新 JavaFX 项目。现在我想将它导出到一个可运行的 JAR。目前,我在 IntelliJ IDEA 2017.1.1-2

如何做到这一点?

这是我当前的 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>

如何为此导出 JAR?

【问题讨论】:

  • 假设你在&lt;artifactId&gt;drawpvp&lt;/artifactId&gt;下面添加&lt;packaging&gt;jar&lt;/packaging&gt;并运行maven install
  • 在尝试运行它在 target 文件夹中创建的 .jar 后返回错误 no main manifest attribute
  • @JadenWang 请看我的回答,如果您想使用 javafx-maven-plugin,它可能会对您有更多帮助
  • @FibreFoX 与使用插件相比,我当前的 pom.xml 有什么缺点?
  • 使用maven-assembly-plugin 并不能解决问题,当使用javafx-maven-plugin 时,除了“只创建一些可执行jar”之外,您还可以创建本机二进制文件和安装程序,并包括一些解决方法对于几个javapackager-bugs

标签: java maven intellij-idea javafx deeplearning4j


【解决方案1】:

免责声明:我是那个 javafx-maven-plugin 的维护者。

要执行 javafx-maven-plugin,您有两个选择:

  1. 将其称为正常 maven 生命周期的一部分 mvn package
  2. 直接从插件的 cli/IDE 目标 jar 调用它

在使用选项 1(maven 生命周期)时使用它:

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

您的可执行 jar 文件将在 target/jfx/app/-文件夹中生成。

您可以在 github 项目中查找一些示例:https://github.com/javafx-maven-plugin/javafx-maven-plugin/tree/master/src/it

【讨论】:

  • 你可以在执行时调用mvn package,而不必像github-project中描述的那样调用mvn jfx:jar
  • 我尝试了很多次,但运气不佳。什么时候开始它就会崩溃。它也没有在 jar 中存储依赖项。我到家后会回复你的。
  • @JadenWang 如果您需要进一步的帮助,请直接通过电子邮件与我联系;)随时为您提供帮助
  • @FibreFoX 随时纠正我的解决方案。我们都在学习。谢谢。
  • @RajithPemabandu javafx-executable jars 包含一个Class-Path-entry、一个普通的Main-Class-entry 和一个Permissions-entry,JDK 包含一个名为javapackager 的工具javafx-maven-插件。您的解决方案适用于“普通 jar 可执行文件”,它的工作方式有点不同,因此调整您的解决方案不适合 javafx
【解决方案2】:

假设你修改 pom.xml 中的&lt;build&gt; 标签如下。

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

【讨论】:

  • 这行得通,但现在它似乎没有拾取依赖项,因为它抛出了一堆依赖项错误。
  • 你能把jar解压出来看看是否包含jar。
  • 不,它不包括我正在使用的库。
  • 参考这个post,这可能有解决方案。
  • @JadenWang 也修改了解决方案。
猜你喜欢
  • 1970-01-01
  • 2016-06-13
  • 2015-04-21
  • 1970-01-01
  • 1970-01-01
  • 2015-08-09
  • 1970-01-01
  • 2018-10-27
  • 2016-02-07
相关资源
最近更新 更多