📜 ⬆️ ⬇️

Creating distributions for different operating systems in Java 9 and 10

The article discusses building complete distributions for Windows , macOS and Linux using standard Java 9 and 10 tools.

It complements the previously published article about reducing the size of the distribution , focusing not on modularity, but on the peculiarities of creating a distribution for various operating systems.

Lists the changes that occurred with Java 9 . Describes the sequence of steps performed by the build script, indicating the places where behavior change and customization are possible. There is a story with a happy ending in overcoming features and errors that appeared in Java 9 .
')


Prologue


Desktop applications written in the Java programming language still have a right to exist. The most striking example is IntelliJ IDEA , which distributions for installation exist for different operating systems.

The Java Packager command line utility is included in the JDK for compiling, creating digital signatures and assembling distributions of Java applications. The utility first appeared in JDK 7 Update 6 . In addition to using the command line, its functions are available as tasks for Ant . In the documentation, they are referred to as JavaFX Ant Tasks .

When writing the article were used:


The project can be found on github . The compilation and build of the distribution is carried out using Maven . The example project includes three parts (modules in Maven terminology):


Build distributive


The table lists the steps in forming the distributions in order. Specially highlighted in bold are the steps at which the need or desire to change the behavior, appearance or composition of distributions may arise.
Item numberMaven moduleMaven PluginPhase of the Maven life cycleStage
onemultiplatform-distribution-resourcesmaven-resources-pluginprocess-resourcesSubstitution of string values ​​in .iss (for Windows ) and .plist (for macOS ) files
2multiplatform-distribution-clientmaven-dependency-plugingenerate-sourcesForming the list of dependencies for the manifest
3build-helper-maven-plugingenerate-sourcesReplace delimiter in manifest dependency list
fourmaven-compiler-plugincompileCompiling code
fivemaven-jar-pluginpackageCreating a .jar file
6maven-dependency-pluginpackageCopying dependencies for distribution
7maven-resources-pluginpackageCopying additional files for distribution
eightmaven-antrun-pluginpackage
  1. Using graphics and configuration files to build the distribution
  2. JRE image creation
  3. Forming distribution for a specific operating system

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


To create distros on macOS and Linux , only JDK and Maven are needed. In the Windows operating system , you must first install Inno Setup or WiX Toolset . The following assumes Inno Setup is used .

Running compilation and build distribution in Windows and macOS :

 mvn clean package -P native-deploy 

Launching the compilation and build of the distribution in Linux (in addition, a tar.gz archive file is created):

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

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

A universal distribution with a zip extension, suitable for any operating system and not containing a JRE , is created in the multiplatform-distribution-distrib/target .

Java 9 and 10 features


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

First , the <fx: deploy> task, which forms the JRE image and creates the distribution, slightly changed the structure of the directories it uses . The build script has been modified to accommodate this.

Second , when creating distribution installers, text and graphic resources were no longer loaded , see JDK-8186683 . The inability to load resources from the classpath when building a distribution in Java 9 is compensated by adding a new dropinResourcesRoot argument. It is recommended to specify the path to the resource directory as the argument value. In the description of the task in the pom.xml file pom.xml it will look like this:



Thirdly , because of the mistakes made in the implementation of Java 9, the ability to include in the distribution subfolders with files , for example, the lib subdirectory with the library-dependencies of the program, was lost . The error manifested itself only in Windows and macOS . Overcoming this problem turned into a whole detective story and had to stretch for a long time, postponing the publication of the article.

Chronicle of events:

  1. OpenJFX repository cloned and an error was found.
  2. An existing JDK-8179033 was found with a description of the same symptoms.
  3. On the advice of lany (thank you very much), one and two letters were written to the openjfx-dev group, with a suggestion of the changes required to correct the error, and advice on how to verify their correctness.
  4. Correspondence with the performers JDK-8179033 - I thank Victor Drozdov for his friendly attitude and patience.
  5. The error was fixed and the fix got into the next build of the preliminary version of Java 10 - jdk-10-ea + 36 .
  6. Building the distribution from the command line was successful.
  7. When trying to add jdk-10-ea + 36 to IntelliJ IDEA in the SDK list, an error (unlike previous builds) created IDEA-183920 .
  8. The commentator IDEA-183920 indicates the cause of the JDK add error — the disappearance of the javah utility in this JDK build , including the presence of which was required for successful identification.
  9. Exit IntelliJ IDEA 2017.3.2 , in which the identification error JDK 10 is fixed.
  10. Building the distribution was possible from the development environment too.

Adaptation of the example for own use


  1. Rename the project's Maven modules, replacing the multiplatform-distribution prefix in the name with something else.
  2. Rename the multiplatform-distribution.bat and multiplatform-distribution.sh files located in the multiplatform-distribution-distrib module .
  3. Edit in pom.xml files:

    • names of the renamed Maven modules;
    • the names of the directories corresponding to the renamed Maven modules.
  4. Modify in assembly.xml files:
    • names of the renamed Maven modules;
    • Mention of modified file names for multiplatform-distribution.bat and multiplatform-distribution.sh .
  5. Modify in the root pom.xml file the pom.xml values ​​named <app. *> Containing:

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

findings



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

For the upcoming March 4 ( JBreak 2018 in Novosibirsk) and April 6-7 ( JPoint 2018 in Moscow) conferences, there is an opportunity to attend presentations on related topics on Java 9 and future versions of Java :

Source: https://habr.com/ru/post/340248/


All Articles