'How do I configure the --output of jlink command in maven-jlink-plugin?

I'm trying to generate a JRE using maven-jlink-plugin, just as I would with the jlink command. I started doing a maven configuration like this :

    <plugin>
        <artifactId>maven-jlink-plugin</artifactId>
        <version>3.1.0</version>
        <extensions>true</extensions>
        <configuration>
            <goal>jlink</goal>
            <compress>2</compress>
            <noHeaderFiles>true</noHeaderFiles>
            <noManPages>true</noManPages>
            <output>${project.build.directory}/jre</output>
            <addModules>
                <addModule>java.compiler</addModule>
                <addModule>java.datatransfer</addModule>
                <addModule>java.desktop</addModule>
                <addModule>java.instrument</addModule>
                <addModule>java.logging</addModule>
                <addModule>java.management</addModule>
                <addModule>java.naming</addModule>
                <addModule>java.prefs</addModule>
                <addModule>java.rmi</addModule>
                <addModule>java.scripting</addModule>
                <addModule>java.sql</addModule>
                <addModule>java.xml</addModule>
                <addModule>jdk.compiler</addModule>
                <addModule>jdk.unsupported</addModule>
            </addModules>
        </configuration>
    </plugin>

However, I can't find any element in the documenation: https://maven.apache.org/plugins/maven-jlink-plugin/jlink-mojo.html

Although maven didn't complain about my <output>, it didn't generate anything.

My command line version looks like this :

%JAVA_HOME%\bin\jlink --no-header-files --no-man-pages --compress=2 --module-path %JAVA_HOME%\jmods --add-modules java.base,java.compiler,java.datatransfer,java.desktop,java.instrument,java.logging,java.management,java.naming,java.prefs,java.rmi,java.scripting,java.sql,java.xml,jdk.compiler,jdk.unsupported --output target\jre


Solution 1:[1]

Apparently, the jlink maven plugin is not meant to do that. To run the command jlink with jre output, maven antrun plugin is suited for that :

    <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
            <execution>
                <phase>package</phase>
                <configuration>
                    <target>
                        <link compress="2" includeHeaders="false" modulepath="${java.home}/jmods"
                            includeManPages="false" debug="false" destDir="target/jre">
                            <module name="java.compiler"/>
                            <module name="java.datatransfer"/>
                            <module name="java.desktop"/>
                            <module name="java.instrument"/>
                            <module name="java.logging"/>
                            <module name="java.management"/>
                            <module name="java.naming"/>
                            <module name="java.prefs"/>
                            <module name="java.rmi"/>
                            <module name="java.scripting"/>
                            <module name="java.sql"/>
                            <module name="java.xml"/>
                            <module name="jdk.compiler"/>
                            <module name="jdk.unsupported"/>
                        </link>
                    </target>
                </configuration>
                <goals>
                    <goal>run</goal>
                </goals>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>org.apache.ant</groupId>
                <artifactId>ant</artifactId>
                <version>1.10.12</version>
            </dependency>
        </dependencies>
    </plugin>

Solution 2:[2]

For me this works:


    <build>

      <pluginManagement>
        <plugins>
          <plugin>
            <groupId>com.github.akman</groupId>
            <artifactId>jlink-maven-plugin</artifactId>
            <version>0.1.8</version>
            <configuration>
              <modulepath>
                <dependencysets>
                  <!-- add project dependencies to modulepath -->
                  <dependencyset>
                    <!-- add project output (module) to modulepath -->
                    <includeoutput>true</includeoutput>
                    <!-- exclude automatic modules -->
                    <excludeautomatic>true</excludeautomatic>
                  </dependencyset>
                </dependencysets>
              </modulepath>
              <noheaderfiles>true</noheaderfiles>
              <nomanpages>true</nomanpages>
              <compress>
                <compression>ZIP</compression>
              </compress>
              <stripdebug>true</stripdebug>
              <addmodules>
                <addmodule>java.base</addmodule>
                <addmodule>java.xml</addmodule>
              </addmodules>
            </configuration>
          </plugin>
        </plugins>
      </pluginManagement>


        <plugins>
          <plugin>
            <groupId>com.github.akman</groupId>
            <artifactId>jlink-maven-plugin</artifactId>
            <executions>
              <execution>
                <phase>verify</phase>
                <goals>
                  <goal>jlink</goal>
                </goals>
              </execution>
            </executions>
          </plugin>        
....
...

mvn clean compile package

mvn jlink:jlink

The result is in the target/jlink folder (about 28Mb for me).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sybuser
Solution 2 RandomB