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 .
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 ):
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.
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 :
Starting compilation and assembly of the distribution package on Linux (an archive file is additionally created
Files of the created distribution with the extension
A universal distribution with an extension
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,
Thirdly, due to errors in the implementation in Java 9, the ability to include subdirectories with files , for example, a subdirectory
Chronicle of events:
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 :
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:
- operating systems Windows 10 , macOS High Sierra 10.13 , Ubuntu 16.04.3 LTS ;
- JDK 9 and JDK 10 Early-Access Builds for all three operating systems;
- Apache Maven 3.5.0 ;
- Inno Setup 5.5.9 ;
- An IDE with support for Java 9 and Java 10 (e.g. IntelliJ IDEA 2017.3.2 or later).
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 / p | Maven module | Maven plugin | Maven Life Cycle Phase | Stage |
---|---|---|---|---|
1 | multiplatform-distribution-resources | maven-resources-plugin | process-resources | Substitution of string values in files .iss (for Windows ) and .plist (for macOS ) |
2 | multiplatform-distribution-client | maven-dependency-plugin | generate-sources | Creating a list of dependencies for a manifest |
3 | build-helper-maven-plugin | generate-sources | Replacing the separator in the list of dependencies for the manifest | |
4 | maven-compiler-plugin | compile | Code compilation | |
5 | maven-jar-plugin | package | File creation .jar | |
6 | maven-dependency-plugin | package | Copying dependencies for distribution | |
7 | maven-resources-plugin | package | Copying additional files for the distribution | |
8 | maven-antrun-plugin | package |
| |
9 | maven-assembly-plugin | package | Generating a distribution file .tar.gz for Linux (only when running on Linux ) | |
10 | multiplatform-distribution-distrib | maven-assembly-plugin | package |
|
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
zip
suitable 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
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.xml
it 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
lib
with 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:
- The OpenJFX repository has been cloned and an error has been found.
- An existing JDK-8179033 with the description of the same symptoms was discovered .
- 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.
- Correspondence with the performers of JDK-8179033 - I thank Viktor Drozdov for his friendly attitude and patience.
- 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 .
- The distribution build, launched from the command line, began to be performed successfully.
- When trying to add jdk-10-ea + 36 to the SDK in IntelliJ IDEA , an error (unlike previous builds) created IDEA-183920 .
- 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.
- Yield IntelliJ IDEA 2017.3.2 , in which the identification error JDK 10 fixed.
- Building the distribution kit became possible from the development environment too.
Customizing the example
- Rename the project Maven modules , replacing the multiplatform-distribution prefix in the name with something else.
- Rename
multiplatform-distribution.bat
and filesmultiplatform-distribution.sh
located in the multiplatform-distribution-distrib module . - Edit in files
pom.xml
:- names of renamed Maven modules ;
- Directory names corresponding to renamed Maven modules .
- Edit in files
assembly.xml
:- names of renamed Maven modules ;
- mention of changed file names
multiplatform-distribution.bat
andmultiplatform-distribution.sh
.
- Change the root
pom.xml
value 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.
- Add your own code to the multiplatform-distribution-client module .
- Change the contents of the file
license.txt
andreadme.txt
the module multiplatform-client-distribution . - 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 :
- JDK 9, Mission Accomplished: What next for Java? (Simon Ritter)
- Migrating to Java 9 modules (Sander Mak)
- Refactoring your code to Java 9 modules (Rabea Gransberger)
- JavaFX on JDK 9 (Gerrit Grunwald)