如何使用maven为Javafx Application创建可执行jar及其依赖jar?

时间:2013-09-30 19:18:35

标签: maven javafx executable-jar maven-assembly-plugin

我想为我的javafx应用程序创建可执行jar。我尝试了maven-assembly plugin,但我找不到课程:com/application/Application。如何将jfxrt.jar包含在我的可执行jar中。

<plugin>
      <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <finalName>XmlEditor</finalName>
                <appendAssemblyId>false</appendAssemblyId>
                <archive>
                    <manifest>
                        <mainClass>MyApplication</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

谢谢..

1 个答案:

答案 0 :(得分:2)

这是一个'gangarajuprj'项目的简单POM。这是您的应用程序的一个很好的起点。 (此POM使用NetBeans 7.4 RC2构建)

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>co.prj</groupId>
  <artifactId>gangarajuprj</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>gangarajuprj</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <mainClass>co.prj.gangarajuprj.MainApp</mainClass>
  </properties>

  <organization>
    <!-- Used as the 'Vendor' for JNLP generation -->
    <name>Your Organisation</name>
  </organization>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>unpack-dependencies</id>
            <phase>package</phase>
            <goals>
              <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
              <excludeScope>system</excludeScope>
              <excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
              <outputDirectory>${project.build.directory}/classes</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
          <execution>
            <id>unpack-dependencies</id>

            <phase>package</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>${java.home}/../bin/javafxpackager</executable>
              <arguments>
                <argument>-createjar</argument>
                <argument>-nocss2bin</argument>
                <argument>-appclass</argument>
                <argument>${mainClass}</argument>
                <argument>-srcdir</argument>
                <argument>${project.build.directory}/classes</argument>
                <argument>-outdir</argument>
                <argument>${project.build.directory}</argument>
                <argument>-outfile</argument>
                <argument>${project.build.finalName}.jar</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>  
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <compilerArguments>
            <bootclasspath>${sun.boot.class.path}${path.separator}${java.home}/lib/jfxrt.jar</bootclasspath>
          </compilerArguments>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>