gpt4 book ai didi

java - 如何使用 Maven 构建用于生产的 Javafx + Spring

转载 作者:行者123 更新时间:2023-12-05 07:13:56 27 4
gpt4 key购买 nike

我使用 ma​​ven 构建工具 创建了 Javafx (13) 应用程序与 spring boot (2.2.4) 和 jpa 集成。现在我的示例应用程序运行良好。但我不清楚这个应用程序如何迁移到生产环境。我的问题是

  1. 如何打包应用
  2. 我可以使用应用程序 jar 文件创建安装文件吗
  3. 我如何在不同的桌面环境(Windows、Linux)中安装应用程序

以下是使用的技术和工具

  • Javafx 13
  • Spring 启动 2.2.4
  • maven 3.6.1
  • Intelij 理念
  • Ubuntu 18.04

pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.emp</groupId>
<artifactId>spring-boot-javafx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-javafx</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>11</java.version>
<maven.test.skip>true</maven.test.skip>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>13</version>
</dependency>

</dependencies>

<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>

<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.emp.springbootjavafx.SpringBootJavafxApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.emp.springbootjavafx.SpringBootJavafxApplication</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>

<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<mainClass>com.emp.springbootjavafx.SpringBootJavafxApplication</mainClass>
</configuration>
</plugin>
</plugins>

<resources>
<!-- copy fxml and css resources -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.fxml</include>
<include>**/*.css</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/test/java</directory>
<includes>
<include>**/*.fxml</include>
<include>**/*.css</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
</project>

Module-info.java 类

module spring.boot.javafx {

requires javafx.fxml;
requires javafx.controls;
requires javafx.graphics;
requires spring.boot;
requires spring.boot.autoconfigure;
requires spring.context;
requires spring.boot.starter.jdbc;
requires java.persistence;
requires java.sql;
requires java.xml.bind;
requires net.bytebuddy;
requires spring.core;
requires spring.data.jpa;
requires com.fasterxml.classmate;

exports com.emp.springbootjavafx.controllers;
opens com.emp.springbootjavafx;

}

application.properties 文件

spring.main.web-application-type=none

#=========================================================================
#DATA SOURCE PROPERTIES
#=========================================================================
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test_db_fx
spring.datasource.username=root
spring.datasource.password=root

#=========================================================================
#JPA PROPERTIES
#=========================================================================
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.properties.hibernate.format_sql=true

主类

package com.emp.springbootjavafx;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
public class SpringBootJavafxApplication extends Application {

private ConfigurableApplicationContext springContext;
private Parent rootNode;
private FXMLLoader fxmlLoader;

public static void main(String[] args) {
launch(SpringBootJavafxApplication.class, args);
}

@Override
public void start(Stage primaryStage) throws Exception {

fxmlLoader.setLocation(getClass().getResource("/fxml/sample.fxml"));
rootNode = fxmlLoader.load();

primaryStage.setTitle("Hello World");
Scene scene = new Scene(rootNode, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}

@Override
public void init() throws Exception {
springContext = SpringApplication.run(SpringBootJavafxApplication.class);
fxmlLoader = new FXMLLoader();
fxmlLoader.setControllerFactory(springContext::getBean);
}

@Override
public void stop() {
springContext.stop();
}
}

在使用上述配置构建项目后,我尝试使用 java -jar app.jar 命令 运行 jar 文件。但我收到以下错误消息

enter image description here

最佳答案

1:咨询这个:IntelliJ can't recognize JavaFX 11 with OpenJDK 11

2 + 3:Jar 文件应该能够在任何安装了 Java 的计算机上启动,但如果您想将其包装在 .exe 中,请使用 Inno Setup 和/或 Launch4j 等工具。您可以通过 Maven 使用它们。

关于java - 如何使用 Maven 构建用于生产的 Javafx + Spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60051814/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com