Skip to content

Commit

Permalink
demo: Add junixsocket-demo-jpackagejlink
Browse files Browse the repository at this point in the history
Demonstrates how to use jlink/jpackage with junixsocket.

#153
  • Loading branch information
kohlschuetter committed Mar 5, 2024
1 parent 0d8ca6a commit 34d7441
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 0 deletions.
53 changes: 53 additions & 0 deletions junixsocket-demo-jpackagejlink/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# junixsocket-demo-jpackagejlink

Example code to demonstrate how to use junixsocket with jlink and jpackage.

## Definitions

### jlink

jlink builds Java runtime images that are tailored towards a specific set of Java classes / modules.

jlink was introduced in Java 9.

### jpackage

jpackage builds native installers for applications, utilizing optimized runtimes very much like jlink.

jpackage was introduced in Java 14.

## The demo code

This demo simply runs a very simple selftest built into junixsocket-common.

It depends on `junixsocket-core`, which is a POM-only dependency that in turn references
`junixsocket-common` (the common junixsocket API) and `junixsocket-native-common` (the
native library images for common operating systems).

## Build instructions

The main purpose of this Maven module is to demonstrate how to configure a project's `pom.xml` file
such that jlink/jpackage-compatible artifacts can be created.

By default, this demo does not create executable jlink/jpackage artifacts.
You have to invoke the `mvn` command with a given profile setting to enable them:

cd junixsocket-demo-jpackagejlink
mvn clean verify -Djpackage -Djlink

Omit either `-Djpackage` or `-Djlink` if desired.

See the [junixsocket-demo-jpackagejlink POM file](pom.xml) for how this is done.

## Results

See `junixsocket-demo-jpackagejlink/target/jpackage` for the binary package result (the actual file
depends on your target platform).

See `junixsocket-demo-jpackagejlink/target/jlink/image` for the Java runtime optimized for the demo
code.

## References

* [Tools Reference: jlink](https://docs.oracle.com/en/java/javase/11/tools/jlink.html)
* [Tools Reference: jpackage](https://docs.oracle.com/en/java/javase/14/docs/specs/man/jpackage.html)
206 changes: 206 additions & 0 deletions junixsocket-demo-jpackagejlink/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
<?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>
<artifactId>junixsocket-demo-jpackagejlink</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.kohlschutter.junixsocket</groupId>
<artifactId>junixsocket</artifactId>
<version>2.9.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<name>junixsocket-demo-jpackagejlink</name>
<properties>
<kohlschutter.project.base.directory>${project.parent.basedir}</kohlschutter.project.base.directory>
<mainClass>org.newsclub.net.unix.demo.jpackagejlink.DemoMainClass</mainClass>
</properties>

<description>junixsocket jpackage/jlink demo setup</description>

<profiles>
<profile>
<id>jpackage</id>
<activation>
<property>
<name>jpackage</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>jpackage-copy-main-jar</id>
<phase>verify</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/jpackage-app</outputDirectory>
<resources>
<resource>
<directory>
${project.build.directory}</directory>
<includes>
<include>
${project.name}-${project.version}.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.akman</groupId>
<artifactId>jpackage-maven-plugin</artifactId>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>jpackage</goal>
</goals>
<configuration>
<mainclass>
${mainClass}</mainclass>
<type>PLATFORM</type>
<modulepath>
<dependencysets>
<dependencyset>
<!-- add project output (module)
to modulepath -->
<includeoutput>true</includeoutput>
<excludeautomatic>false</excludeautomatic>
<includes>
<include>glob:**/*.jar</include>
</includes>
</dependencyset>
</dependencysets>
</modulepath>
<addmodules>
<addmodule>java.base</addmodule>
<!-- junixsocket-common -->
<addmodule>org.newsclub.net.unix</addmodule>
<!-- junixsocket-native-common -->
<addmodule>
com.kohlschutter.junixsocket.nativecommon</addmodule>
<!-- this module -->
<addmodule>
org.newsclub.net.unix.demo.jpackagejlink</addmodule>
</addmodules>
<input>
${project.build.directory}/jpackage-app</input>
<mainjar>
${project.name}-${project.version}.jar</mainjar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

<pluginManagement>
<plugins>
<plugin>
<groupId>com.github.akman</groupId>
<artifactId>jpackage-maven-plugin</artifactId>
<version>0.1.5</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>

<profile>
<id>jlink</id>
<activation>
<property>
<name>jlink</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.github.akman</groupId>
<artifactId>jlink-maven-plugin</artifactId>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>jlink</goal>
</goals>
<configuration>
<launcher>
<command>junixsocket-jlink-demo</command>
<mainmodule>
org.newsclub.net.unix.demo.jpackagejlink</mainmodule>
<mainclass>
${mainClass}</mainclass>
</launcher>
<modulepath>
<dependencysets>
<dependencyset>
<!-- add project output (module)
to modulepath -->
<includeoutput>true</includeoutput>
<excludeautomatic>false</excludeautomatic>
<includes>
<include>glob:**/*.jar</include>
</includes>
</dependencyset>
</dependencysets>
</modulepath>
<addmodules>
<addmodule>java.base</addmodule>
<!-- junixsocket-common -->
<addmodule>org.newsclub.net.unix</addmodule>
<!-- junixsocket-native-common -->
<addmodule>
com.kohlschutter.junixsocket.nativecommon</addmodule>
<!-- this module -->
<addmodule>
org.newsclub.net.unix.demo.jpackagejlink</addmodule>
</addmodules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

<pluginManagement>
<plugins>
<plugin>
<groupId>com.github.akman</groupId>
<artifactId>jlink-maven-plugin</artifactId>
<version>0.1.11</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>

<repositories>
<repository>
<id>sonatype.snapshots</id>
<name>Sonatype snapshot repository</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<layout>default</layout>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.kohlschutter.junixsocket</groupId>
<artifactId>junixsocket-core</artifactId>
<version>${project.version}</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
11 changes: 11 additions & 0 deletions junixsocket-demo-jpackagejlink/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* jpackage/jlink demo module
*/
module org.newsclub.net.unix.demo.jpackagejlink {
exports org.newsclub.net.unix.demo.jpackagejlink;

requires java.base;

requires org.newsclub.net.unix;
requires com.kohlschutter.junixsocket.nativecommon;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* junixsocket
*
* Copyright 2009-2023 Christian Kohlschütter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.newsclub.net.unix.demo.jpackagejlink;

import org.newsclub.net.unix.AFUNIXSocket;

/**
* The entrypoint class for the jpackage or jlink demo binary.
*
* @author Christian Kohlschütter
*/
public final class DemoMainClass {

private DemoMainClass() {
}

/**
* The entrypoint method for the jpackage or jlink demo binary.
*
* @param args The program arguments.
*/
public static void main(String[] args) {
// run a simple built-in selftest
AFUNIXSocket.main(new String[0]);
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ In contrast to other implementations, junixsocket extends the Java Sockets API (
<module>junixsocket-mysql</module>
<module>junixsocket-core</module>
<module>junixsocket-demo</module>
<module>junixsocket-demo-jpackagejlink</module>
<module>junixsocket-dist</module>
<module>junixsocket-codecoverage</module>
</modules>
Expand Down

0 comments on commit 34d7441

Please sign in to comment.