logo RManniBucau Blog
    • Java Code Geeks

    (J)Link your Java application before putting it into Docker! Part 2/3

    , Romain Manni-Bucau, 2019-02-21, 2 min and 42 sec read

    In the previous blogpost , we saw how to get started to develop a Java module. To go further, this post will show you how to build a custom Java distribution using the new jlink tool with Apache Maven.

    Previously, we created a module (let's name it "demo") and bundled it with Maven using the default packaging type : jar.

    In this post, we will create a new module - let's name it "demo-link". This one will produce a custom Java distribution linking the previous module.

    The first step is to create a new Maven module with "demo" module as a dependency:

    <?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">
      <parent>
        <artifactId>demo-parent</artifactId>
        <groupId>com.github.rmannibucau</groupId>
        <version>1.0.0-SNAPSHOT</version>
      </parent>
      <modelVersion>4.0.0</modelVersion>
    
      <artifactId>demo-link</artifactId>
      <name>Demo :: JLink</name>
    
      <dependencies>
        <dependency>
          <groupId>com.github.rmannibucau</groupId>
          <artifactId>demo</artifactId>
          <version>${project.version}</version>
        </dependency>
      </dependencies>
    </project>

    Nothing special for this pom and the nice thing is that the dependency management has nothing special.

    Then we add Maven JLink plugin. To set it up you need to:

    • Define the plugin as an extension - since it brings a new packaging type,
    • Change your module packaging to jlink.

    Here is our modified 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
               http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <parent>
        <artifactId>demo-parent</artifactId>
        <groupId>com.github.rmannibucau</groupId>
        <version>1.0.0-SNAPSHOT</version>
      </parent>
      <modelVersion>4.0.0</modelVersion>
    
      <artifactId>demo-link</artifactId>
      <name>Demo :: JLink</name>
      <packaging>jlink</packaging> <!-- 1 -->
    
      <dependencies>
        <dependency>
          <groupId>com.github.rmannibucau</groupId>
          <artifactId>demo</artifactId>
          <version>${project.version}</version>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jlink-plugin</artifactId>
            <version>3.0.0-alpha-1</version>
            <extensions>true</extensions> <!-- 2 -->
            <configuration>
              <noHeaderFiles>true</noHeaderFiles>
              <noManPages>true</noManPages>
              <stripDebug>true</stripDebug>
            </configuration>
            <dependencies>
              <dependency> <!-- 3 -->
                <groupId>org.ow2.asm</groupId>
                <artifactId>asm</artifactId>
                <version>7.0</version>
              </dependency>
            </dependencies>
          </plugin>
        </plugins>
      </build>
    </project>?
    1. The packaging type being jlink, the build will use the maven-jlink-plugin to build a custom ditribution,
    2. If you don't set this plugin as an extension you can't use jlink packaging type,
    3. Last release of the jlink plugin does not support Java 11 until you upgrade ASM so don't forget to use at least ASM 7 if you build a Java 11 application.

    Once this is done, you can just build your project and you will find in your "demo-link" build directory two new things:

    • A "maven-jlink" folder which is the default location of the custom Java distribution,
    • A zip (its name being your module final name, for instance demo-link-1.0.0-SNAPSHOT.zip) which contains previous folder and is attached to the build - i.e. deployed when running mvn deploy.

    If you go into the jlink folder, you have your application which is just a light JRE. Thus, you can run it using bin/java command.

    If you want to launch a main created in "demo" module (see previous post) you can use that command:

    ./bin/java \
      --add-modules com.github.rmannibucau.demo \
      com.github.rmannibucau.demo.Main

    The exploded folder is about 43M, most of the actual code will be in the binary file lib/modules. Note that it can change the behavior of some libraries. Typically the URL of resources (and classes) will go from file://..... or jar:file://.... to jrt://.... URLs which can not be supported by a lot of libraries. So make sure you test it before running the produced application in production.

    We saw how to build a native image relying on JLink in this post. In the next blogpost, I will show you how to build a Docker image using that binary thanks to JIB library.

    From the same author:

    • Romain Manni-Bucau

    In the same category:

    • Java/EE/Microprofile

    • Previous

    • All posts

    • Next

    Navigation

    • Java Code Geeks
    RManniBucau ©
    Search