繁体   English   中英

maven项目中的javafx应用程序

[英]javafx application in maven project

我有一个结构如下的项目:

主要项目
-libary-1
-library-2
-i-需要,JavaFX的应用,在这里

所以,我需要在我的maven项目中使用JavaFX 2应用程序,它应该使用library-1和library-2及其依赖项。 我找不到JavaFX 2项目的任何maven原型,我可以找到有关如何通过Maven 3构建JavaFX应用程序的任何充分信息。我不需要任何部署到Web,它只会是桌面应用程序。

那么,任何人都可以帮助解决这个问题吗?

UPD:

java.lang.NoClassDefFoundError: javafx/application/Application
...
Caused by: java.lang.ClassNotFoundException: javafx.application.Application

当我尝试运行由pgras方式构建的应用程序时发生异常。

所以我猜你已经使用了Java 7,我确切地知道你需要什么,为了编译你只需要依赖javafx,所以在你的pom中为javafx app:

<dependencies>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>javafx</artifactId>
        <version>2.0</version>
        <systemPath>${javafx.rt.jar}</systemPath>
        <scope>system</scope>
    </dependency>
</dependencies>

在你的maven settings.xml中添加(并调整你的系统路径):

<profile>
  <id>javafx</id>
  <activation>
    <activeByDefault>true</activeByDefault>
  </activation>
  <properties>
    <javafx.rt.jar>C:\Program Files (x86)\Oracle\JavaFX 2.0 SDK\rt\lib\jfxrt.jar</javafx.rt.jar>
    <ant.javafx.jar>C:\Program Files (x86)\Oracle\JavaFX 2.0 SDK\tools\ant-javafx.jar</ant.javafx.jar>
  </properties>
</profile>

它应该足以编译和制作jar文件...如果没有告诉我,我会发布更多信息......

非常感谢,这个链接很有帮助。 那么,现在我的pom看起来像这样:

<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>info.kosmonaffft</groupId>
    <artifactId>javafx-maven-example</artifactId>
    <version>0.1</version>
    <packaging>jar</packaging>
    <name>javafx-maven-example</name>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <excludeArtifactIds>junit,hamcrest-core</excludeArtifactIds>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</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}/${project.build.finalName}">
                                    <fileset dir="${project.build.directory}/classes"/>
                                    <application name="${project.name}" mainClass="info.kosmonaffft.jfxexample.App"/>
                                    <resources>
                                    </resources>
                                </jfxjar>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>com.oracle.javafx</groupId>
                        <artifactId>ant-javafx</artifactId>
                        <version>2.0</version>
                        <scope>system</scope>
                        <systemPath>C:\Program Files\Oracle\JavaFX 2.1 SDK\tools\ant-javafx.jar</systemPath>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javafx</groupId>
            <artifactId>javafx-rt</artifactId>
            <version>2.1</version>
            <scope>system</scope>
            <systemPath>C:\Program Files\Oracle\JavaFX 2.1 SDK\rt\lib\jfxrt.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

因此,现在可以从命令行“java -jar myapp.jar”运行应用程序,但我还没有测试它将如何运行依赖项。

我添加了另一个答案,因为它很长,它显示了我如何生成一个签名的jar和一个JNLP webstart文件。

所以首先我创建一个证书,以便能够签署jar(你会被要求输入一些信息,我使用“superpass”作为密码,看看它在pom中使用的位置):

cd src/main/java
mkdir jnlp
cd jnlp
keytool -genkey -alias signFiles -keystore keystore.jks

然后我使用这个pom.xml(它有一个父pom但它不包含任何与javafx相关的东西):

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
     xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
    <artifactId>testfx</artifactId>
    <groupId>ch.pgras</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<groupId>ch.pgras</groupId>
<artifactId>hellofx</artifactId>
<version>1.0-SNAPSHOT</version>
<name>hellofx</name>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>javafx</artifactId>
        <version>2.0</version>
        <systemPath>${javafx.rt.jar}</systemPath>
        <scope>system</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <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>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <configuration>
                        <target>
                            <taskdef name="jfxdeploy" classname="com.sun.javafx.tools.ant.DeployFXTask"
                                     classpathref="maven.plugin.classpath"/>

                            <taskdef name="jfxsignjar" classname="com.sun.javafx.tools.ant.FXSignJarTask"
                                     classpathref="maven.plugin.classpath"/>

                            <jfxdeploy width="800" height="600" outdir="${project.build.directory}/deploy"
                                       outfile="${project.build.finalName}">
                                <info title="${project.name}"/>
                                <application name="${project.name}" mainclass="webmap.WebMap"/>
                                <resources>
                                    <fileset dir="${project.build.directory}" includes="*.jar"/>
                                    <fileset dir="${project.build.directory}/dependency"
                                             includes="*.jar"/>
                                </resources>
                                <platform javafx="2.0">
                                    <jvmarg value="-Xms64m"/>
                                    <jvmarg value="-Xmx256m"/>
                                </platform>
                            </jfxdeploy>
                            <jfxsignjar destdir="${project.build.directory}/deploy"
                                        keystore="${project.basedir}/src/main/java/jnlp/keystore.jks"
                                        storepass="superpass" alias="signFiles" keypass="superpass">
                                <fileset dir="${project.build.directory}/deploy" includes="*.jar"/>
                            </jfxsignjar>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.oracle.javafx</groupId>
                    <artifactId>ant-javafx</artifactId>
                    <version>2.0</version>
                    <systemPath>${ant.javafx.jar}</systemPath>
                    <scope>system</scope>
                </dependency>
            </dependencies>
        </plugin>

    </plugins>
</build>

最后我运行mvn install,结果将在target / deploy中...

祝好运 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM