Class JLinkMojo

java.lang.Object
org.apache.maven.plugin.AbstractMojo
All Implemented Interfaces:
org.apache.maven.plugin.ContextEnabled, org.apache.maven.plugin.Mojo

@Mojo(name="jlink", requiresDependencyResolution=RUNTIME, defaultPhase=PACKAGE) public class JLinkMojo extends AbstractJLinkMojo
Creates a self-contained java application with the jlink tool.

This mojo works for modular, non-modular and even mixed applications. It's not just a wrapper for jlink, but analyzes the project's dependencies and finds the best strategy to invoke jlink to create a directory containing an application-specific jimage module. As a result, it requires only minimum configuration.

Basically, applications fall into one of 3 categories:

  1. Full-blown modular applications: all module-infos must require real modules only. Jlink creates an image from those modules. Optional artifacts and runtime modules can still be added.
  2. Modular applications with non-modular dependencies: jlink is used to create an image from the minimum necessary java runtime modules only, which are determined by the plugin either from the module-infos or via the jdeps tool. The application's dependencies are placed on the modulepath via the generated run-script.
  3. Non-modular traditional classpath applications: same as 2, but all dependencies are placed on the classpath.
Since it is very likely, that even modern modular applications require some 3rd-party dependencies not modularized yet, most of those applications will probably fall into the second category.

Artifacts not processed by jlink are copied to separate folders and passed to the java runtime explicitly via the module- and/or classpath. A platform-specific launch script will be generated according to the runTemplate. For applications using Tentackle's auto update feature, an update script is generated via the updateTemplate as well. Finally, the created directory is packed into a deployable zip file.

The minimum plugin configuration is very simple:

   ...
   <packaging>jlink</packaging>
   ...
       <plugin>
         <groupId>org.tentackle</groupId>
         <artifactId>tentackle-jlink-maven-plugin</artifactId>
         <version>${tentackle.version}</version>
         <extensions>true</extensions>
         <configuration>
           <mainModule>com.example</mainModule>
           <mainClass>com.example.MyApp</mainClass>
         </configuration>
       </plugin>
 
The freemarker templates are copied to the project's template folder, if missing. They become part of the project and can be changed easily according to project specific needs (for example by adding runtime arguments). To install and edit the templates before running jlink (or jpackage, see JPackageMojo), use InitMojo first.

The template model provides the following variables:

  • mainModule: the name of the main module. Empty if classpath application.
  • mainClass: the name of the main class.
  • modulePath: the module path.
  • classPath: the class path
  • phase: the mojo lifecycle phase
  • goal: the plugin goal (jlink or jpackage)
  • id: the execution id
  • all system properties (dots in property names translated to camelCase, e.g. "os.name" becomes "osName"
  • all maven properties (translated to camelCase as well)
  • the plugin configuration AbstractJLinkMojo.variables
Modules not passed to jlink and automatic modules are copied to the mp folder and added to the modulePath template variable. If no such modules are detected, no folder is created.
Non-modular classpath artifacts are copied to the cp folder and added to the classPath template variable. Again, the folder is only created if necessary.
Additional project resources, such as property files or logger configurations, are copied to the conf directory and this directory is prepended to the classpath.

The generation of the ZIP-file and attachment of the artifact for installation and deployment can be customized by an application-specific implementation. This allows further modification of the generated image or files in the jlink target directory. It is also possible to add more than one artifact, for example, each with a different configuration. To do so, provide a plugin dependency that contains a class annotated with @Service(ArtifactCreator).

Notice that you can create an image for a different java version than the one used for the maven build process by specifying an explicit AbstractTentackleMojo.jdkToolchain.

  • Field Details

  • Constructor Details

    • JLinkMojo

      public JLinkMojo()
  • Method Details

    • getNameTemplate

      public String getNameTemplate()
      Gets the name of the name template.
      Returns:
      the template to generate the name of the runner script
    • getRunTemplate

      public String getRunTemplate()
      Gets the name of the runner template.
      Returns:
      the template to generate the runner script
    • getUpdateTemplate

      public String getUpdateTemplate()
      Gets the name of the update template.
      Returns:
      the template to generate the update script, null if don't generate an update script
    • executeImpl

      public void executeImpl() throws org.apache.maven.plugin.MojoExecutionException, org.apache.maven.plugin.MojoFailureException
      Overrides:
      executeImpl in class AbstractJLinkMojo
      Throws:
      org.apache.maven.plugin.MojoExecutionException
      org.apache.maven.plugin.MojoFailureException
    • generateFiles

      protected void generateFiles(JLinkResolver.Result result) throws org.apache.maven.plugin.MojoExecutionException
      Description copied from class: AbstractJLinkMojo
      Generates additional files such as shell scripts or command files.
      Specified by:
      generateFiles in class AbstractJLinkMojo
      Parameters:
      result - the resolver result
      Throws:
      org.apache.maven.plugin.MojoExecutionException - if generation failed
    • validate

      protected boolean validate() throws org.apache.maven.plugin.MojoExecutionException
      Overrides:
      validate in class AbstractJLinkMojo
      Throws:
      org.apache.maven.plugin.MojoExecutionException
    • installTemplates

      protected void installTemplates(boolean overwrite) throws org.apache.maven.plugin.MojoExecutionException
      Description copied from class: AbstractJLinkMojo
      Copies the templates to the template directory.
      Specified by:
      installTemplates in class AbstractJLinkMojo
      Parameters:
      overwrite - true if overwrite existing templates, false if install only missing
      Throws:
      org.apache.maven.plugin.MojoExecutionException - if installation failed