How to create javaFX project in maven

huangapple go评论95阅读模式
英文:

How to create javaFX project in maven

问题

我正在尝试在Maven中创建JavaFX项目,我已经添加了以下依赖项:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.tensorApplication</groupId>
	<artifactId>billingWizard</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<javafx.version>17.0.1</javafx.version>
		<javafx.maven.plugin.version>0.0.8</javafx.maven.plugin.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.openjfx</groupId>
			<artifactId>javafx-controls</artifactId>
			<version>${javafx.version}</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.openjfx</groupId>
				<artifactId>javafx-maven-plugin</artifactId>
				<version>${javafx.maven.plugin.version}</version>
				<configuration>
					<mainClass>com.tensorApplications.jBills.Main.java</mainClass>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

我已经添加了主类,但是在构建和运行时遇到了很多错误,请问有人能提供更好的方法来在Eclipse中创建带有JavaFX的Maven项目吗?

英文:

i am trying to create javafx project in maven, i have added this dependencies

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;com.tensorApplication&lt;/groupId&gt;
&lt;artifactId&gt;billingWizard&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;


&lt;properties&gt;
	&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
	&lt;javafx.version&gt;17.0.1&lt;/javafx.version&gt;
	&lt;javafx.maven.plugin.version&gt;0.0.8&lt;/javafx.maven.plugin.version&gt;
&lt;/properties&gt;

&lt;dependencies&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.openjfx&lt;/groupId&gt;
		&lt;artifactId&gt;javafx-controls&lt;/artifactId&gt;
		&lt;version&gt;${javafx.version}&lt;/version&gt;
	&lt;/dependency&gt;

&lt;/dependencies&gt;

&lt;build&gt;
	&lt;plugins&gt;
		&lt;plugin&gt;
			&lt;groupId&gt;org.openjfx&lt;/groupId&gt;
			&lt;artifactId&gt;javafx-maven-plugin&lt;/artifactId&gt;
			&lt;version&gt;0.0.8&lt;/version&gt;
			&lt;configuration&gt;
				&lt;mainClass&gt;com.tensorApplications.jBills.Main.java&lt;/mainClass&gt;
			&lt;/configuration&gt;
		&lt;/plugin&gt;


	&lt;/plugins&gt;
&lt;/build&gt;

</project>
i have added main class and simply buiding and running i am getting lots of error, please someone could provide me better way to create maven project with javafx in eclips

答案1

得分: 2

我将假设你的项目按照传统的Maven项目结构进行组织,Java源代码位于src/main/java目录下,资源文件位于src/main/resources目录下。

以下是一个简单的用于JavaFX 17应用程序的Maven 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">
<modelVersion>4.0.0</modelVersion>

<groupId>com.tensorApplications</groupId>
<artifactId>billingWizard</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <javafx.version>17.0.1</javafx.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>${javafx.version}</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>${javafx.version}</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </plugin>
        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.8</version>
            <configuration>
                <mainClass>com.tensorApplications.billingWizard.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

在javafx-maven-plugin中的主类配置应该是类的完全限定名(包名.类名),不需要包含.java扩展名。

在你的Main.java文件中(应该位于com.tensorApplications.billingWizard包中),你需要继承javafx.application.Application类并重写start方法。以下是一个简单的示例:

package com.tensorApplications.billingWizard;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(new Scene(new Label("Hello World!"), 300, 250));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

编译:mvn compile

运行:mvn javafx:run

在修改pom.xml文件后,你还需要刷新Eclipse项目:在项目资源管理器中右键单击项目,选择Maven > Update Project...。

英文:

I'll assume that your project is structured in the conventional way for Maven projects, with Java sources in src/main/java, and resources in src/main/resources.

Here is a simple Maven pom.xml for a JavaFX 17 application:

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
     xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
     xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

&lt;groupId&gt;com.tensorApplications&lt;/groupId&gt;
&lt;artifactId&gt;billingWizard&lt;/artifactId&gt;
&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

&lt;properties&gt;
    &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;maven.compiler.source&gt;17&lt;/maven.compiler.source&gt;
    &lt;maven.compiler.target&gt;17&lt;/maven.compiler.target&gt;
    &lt;javafx.version&gt;17.0.1&lt;/javafx.version&gt;
&lt;/properties&gt;

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
        &lt;artifactId&gt;javafx-controls&lt;/artifactId&gt;
        &lt;version&gt;${javafx.version}&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
        &lt;artifactId&gt;javafx-fxml&lt;/artifactId&gt;
        &lt;version&gt;${javafx.version}&lt;/version&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;

&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
            &lt;version&gt;3.8.1&lt;/version&gt;
        &lt;/plugin&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
            &lt;artifactId&gt;javafx-maven-plugin&lt;/artifactId&gt;
            &lt;version&gt;0.0.8&lt;/version&gt;
            &lt;configuration&gt;
                &lt;mainClass&gt;com.tensorApplications.billingWizard.Main&lt;/mainClass&gt;
            &lt;/configuration&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;

</project>

The main class configuration in the javafx-maven-plugin should just be the fully qualified name of the class (package.classname), without the .java extension.

In your Main.java file (which should be located in the package com.tensorApplications.billingWizard), you need to extend from javafx.application.Application and override the start method. This is a simple example:

package com.tensorApplications.billingWizard;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle(&quot;Hello World!&quot;);
        primaryStage.setScene(new Scene(new Label(&quot;Hello World!&quot;), 300, 250));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

To compile: mvn compile

To run: mvn javafx:run

You should also refresh your Eclipse project after making changes to the pom.xml file : right-click on the project in the Project Explorer and select Maven > Update Project....

huangapple
  • 本文由 发表于 2023年7月27日 16:02:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定