Finished work on the JPackage call, placed it inside separate profile Image.

This commit is contained in:
Konrad Neitzel 2023-03-21 16:29:01 +01:00
parent 08f95c3732
commit ced77b8e6b
5 changed files with 169 additions and 148 deletions

View File

@ -2,6 +2,14 @@
Example Maven Project for a JavaFX Application. Example Maven Project for a JavaFX Application.
Maven modules are configured so that even Images are build (including JPackage app-image). This projects includes multiple plugins:
- Static code analysis with PMD and Spotbugs
- Check of dependency updates during build
- Build of an App-Image using JPackage
Project includes PMD and spotbugs! The application is no longer a modular application so there are no problems with dependencies that are not providing a
module-info.
** Build the Image **
To build the image, the profile Image must be used:
```./mvnw -DImage install```

100
pom.xml
View File

@ -17,18 +17,17 @@
<link.name>${project.artifactId}</link.name> <link.name>${project.artifactId}</link.name>
<launcher>${project.artifactId}</launcher> <launcher>${project.artifactId}</launcher>
<appName>${project.artifactId}</appName> <appName>${project.artifactId}</appName>
<main.class>de.kneitzel.JavaFXApp</main.class> <main.class>de.kneitzel.Main</main.class>
<main.module>FXAppModule</main.module>
<java.version>17</java.version> <java.version>17</java.version>
<required.maven.version>3.6.3</required.maven.version> <required.maven.version>3.6.3</required.maven.version>
<jar.file>javafxapp</jar.file> <jar.filename>${project.artifactId}-${project.version}</jar.filename>
<!-- Dependency versions --> <!-- Dependency versions -->
<junit.version>5.9.1</junit.version>
<javafx.version>19</javafx.version> <javafx.version>19</javafx.version>
<log4j.version>2.19.0</log4j.version> <jetbrains.annotations.version>24.0.1</jetbrains.annotations.version>
<lombok.version>1.18.24</lombok.version> <junit.version>5.9.2</junit.version>
<jetbrains.annotations.version>23.0.0</jetbrains.annotations.version> <lombok.version>1.18.26</lombok.version>
<mockito.version>5.2.0</mockito.version>
<!-- Plugin dependencies --> <!-- Plugin dependencies -->
<maven.clean.plugin>3.2.0</maven.clean.plugin> <maven.clean.plugin>3.2.0</maven.clean.plugin>
@ -44,7 +43,7 @@
<moditect.maven.plugin>1.0.0.RC2</moditect.maven.plugin> <moditect.maven.plugin>1.0.0.RC2</moditect.maven.plugin>
<jpackage.maven.plugin>0.1.3</jpackage.maven.plugin> <jpackage.maven.plugin>0.1.3</jpackage.maven.plugin>
<maven.pmd.version>3.16.0</maven.pmd.version> <maven.pmd.version>3.16.0</maven.pmd.version>
<pmd.version>6.50.0</pmd.version> <pmd.version>6.55.0</pmd.version>
<codehaus.version.plugin>2.11.0</codehaus.version.plugin> <codehaus.version.plugin>2.11.0</codehaus.version.plugin>
<spotbugs.maven.plugin>4.7.2.0</spotbugs.maven.plugin> <spotbugs.maven.plugin>4.7.2.0</spotbugs.maven.plugin>
<spotbugs.version>4.7.3</spotbugs.version> <spotbugs.version>4.7.3</spotbugs.version>
@ -58,6 +57,7 @@
</properties> </properties>
<dependencies> <dependencies>
<!-- JavaFX dependencies -->
<dependency> <dependency>
<groupId>org.openjfx</groupId> <groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId> <artifactId>javafx-controls</artifactId>
@ -79,6 +79,7 @@
<version>${javafx.version}</version> <version>${javafx.version}</version>
</dependency> </dependency>
<!-- Lombok -->
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
@ -86,6 +87,34 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- JUnit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- Mockito -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<!-- Dependency used for @NotNull / @Nullable --> <!-- Dependency used for @NotNull / @Nullable -->
<dependency> <dependency>
<groupId>org.jetbrains</groupId> <groupId>org.jetbrains</groupId>
@ -94,28 +123,10 @@
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
<finalName>${jar.file}</finalName> <finalName>${jar.filename}</finalName>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
@ -290,6 +301,26 @@
</executions> </executions>
</plugin> </plugin>
</plugins>
</build>
<profiles>
<!-- Profile that adds JLink and JPackage runs.
Add -PImage or -DImage to use this profile.
-->
<profile>
<id>Image</id>
<activation>
<property>
<name>Image</name>
</property>
</activation>
<build>
<finalName>${jar.filename}</finalName>
<plugins>
<!-- Copy dependencies -->
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId> <artifactId>maven-dependency-plugin</artifactId>
@ -304,16 +335,7 @@
</goals> </goals>
<configuration> <configuration>
<outputDirectory>${project.build.directory}/modules</outputDirectory> <outputDirectory>${project.build.directory}/modules</outputDirectory>
<excludeGroupIds>org.openjfx</excludeGroupIds>
<includeScope>runtime</includeScope> <includeScope>runtime</includeScope>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<type>${project.packaging}</type>
</artifactItem>
</artifactItems>
<overWriteReleases>false</overWriteReleases> <overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots> <overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer> <overWriteIfNewer>true</overWriteIfNewer>
@ -364,10 +386,11 @@
<addmodule>javafx.controls</addmodule> <addmodule>javafx.controls</addmodule>
<addmodule>javafx.graphics</addmodule> <addmodule>javafx.graphics</addmodule>
<addmodule>javafx.fxml</addmodule> <addmodule>javafx.fxml</addmodule>
<addmodule>javafx.web</addmodule>
</addmodules> </addmodules>
<mainclass>${main.class}</mainclass> <mainclass>${main.class}</mainclass>
<input>${project.build.directory}/modules</input> <input>${project.build.directory}/modules</input>
<mainjar>${jar.file}.jar</mainjar> <mainjar>${jar.filename}.jar</mainjar>
</configuration> </configuration>
<executions> <executions>
<execution> <execution>
@ -380,4 +403,7 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</profile>
</profiles>
</project> </project>

View File

@ -1,24 +1,17 @@
package de.kneitzel; package de.kneitzel;
import javafx.application.Application; import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Parent; import javafx.scene.Parent;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage; import javafx.stage.Stage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException; import java.io.IOException;
public class JavaFXApp extends Application { public class JavaFXApp extends Application {
static final Logger logger = LogManager.getLogger(JavaFXApp.class);
@Override @Override
public void start(Stage primaryStage) throws IOException { public void start(Stage primaryStage) {
try { try {
primaryStage.setTitle("Hello World!"); primaryStage.setTitle("Hello World!");
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("MainWindow.fxml")); FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("MainWindow.fxml"));
@ -26,16 +19,12 @@ public class JavaFXApp extends Application {
primaryStage.setScene(new Scene(root)); primaryStage.setScene(new Scene(root));
primaryStage.show(); primaryStage.show();
} catch (Exception ex) { } catch (Exception ex) {
logger.error("Exception!", ex); System.out.println("Exception: " + ex.getMessage());
ex.printStackTrace();
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
logger.warn("Starting ...");
try {
launch(args); launch(args);
} catch (Exception ex) {
logger.error("Exception!", ex);
}
} }
} }

View File

@ -0,0 +1,15 @@
package de.kneitzel;
/**
* Another Main class as workaround when the JavaFX Application ist started without
* taking care os Classloader Requirements of JavaFX. (Important when starting from inside NetBeans!)
*/
public class Main {
/**
* Additional main methode to start Application.
* @param args Commandline Arguments.
*/
public static void main(String[] args) {
JavaFXApp.main(args);
}
}

View File

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="MyFile" fileName="all.log" immediateFlush="false" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" />
<AppenderRef ref="MyFile"/>
</Root>
</Loggers>
</Configuration>