如何在 2021 年使用 Maven 构建 JavaFX 应用程序?

时间:2021-06-23 08:00:24

标签: java maven javafx

我需要支持无法正常构建和运行的旧版 JavaFX 应用程序。该应用程序相当复杂,因此我做了简单的“Hello world”项目,而不是对其进行试验。我也无法编译它。我希望有人能指出我的错误。

我从这个例子开始,JavaFX 文档中提到了这个例子: https://github.com/openjfx/samples

<?php
        session_start();
        //header("Cache-Control: no-cache");
        //header("Pragma: no-cache");

    include 'loginClass.php';

    $login = new userLogin();

    if(isset($_POST['submit'])){
        $login->login($_POST);
    }
?>
<!DOCTYPE html>
<html lang="en" class="html">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="receipt.css?v=<?php echo time();?>">
    <script rel="javascript" src="jquery-3.5.1.min.js"></script>
    <link href="bootstrap-5.0.1-dist/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="fontawesome-free-5.15.3-web/css/all.css" rel="stylesheet"/>
    <title>Login Page</title>
    <?php
        //error_reporting(0);
    ?>

    <script type="text/javascript">
    function passFunction()
    {
        var x = document.getElementById("pass");
        if (x.type === "password")
        {
        x.type = "text";
        }
        else
        {
        x.type = "password";
        }
    } 
    </script>

    <script type="text/javascript">
        function cpassFunction()
        {
            var y = document.getElementById('cpass');
            if(y.type === "password")
            {
                y.type = "text";
            }
            else
            {
                y.type = "password";
            }
        }
    </script>

</head>
<body>
        <!--Navigation-->
        <nav class="navbar sticky-top navbar-light" style="background-color:darkgrey" role="navigation">
        <div class="container-fluid">
            <label class="navbar-brand"><img src="img/buy.png" width="20" height="20"><b>  RECEIPT GENERATING SYSTEM (RGS)</b></label>

            <div class="navbar-header navbar-right">
            <ul class="nav nav-pills">
                <li class="nav-item" role="presentation"><a class="nav-link" href="receipthome.php">Home</a></li>
                <li class="nav-item" role="presentation"><a class="nav-link" href="#">About</a></li>
                <li class="nav-item" role="presentation"><a class="nav-link" href="#">Contact</a></li>
                <li class="nav-item" role="presentation"><a class="nav-link" href="#">Admin</a></li>
            </ul>
            </div>
        </div>
        </nav><br><br>
    <div class="container h-100" style="border-radius:5px; padding:1rem">
        <div class="row h-100 justify-content-center align-items-center">
            <div class="col-10 col-md-8 col-lg-6"  style="border:1px solid grey; border-radius:5px; padding:1rem;">
            <form class="" style="height:100%; padding:1rem;" method="POST" action="userlogin.php">
            <h4 class="#">Sign in
            <label class="label" style="float:right" style="font-size:10px;">Don't have an account yet? 
            <a class="btn btn-primary btn-sm" style="float:right;" href="userregister.php"><b>Register</b></a>
            </label>
            </h4><br>
            <?php
                if (isset($_GET['msg1']) == "success") {
                echo "<div id='myView' class='alert alert-success alert-dismissible'>
                <span type='button' id='modalClose' class='close' data-dismiss='alert' style='float:right' title='Hide this box'>X</span>
                Registration successfull. Login below.
            </div>";
                }
                elseif (isset($_GET['logerr1']) == "passerr") {
                echo "<div id='myView' class='alert alert-danger alert-dismissible'>
                <span type='button' id='modalClose' class='close' data-dismiss='alert' style='float:right' title='Hide this box'>X</span>
                Oops! Invalid password.
            </div>";
                }
                elseif(isset($_GET['logerr2']) == "nouser"){
                echo "<div id='myView' class='alert alert-danger alert-dismissible'>
                <span type='button' id='modalClose' class='close' data-dismiss='alert' style='float:right' title='Hide this box'>X</span>
                User does not exist. Are you sure you have an account?
            </div>";
                }
            ?>
        
        <div class="form-group">
            <label for="username">Username</label>
            <input class="form-control" id="username" type="text" name="uname" placeholder="Username" value="<?php echo $_SESSION['uname']; ?>" required></input>
        </div><br>
        <div class="form-group">
            <label for="cpass">Password</label>
            <label class="lpass" for="chk" style="float:right">show password</label>
            <input class="checkbox" type="checkbox" id="chk" style="float:right;" onclick="cpassFunction()"></input><br>
            <input id="cpass" class="form-control" type="password" name="pass" placeholder="Password" value="<?php //echo $_SESSION['pass']; ?>" required></input>
        </div><br>
        <input class="btn btn-outline-primary" aria-pressed="true" style="float:right" type="submit" name="submit" value="Login"></input>
    </div>
    </form>
<script type="text/javascript">
    $(function () {
        $('#modalClose').on('click', function () {
            $('#myView').hide();
        })
    })
</script>
<footer class="footer">
    <div class="container">
            <a href="receipthome.php">Home</a>
            <a href="#">About</a>
            <a href="#">Contact</a>
            <p>Barna_Be &copy; 2021 All rights reserved. <b>BnB SoftTech</p>
    </div>
</footer>
</body>
</html>

好吧,可能 pom 文件有点过时了,缺少 maven.compiler.source 和 maven.compiler.target。让我们添加它们:

owner@owner-desktop:~$ mkdir example
owner@owner-desktop:~$ cd example/
owner@owner-desktop:~/example$ git clone https://github.com/openjfx/samples/
Cloning into 'samples'...
remote: Enumerating objects: 1870, done.
remote: Counting objects: 100% (215/215), done.
remote: Compressing objects: 100% (154/154), done.
remote: Total 1870 (delta 75), reused 183 (delta 56), pack-reused 1655
Receiving objects: 100% (1870/1870), 385.56 KiB | 3.38 MiB/s, done.
Resolving deltas: 100% (702/702), done.
owner@owner-desktop:~/example$ cd samples/HelloFX/Maven/hellofx/
owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ mvn package
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< org.openjfx:hellofx >-------------------------
[INFO] Building demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hellofx ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/owner/example/samples/HelloFX/Maven/hellofx/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hellofx ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/owner/example/samples/HelloFX/Maven/hellofx/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] Source option 5 is no longer supported. Use 6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.
[INFO] 2 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.042 s
[INFO] Finished at: 2021-06-23T10:10:38+03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project hellofx: Compilation failure: Compilation failure: 
[ERROR] Source option 5 is no longer supported. Use 6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

现在它确实编译...

    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>org.openjfx</groupId>
      <artifactId>hellofx</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>demo</name>
      <url>http://maven.apache.org</url>

      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <javafx.version>11</javafx.version>
        <javafx.maven.plugin.version>0.0.6</javafx.maven.plugin.version>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
      </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>HelloFX</mainClass>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>

...但似乎 javafx-maven-plugin 的 指令对创建和填充 JAR 的清单没有帮助。

owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ mvn package
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/usr/share/maven/lib/guice.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< org.openjfx:hellofx >-------------------------
[INFO] Building demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 

...CUT FOR BREVITY ...

[INFO] Building jar: /home/owner/example/samples/HelloFX/Maven/hellofx/target/hellofx-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.561 s
[INFO] Finished at: 2021-06-23T10:28:19+03:00
[INFO] ------------------------------------------------------------------------

让我们复制粘贴我之前在 stackoverflow 上找到的一些 pom 片段。我的理解是,它会指示 maven-assembly-plugin 制作自定义 jar,除了普通的 JAR 之外,还包含其中的所有依赖项。

owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ java --version
openjdk 11.0.11 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.10)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.10, mixed mode, sharing)
owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ java -jar /home/owner/example/samples/HelloFX/Maven/hellofx/target/hellofx-1.0-SNAPSHOT.jar 
no main manifest attribute, in /home/owner/example/samples/HelloFX/Maven/hellofx/target/hellofx-1.0-SNAPSHOT.jar

太好了,我想它是想告诉我它缺少 jmods。让我们下载它们:

owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ cat 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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>org.openjfx</groupId>
      <artifactId>hellofx</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>demo</name>
      <url>http://maven.apache.org</url>

      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <javafx.version>11</javafx.version>
        <javafx.maven.plugin.version>0.0.6</javafx.maven.plugin.version>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
      </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>HelloFX</mainClass>
            </configuration>
          </plugin>
          
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
          <executions>
            <execution>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <archive>
              <manifest>
                <mainClass>HelloFX</mainClass>
              </manifest>
            </archive>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
          </configuration>
      </plugin>
    </plugins>
  </build>
    </project>
owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ mvn package
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/usr/share/maven/lib/guice.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< org.openjfx:hellofx >-------------------------
[INFO] Building demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 

...CUT FOR BREVITY...

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.313 s
[INFO] Finished at: 2021-06-23T10:42:40+03:00
[INFO] ------------------------------------------------------------------------
owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ java -jar target/hellofx-1.0-SNAPSHOT-jar-with-dependencies.jar 
Error: JavaFX runtime components are missing, and are required to run this application
owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ 

现在让我们开始吧:

owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ cd ~/example/
owner@owner-desktop:~/example$ ls -la openjfx-11.0.2_linux-x64_bin-jmods\(1\).zip 
-rw-rw-r-- 1 owner owner 38699001 юни 23 10:46 'openjfx-11.0.2_linux-x64_bin-jmods(1).zip'
owner@owner-desktop:~/example$ unzip openjfx-11.0.2_linux-x64_bin-jmods\(1\).zip 
Archive:  openjfx-11.0.2_linux-x64_bin-jmods(1).zip
   creating: javafx-jmods-11.0.2/
  inflating: javafx-jmods-11.0.2/javafx.graphics.jmod  
  inflating: javafx-jmods-11.0.2/javafx.media.jmod  
  inflating: javafx-jmods-11.0.2/javafx.controls.jmod  
  inflating: javafx-jmods-11.0.2/javafx.fxml.jmod  
  inflating: javafx-jmods-11.0.2/javafx.base.jmod  
  inflating: javafx-jmods-11.0.2/javafx.web.jmod  
  inflating: javafx-jmods-11.0.2/javafx.swing.jmod  

我现在已经没有想法了。我做错了什么?

1 个答案:

答案 0 :(得分:2)

您试图同时做几件事,这绝不是一个好主意,而且您没有密切关注文档。您是否尝试按照文档中的说明使用 mvn clean javafx:run 运行您的示例?

将遗留应用程序移植到最新的 JDK/JavaFX 时,您应该做的第一件事是编译它并使用 JavaFX Maven 插件运行它。如果它是为 JDK8 编写的旧应用程序,并且它包含 JavaFX,那么这可能已经是一些挑战,因为有一些 API 更改和其他事情。

一旦您解决了这些问题并让您的应用运行起来,您就可以开始考虑打包了。现在的 JavaFX 应用程序不应该只是被扔到一些大的 jar 文件中,因为这只会导致很多其他问题。相反,您应该考虑使用 jpackage 来创建合适的应用程序安装程序。你可能想看看这里:https://github.com/dlemmermann/JPackageScriptFX(注意:我对此有偏见。)