簡體   English   中英

在 JDK11 之后發布 javafx 應用程序是否需要 javafx-maven-plugin?

[英]Is javafx-maven-plugin required for shiping a javafx application after JDK11?

I'm trying to figure out what would be the minimum pom.xml to use for creating a hello-world javafx application that can be provided to the client as a jar + ./lib and run with java -jar .

我用過:

pom.xml

<?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>com.example.javafx</groupId>
    <artifactId>javafx-jdk14-example</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.example.javafx.app.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>14</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics </artifactId>
            <version>14</version>
            <classifier>linux</classifier>
        </dependency>        
    </dependencies>    
</project>

主.java

package com.example.javafx.app;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
 
public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);  // Override default
        grid.setHgap(10);
        grid.setVgap(12);        
        
        Button btn = new Button();

        
        btn.setText("Clickme");
        btn.setOnAction((t) -> System.out.println("Clicked!"));
        
        grid.add(btn, 0, 0);
        primaryStage.setScene(new Scene(grid, 640, 480));
        primaryStage.show();
    }
}

請注意,我沒有在我的應用程序上使用module-info.java

但是當我運行它時:

mvn clean package
cd target
/jdk14/bin/java -jar javafx-jdk14-example-1.0.0.jar

我得到:

錯誤:缺少 JavaFX 運行時組件,需要運行此應用程序

如果我對 JDK10 使用相同的(還有compiler.source/target=10和注釋掉org.openjfx deps),那么上面的工作正常。

是否有任何其他方式(在 JDK11+ 上)構建然后能夠從目標目錄內部運行應用程序而無需使用javafx-maven-plugin

在@Slaw 的幫助下,我找到了運行 javafx 應用程序的方法(在沒有javafx-maven-plugin情況下創建):

cd target
java -p lib/ --add-modules javafx.controls -jar javafx-jdk14-example-1.0.0.jar

我需要明確添加javafx.controls模塊。

底線:

構建和發布 javafx 應用程序不需要javafx-maven-plugin

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM