Build, develop and package JavaFX projects using Maven

Maven is a project management tool that can build Java projects, manage dependencies, and more. Using Maven, you can easily build and package JavaFX projects. The prerequisite for this chapter is that your system has installed and configured jdk8 and Maven, and you are familiar with the use of Maven.

Create a JavaFX project

1. Use the Maven command line to create a project.

Open the command line or terminal, and execute the following command:

mvn archetype:generate -DgroupId=com.learn -DartifactId=javafx-learn-02 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

If this command is executed for the first time, a lot of things will be downloaded first. As long as BUILD SUCCESS appears and the javafx-learn-02 project is generated under its directory, it means success.

What is created here is an ordinary maven project, first import it into Eclipse (you can also use other development tools for development, such as IDEA, VsCode, sublime, etc., one of the benefits of Maven is that it can cross tools, even with ordinary text editing can also be used).

Open Eclipse, select the top menu File->Import..., and open the Eclipse project import interface.

Select the Existing Maven Projects project in the import interface (if there are too many contents, you can search Maven filter in the input box).

Select the Maven project you just created.

Click Finish. If the project appears in the project list on the left, the Maven project import is successful (because I have too many projects, so here I use the Working Set mode in Eclipse, which is convenient for group management of projects. If the Working Set mode is not used If you don’t need to worry about the learn group, as long as there is javafx-learn-02 project in the project on the left and the one in my screenshot has jdk display, maven dependencies display, and src/main/java, it means that the import is successful. Specifically, the use of Eclipse self-study).

2. Create a Maven project directly using eclipse

The above is to use the command line to create a Maven project. It is more convenient for developers who use other IDEs or text development. However, if you use an IDE that supports Maven such as Eclipse or IDEA for development, you can directly use the IDE to create a Maven project.

The prerequisite for creating a Maven project with Eclipse or importing a Maven project in the first way is that Eclipse has installed the Maven plug-in. However, the latest Eclipse basically has a built-in Maven plug-in, and it is recommended to use the latest Eclipse for development. Not introduced here, the specific installation is relatively simple.

Open Eclipse, select the top menu File->New->Other....

Select Maven Project in the opened interface, and enter Maven filter in the search box.

In the next interface, check: create a simple project, the Maven plug-in of eclipse provides many Maven prototypes (which can be understood as templates) to create Maven projects, here is simply to build an empty Maven project, so there is no need to select Prototype, better English should be able to understand, the meaning of the tick means to create a classic project, skip the prototype selection. The following ones can be defaulted. I choose to add it to the learn working set here. If you don’t choose the working set, you can ignore it, and it doesn’t affect it here.

Continue to click Next, to the next step interface, fill in the details as follows.

  • Group ID:com.learn
  • Artifact ID: javafx-learn-maven-02
  • version: 0.0.1-SHAPSHOT
  • packaging: jar

The specific parameters are the basic knowledge of Maven, so I won’t introduce them here, and click Finish after filling them out.

After completion, we created a new Maven project in the project on the left.

The ones created by the command line are almost the same as the ones created by Eclipse. Later, I will use the javafx-learn-maven-02 project. The one created by the command line is just an example, so I will delete it here first.

Add Maven's javafx plugin.

javafx-maven-plugin is a Maven plugin for managing JavaFX applications in Maven. For example, you only need one command mvn jfx:run to run a JavaFX application, and you only need one command mvn package jfx:native to package a JavaFX application, which is very convenient.

Open the pom.xml of the project, as follows:

At this point pom.xml is a simple xml file that only contains the basic information of the project.

<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.learn</groupId>
	<artifactId>javafx-learn-maven-02</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</project>

The code to add the javafx-maven-plugin plugin is as follows:

<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.learn</groupId>
	<artifactId>javafx-learn-maven-02</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	
	<build>
		<plugins>
			<plugin>
				<groupId>com.zenjava</groupId>
				<artifactId>javafx-maven-plugin</artifactId>
				<version>8.8.3</version>
				<configuration>
					<!-- 作者 -->
					<vendor>xiangtao</vendor>
					<!-- main方法的类 -->
					<mainClass>com.learn.LearnMain02</mainClass>
					<!-- 运行文件名 -->
					<appName>${project.build.finalName}</appName>
					<!-- 图标的位置,默认位置 src/main/deploy -->
					<!--<deployDir>${basedir}/src/main/resources/images/ico/Flap.ico</deployDir>-->
					<!-- 菜单 -->
					<needMenu>true</needMenu>
					<!-- 桌面图标 -->
					<needShortcut>true</needShortcut>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

When packaging, the resources under resource and the jar package imported using systemPath need to be packaged, so continue to add the resources configuration code:

<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.learn</groupId>
	<artifactId>javafx-learn-maven-02</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	
	<build>
		<plugins>
			<plugin>
				<groupId>com.zenjava</groupId>
				<artifactId>javafx-maven-plugin</artifactId>
				<version>8.8.3</version>
				<configuration>
					<!-- 作者 -->
					<vendor>xiangtao</vendor>
					<!-- main方法的类 -->
					<mainClass>com.learn.LearnMain02</mainClass>
					<!-- 运行文件名 -->
					<appName>${project.build.finalName}</appName>
					<!-- 图标的位置,默认位置 src/main/deploy -->
					<!--<deployDir>${basedir}/src/main/resources/images/ico/Flap.ico</deployDir>-->
					<!-- 菜单 -->
					<needMenu>true</needMenu>
					<!-- 桌面图标 -->
					<needShortcut>true</needShortcut>
				</configuration>
			</plugin>
		</plugins>
		
		<resources>
			<resource>
				<!--把src/main/java目录下的properties、xm文件打包打进程序中-->
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
				</includes>
				<filtering>false</filtering>
			</resource>
		 
			<resource>
				<!--把src/main/resources目录下的properties、xm文件打包打进程序中-->
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
					<include>**/*.fxml</include>
					<include>**/*.setting</include>
				</includes>
				<filtering>false</filtering>
			</resource>
		 
			<resource>
				<!--把lib/目录下第三方jar包打进程序中,如systemPath目录下的jar-->
				<directory>lib/</directory>
				<includes>
					<include>**/*.jar</include>
				</includes>
				<filtering>false</filtering>
			</resource>
		</resources>
		
	</build>
</project>

Write code

Create a new package com.learn, add the LearnMain02 class to it, and then add the main method. Here the main class needs to be consistent with the configuration of the mainClass attribute in the javafx-maven-plugin plug-in.

Let the modified class inherit the "javafx.application.Application" class, implement its start method, and then add the main method, and start the application in the main method. The code is as follows:

package com.learn;

import javafx.application.Application;
import javafx.stage.Stage;

public class LearnMain02  extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
    	
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

In JavaFX, an application is composed of Stage, Scene, interface components, container layout, etc. Stage can be simply understood as a window representing the real application, in which Scene is included, and various interface layouts and UI controls are included under Scene and other components

. For details, please read relevant chapters such as Stage and Scene .

We create an interface based on the above ideas:

package com.learn;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;

public class LearnMain02 extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
    	//创建一个BorderPane布局
			BorderPane rootPane = new BorderPane();

			//创建一个Label标签,将其添加到布局的中心
			Label label = new Label("Hello World!!!");
			rootPane.setCenter(label);

			// 创建一个800*640大小的Scene,关联其布局
			Scene scene = new Scene(rootPane, 800, 640);

			// 设置Stage的Scene、标题,然后显示
			primaryStage.setScene(scene);
			primaryStage.setTitle("JavaFX Maven 学习示例");
			primaryStage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

The result of the operation is as follows:

Use the plugin command to run and package

1. Liunx(Deepin)

Open the command line or terminal and enter the project root directory.

Execute the command mvn jfx:run to run the javafx project:

Execute the command mvn jfx:native or mvn package jfx:native to package the javafx project:

Then a packaged deb file and a binary file that can be run directly will be generated in the target/jfx directory.

deb file can be used directly after installation:

Binaries in the javafx-learn-maven-02-0.0.1-SNAPSHOT directory can be run directly:

2. Window

CMD opens the project and executes mvn jfx:run to run the project:

Execute mvn jfx:native or mvn package jfx:native to package the project:

If InnoSetup is installed in the computer, it will also be packaged as a program that clicks Next, and the next step is similar to the installation program. I do not have this software installed in my computer, so I skipped the generation of the installer, and only packaged one that can be run directly. green software.

Double-click the exe to run:

3. Mac

Open the project in the terminal and execute mvn jfx:run to run the project:

Execute mvn jfx:native or mvn package jfx:native to package the project:

After the packaging is successful, a packaging file will be generated in the target/jfx/native directory. Here I click javafx...app to run the effect as follows:

Guess you like

Origin blog.csdn.net/u012970287/article/details/127559543