Creating distributions for different operating systems in Java 9 and 10

    The article talks about building full-fledged distributions for Windows , macOS and Linux using standard Java 9 and 10 tools .

    Supplements a previously published article on reducing the size of the distribution , focusing not on modularity, but on the features of creating a distribution for different operating systems.

    The changes that have occurred since Java 9 are listed . The sequence of steps performed by the build script is described, indicating the places where behavior changes and settings are possible. A story is presented with a happy ending to overcome the features and errors that appeared in Java 9 .



    Prologue


    Desktop ( desktop ) applications written in programming the Java , still have the right to exist. The most striking example is IntelliJ IDEA , distributions for installing which exist for different operating systems.

    The JDK includes the Java Packager command-line utility for compiling, digitally signing, and building Java application distribution kits. The utility first appeared in JDK 7 Update 6 . In addition to using the command line its functions are available in the form of tasks ( tasks ) for Ant . In the documentation, they are called JavaFX Ant Tasks .

    When writing the article were used:


    The project can be found on GitHub . Compilation and assembly of the distribution is carried out using Maven . The sample project includes three parts (modules in Maven terminology ):

    • multiplatform-distribution-client with application code;
    • multiplatform-distribution-distrib with auxiliary distribution files;
    • multiplatform-distribution-resources with resources for installers.

    Distribution build


    The table lists the steps in the formation of distributions in order of execution. Those stages are specially marked in bold at which a need or desire may arise to change the behavior, appearance or composition of distributions.
    No. p / pMaven moduleMaven pluginMaven Life Cycle PhaseStage
    1multiplatform-distribution-resourcesmaven-resources-pluginprocess-resourcesSubstitution of string values ​​in files .iss(for Windows ) and .plist(for macOS )
    2multiplatform-distribution-clientmaven-dependency-plugingenerate-sourcesCreating a list of dependencies for a manifest
    3build-helper-maven-plugingenerate-sourcesReplacing the separator in the list of dependencies for the manifest
    4maven-compiler-plugincompileCode compilation
    5maven-jar-pluginpackageFile creation .jar
    6maven-dependency-pluginpackageCopying dependencies for distribution
    7maven-resources-pluginpackageCopying additional files for the distribution
    8maven-antrun-pluginpackage
    1. Using graphic and configuration files to build the distribution kit
    2. Creating a JRE Image
    3. Generating a distribution for a specific operating system

    9maven-assembly-pluginpackageGenerating a distribution file .tar.gzfor Linux (only when running on Linux )
    10multiplatform-distribution-distribmaven-assembly-pluginpackage
    1. Substitution of string values ​​in files .bat(for Windows ), .sh(for macOS and Linux ) and licenses
    2. Formation of a universal distribution .zip(without JRE )


    To create distributions on macOS and Linux , you need only the JDK and Maven . In Windows, you must first install Inno Setup or WiX Toolset . It is further understood that Inno Setup is being used .

    Starting compilation and assembly of the distribution package on Windows and macOS :

    mvn clean package -P native-deploy

    Starting compilation and assembly of the distribution package on Linux (an archive file is additionally created tar.gz):

    mvn clean package -P native-deploy,tar-gz

    Files of the created distribution with the extension exe(for Windows ), dmg(for macOS ) are located in the directory multiplatform-distribution-client/target/deploy/native, with the extension tar.gz(for Linux ) - in the directory multiplatform-distribution-client/target.

    A universal distribution with an extension zipsuitable for any operating system and not containing a JRE is created in a directory multiplatform-distribution-distrib/target.

    Java 9 and 10 Features


    The long-awaited release of Java 9 destructively affected the distribution build script, which had previously worked successfully in the previous version of Java .

    First task, which performs the formation of the JRE image and the creation of the distribution kit, slightly changed the structure of the directories it uses . The build script has been modified to accommodate this.

    Secondly , when creating distribution installers, text and graphic resources stopped loading , see JDK-8186683 . The inability to load resources from the classpath when building the distribution in Java 9 is compensated by the addition of the new dropinResourcesRoot argument . We recommend specifying the path to the resource directory as the argument value. In the description of the task in the file, pom.xmlit will look like this:



    Thirdly, due to errors in the implementation in Java 9, the ability to include subdirectories with files , for example, a subdirectory libwith program dependent libraries, was lost in the distribution . The error only appeared on Windows and macOS . Overcoming this problem turned into a whole detective story and was forced to stretch for a long time, postponing the publication of the article.

    Chronicle of events:

    1. The OpenJFX repository has been cloned and an error has been found.
    2. An existing JDK-8179033 with the description of the same symptoms was discovered .
    3. On the advice of lany (thank you very much) letters were written once and twice to the openjfx-dev group , with suggestions of the changes required to fix the error, and advice on how to check their correctness.
    4. Correspondence with the performers of JDK-8179033 - I thank Viktor Drozdov for his friendly attitude and patience.
    5. The bug has been fixed and the bug has got into the next build of the preliminary version of Java 10 - jdk-10-ea + 36 .
    6. The distribution build, launched from the command line, began to be performed successfully.
    7. When trying to add jdk-10-ea + 36 to the SDK in IntelliJ IDEA , an error (unlike previous builds) created IDEA-183920 .
    8. The IDEA-183920 commentator indicated the reason for the JDK adding error - the javah utility disappeared in this JDK assembly , including the presence of which was required for successful identification.
    9. Yield IntelliJ IDEA 2017.3.2 , in which the identification error JDK 10 fixed.
    10. Building the distribution kit became possible from the development environment too.

    Customizing the example


    1. Rename the project Maven modules , replacing the multiplatform-distribution prefix in the name with something else.
    2. Rename multiplatform-distribution.batand files multiplatform-distribution.shlocated in the multiplatform-distribution-distrib module .
    3. Edit in files pom.xml:

      • names of renamed Maven modules ;
      • Directory names corresponding to renamed Maven modules .
    4. Edit in files assembly.xml:
      • names of renamed Maven modules ;
      • mention of changed file names multiplatform-distribution.batand multiplatform-distribution.sh.
    5. Change the root pom.xmlvalue of properties with names in the filecontaining:

      • full name of the application;
      • short name of the application;
      • year (-a) copyright;
      • application code in file names;
      • The default application package
      • the name of the class to run the application.
    6. Add your own code to the multiplatform-distribution-client module .
    7. Change the contents of the file license.txtand readme.txtthe module multiplatform-client-distribution .
    8. Change the contents of image files and their names in the multiplatform-distribution-resources module .

    conclusions


    • using standard JDK 9 and 10 tools, you can build distributions for various operating systems;
    • customization of the composition, behavior and type of distributions is relatively simple;
    • in Java 9 there were some features that were taken into account when writing this article.

    JEP 311 has recently appeared : Java Packager API & CLI to create a new API and CLI (command line interface) for Java Packager . JEP is currently rejected; previously it was planned to make changes in JDK 10 and JDK 11 . With the resumption of activity, this article may be continued in the near future.

    At the upcoming March 4 ( JBreak 2018 in Novosibirsk) and April 6-7 ( JPoint 2018 in Moscow) conferences, you can attend reports on related topics on Java 9 and future versions of Java :


    Also popular now: