Class JlinkMojo

  • All Implemented Interfaces:
    org.apache.maven.plugin.ContextEnabled, org.apache.maven.plugin.Mojo

    @Mojo(name="jlink",
          requiresDependencyResolution=RUNTIME,
          defaultPhase=PACKAGE,
          requiresProject=true)
    public class JlinkMojo
    extends AbstractJlinkMojo
    Creates a self-contained java application zip-file with jlink.
    This mojo works for modular, non-modular and even mixed applications. It does so by analyzing the dependencies and finding the best strategy to invoke jlink, which creates a directory containing an application-specific jimage module.
    Artifacts not processed by jlink are copied to separate folders. Next, a platform-specific run-script will be generated according to a template provided by the project. Finally, the created directory is packed into a zip file.

    Basically, applications fall into one of 3 categories:

    1. full-blown modular applications: all module-infos must only require real modules. Jlink is used to create an image from the application modules. Optional (but not required!) real and automatic modules are allowed and will be added to the module path via the generated run-script.
    2. modular applications with non-modular dependencies: jlink is used to create an image from java's runtime modules only, which are determined by the plugin either from the module-infos or via jdeps. The application's dependencies are placed on the module-path via the generated run-script.
    3. non-modular applications: same as 2, but all dependencies are placed on the classpath.
    The minimum plugin configuration is very simple:
           <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 it does not exist, i.e. when the goal is invoked the first time. They become part of the project and can be changed easily according to project specific needs (for example by adding runtime arguments). 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
    • goal: the plugin goal (jlink or jpackage)
    • all system properties (dots in property names translated to camelCase, e.g. "os.name" becomes "osName"
    • all maven properties (translated to camelCase as well)
    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 resources, such as property files or logger configurations, are copied to the conf directory and this directory is added to the classpath.
    • Constructor Detail

      • JlinkMojo

        public JlinkMojo()
    • Method Detail

      • getImageDirectory

        public java.io.File getImageDirectory()
        Gets the directory created by jlink holding the image.
        Specified by:
        getImageDirectory in class AbstractJlinkMojo
        Returns:
        the image dir, never null
      • prepareExecute

        public void prepareExecute()
                            throws org.apache.maven.plugin.MojoExecutionException,
                                   org.apache.maven.plugin.MojoFailureException
        Overrides:
        prepareExecute in class AbstractJlinkMojo
        Throws:
        org.apache.maven.plugin.MojoExecutionException
        org.apache.maven.plugin.MojoFailureException
      • 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
      • createImage

        protected void createImage​(JlinkResolver.Result result)
                            throws org.apache.maven.plugin.MojoExecutionException,
                                   org.apache.maven.plugin.MojoFailureException
        Creates the jlink image.
        Specified by:
        createImage in class AbstractJlinkMojo
        Parameters:
        result - the resolver info
        Throws:
        org.apache.maven.plugin.MojoExecutionException - if building the JPMS info failed
        org.apache.maven.plugin.MojoFailureException - if jlink returned an error code
      • validate

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