java maven项目中的javafx应用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10531750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javafx application in maven project
提问by Anton
I have a project with structure like this:
我有一个结构如下的项目:
main-project
-libary-1
-library-2
-i-need-javafx-app-here
main-project
-libary-1
-library-2
-i-need-javafx-app-here
So, i need a JavaFX 2 application in my maven project, and it should use library-1 and library-2 with its dependencies. I can't find any maven archetypes for JavaFX 2 projects and i cand find any adequate information about how i can build JavaFX app by Maven 3. I don't need any deployment to web, it will only desktop app.
因此,我的 Maven 项目中需要一个 JavaFX 2 应用程序,它应该使用 library-1 和 library-2 及其依赖项。我找不到 JavaFX 2 项目的任何 Maven 原型,我可以找到有关如何通过 Maven 3 构建 JavaFX 应用程序的任何足够信息。我不需要任何部署到网络,它只会部署桌面应用程序。
So, can anybody help with this problem?
那么,有人可以帮助解决这个问题吗?
UPD:
更新:
java.lang.NoClassDefFoundError: javafx/application/Application
...
Caused by: java.lang.ClassNotFoundException: javafx.application.Application
exception is occured when i try to run application, which is builded by pgrasway.
java.lang.NoClassDefFoundError: javafx/application/Application
...
Caused by: java.lang.ClassNotFoundException: javafx.application.Application
当我尝试运行由pgras方式构建的应用程序时发生异常。
回答by pgras
So I guess you already use Java 7, I d'ont know exactly what you need, for compiling you will need only the dependency to javafx, so in your pom for the javafx app:
所以我猜你已经在使用 Java 7,我不知道你到底需要什么,为了编译你只需要对 javafx 的依赖,所以在你的 javafx 应用程序的 pom 中:
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>javafx</artifactId>
<version>2.0</version>
<systemPath>${javafx.rt.jar}</systemPath>
<scope>system</scope>
</dependency>
</dependencies>
And in your maven settings.xml add (and adapt path to your system):
并在您的 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>
It should be enough to compile and make jar file... If not tell me and I'll post more info...
编译和制作jar文件应该足够了......如果没有告诉我,我会发布更多信息......
回答by Anton
Thank you a lot, this link is helpful. So, now my pom looks like this:
非常感谢,这个链接很有帮助。所以,现在我的 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>
So, it is possible to run application from command line "java -jar myapp.jar" now, but i have not tested how it will runs with dependencies.
因此,现在可以从命令行“java -jar myapp.jar”运行应用程序,但我还没有测试它如何在依赖项下运行。
回答by pgras
I add another answer because it's a long one, and it shows how I do to also generate a signed jar and a JNLP webstart file.
我添加了另一个答案,因为它很长,它展示了我如何生成一个签名的 jar 和一个 JNLP webstart 文件。
So first I create a certificate to be able to sign the jar (you'll be asked to enter some info, I used "superpass" for the passwords, look where it is used in pom) :
因此,首先我创建一个证书以便能够对 jar 进行签名(您将被要求输入一些信息,我使用“superpass”作为密码,看看它在 pom 中的使用位置):
cd src/main/java
mkdir jnlp
cd jnlp
keytool -genkey -alias signFiles -keystore keystore.jks
And then I use this pom.xml (which has a parent pom but it doesn't contain anything related to javafx):
然后我使用这个 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>
finally I run mvn install, and the result will be in target/deploy...
最后我运行 mvn install,结果将在目标/部署中......
good luck :)
祝你好运 :)