JavaPackager is a hybrid plugin for Maven and Gradle which provides an easy way to package Java applications in native Windows, Mac OS X or GNU/Linux executables, and generate installers for them.
SNAPSHOT version is not released to Maven Central, so you have to install it manually.
π See JavaPackager changes and fixes.
It was born while teaching to my students how to build and distribute their Java apps, and after seeing that a chain of several plugins was needed to achieve this task, I decided to develop a plugin π to govern them all.
Add the following plugin
tag to your pom.xml
:
<plugin>
<groupId>io.github.fvarrui</groupId>
<artifactId>javapackager</artifactId>
<version>1.6.7</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>package</goal>
</goals>
<configuration>
<!-- mandatory -->
<mainClass>path.to.your.mainClass</mainClass>
<!-- optional -->
<bundleJre>true|false</bundleJre>
<generateInstaller>true|false</generateInstaller>
<administratorRequired>true|false</administratorRequired>
<platform>auto|linux|mac|windows</platform>
<additionalResources>
<additionalResource>file path</additionalResource>
<additionalResource>folder path</additionalResource>
<additionalResource>...</additionalResource>
</additionalResources>
<linuxConfig>...</linuxConfig>
<macConfig>...</macConfig>
<winConfig>...</winConfig>
[...]
</configuration>
</execution>
</executions>
</plugin>
See Maven plugin configuration samples to know more.
And execute the next command in project's root folder:
mvn package
Apply JavaPackager plugin in build.gradle
using legacy mode (because at the moment it's only available in Maven Central repository):
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'io.github.fvarrui:javapackager:1.6.7'
}
}
apply plugin: 'io.github.fvarrui.javapackager.plugin'
Create your packaging task:
task packageMyApp(type: io.github.fvarrui.javapackager.gradle.PackageTask, dependsOn: build) {
// mandatory
mainClass = 'path.to.your.mainClass'
// optional
bundleJre = true|false
generateInstaller = true|false
administratorRequired = true|false
platform = auto|linux|mac|windows
additionalResources = [ file('file path'), file('folder path'), ... ]
linuxConfig {
...
}
macConfig {
...
}
winConfig {
...
}
...
}
See Gradle plugin configuration samples to know more.
And execute the next command in project's root folder:
gradle packageMyApp
By default it will generate next artifacts in ${outputDirectory}
folder:
Artifact | Description | Platform | Requires |
---|---|---|---|
${name} |
Directory with native application and other assets. | All | |
${name}-${version}-runnable.jar |
Runnable JAR file. | All | |
${name}_${version}.AppImage |
AppImage package file. | GNU/Linux | FUSE 2 to run the app. |
${name}_${version}.deb |
DEB package file. | All | |
${name}_${version}.rpm |
RPM package file. | All | |
${name}_${version}.exe |
Setup file. | Windows | Inno Setup |
${name}_${version}.msi |
MSI installer file. | Windows | WiX Toolset |
${name}_${version}.msm |
MSI merge module file. | Windows | WiX Toolset |
${name}_${version}.dmg |
Disk image file (uses hdiutil). | Mac OS | |
${name}_${version}.pkg |
PKG installer file (uses pkgbuild). | Mac OS | |
${name}-${version}-${platform}.zip |
Zipball containing generated directory ${name} . |
All | |
${name}-${version}-${platform}.tar.gz |
Compressed tarball containing generated directory ${name} . |
All | |
assets |
Directory with all intermediate files generated by JavaPackager. | All |
Inno Setup and WiX Toolset installation guide.
Property | Mandatory | Default value | Description |
---|---|---|---|
additionalModulePaths |
β | [] |
Additional module paths for jdeps . |
additionalModules |
β | [] |
Additional modules to the ones identified by jdeps or the specified with modules property. |
additionalResources |
β | [] |
Additional files and folders to include in the bundled app. |
administratorRequired |
β | false |
App will run as administrator (with elevated privileges). |
assetsDir |
β | ${basedir}/assets or ${projectdir}/assets |
Assets location (icons and custom Velocity templates). |
bundleJre |
β | false |
Embeds a customized JRE with the app. |
classpath |
β | List of additional paths to JVM classpath, separated with ; (recommended) or : . |
|
copyDependencies |
β | true |
Bundles all dependencies (JAR files) with the app. |
createTarball |
β | false |
Bundles app folder in tarball. |
createZipball |
β | false |
Bundles app folder in zipball. |
customizedJre |
β | true |
Generates a customized JRE, including only identified or specified modules. Otherwise, all modules will be included. |
description |
β | ${project.description} or ${displayName} |
Project description. |
displayName |
β | ${project.name} or ${name} |
App name to show. |
envPath |
β | Defines PATH environment variable in GNU/Linux and Mac OS X startup scripts. | |
extra |
β | Map with extra properties to be used in customized Velocity templates, accesible through $info.extra variable. |
|
fileAssociations |
β | FileAssociation[] |
Associate file extensions or MIME types to the app. |
forceInstaller |
β | false |
If true , skips operating system check when generating installers. |
generateInstaller |
β | true |
Generates an installer for the app. |
jdkPath |
β | ${java.home} |
JDK used to generate a customized JRE. It allows to bundle customized JREs for different platforms. |
jreDirectoryName |
β | "jre" |
Bundled JRE directory name. |
jreMinVersion |
β | JRE minimum version. If an appropriate version cannot be found display error message. Disabled if a JRE is bundled. | |
jrePath |
β | "" |
Path to JRE folder. If specified, it will bundle this JRE with the app, and won't generate a customized JRE. For Java 8 version or least. |
licenseFile |
β | ${project.licenses[0].url} or ${basedir}/LICENSE or ${projectdir}/LICENSE |
Path to project license file. |
mainClass |
βοΈ | ${exec.mainClass} |
Full path to your app main class. |
manifest |
β | Allows adding additional entries to MANIFEST.MF file. | |
modules |
β | [] |
Modules to customize the bundled JRE. Don't use jdeps to get module dependencies. |
name |
β | ${project.name} or ${project.artifactId} |
App name. |
organizationName |
β | ${project.organization.name} or "ACME" |
Organization name. |
organizationUrl |
β | ${project.organization.url} |
Organization website URL. |
organizationEmail |
β | Organization email. | |
outputDirectory |
β | ${project.build.directory} or ${project.builddir} |
Output directory (where the artifacts will be generated). |
packagingJdk |
β | ${java.home} |
JDK used in the execution of jlink and other JDK tools. |
platform |
β | auto |
Defines the target platform, which could be different to the execution platform. Possible values: auto , mac , linux , windows . Use auto for using execution platform as target. |
runnableJar |
β | Defines your own JAR file to be bundled. If it's ommited, the plugin packages your code in a runnable JAR and bundle it with the app. | |
scripts |
β | Specify bootstrap script. Pre and post-install scripts comming soon! | |
url |
β | App website URL. | |
useResourcesAsWorkingDir |
β | true |
Uses app resources folder as default working directory (always true on Mac OS). |
version |
β | ${project.version} |
App version. |
vmArgs |
β | [] |
VM arguments. |
Some default values depends on the used building tool.
Platform specific properties
Property | Mandatory | Description |
---|---|---|
linuxConfig |
β | GNU/Linux specific properties. |
macConfig |
β | Mac OS X specific properties. |
winConfig |
β | Windows specific properties. |
β οΈ Be careful when using theplatform
property if your project uses platform dependent libraries, so the libraries of the current platform will be copied, not those required for the target platform. You can solve this problem usingclassifiers
.
Any asset used by JavaPackager, such as application icons or templates, can be replaced just by placing a file with the same name in ${assetsDir}
folder organized by platform.
${assetsDir}/
βββ linux/
βββ mac/
βββ windows/
If icons are located in ${assetsDir}
folder, it would not be necessary to use icon properties:
${assetsDir}/
βββ linux/
β βββ ${name}.png # on GNU/Linux it has to be a PNG file
βββ mac/
β βββ ${name}.icns # on Mac OS X it has to be a ICNS file
βββ windows/
βββ ${name}.ico # on Windows it has to be a ICO file
β οΈ If icon is not specified , it will use an icon by default for all platforms.
Velocity templates (.vtl
files) are used to generate some artifacts which have to be bundled with the app or needed to generate other artifacts.
It is possible to use your own customized templates. You just have to put one of the following templates in the ${assetsDir}
folder organized by platform, and the plugin will use these templates instead of default ones:
${assetsDir}/
βββ linux/
| βββ assembly.xml.vtl # maven-assembly-plugin template to generate ZIP/TGZ bundles for GNU/Linux
| βββ control.vtl # DEB control template
| βββ desktop.vtl # Desktop template
| βββ desktop-appimage.vtl # AppImage format Desktop template
| βββ mime.xml.vtl # MIME.XML template
β βββ startup.sh.vtl # Startup script template
βββ mac/
| βββ assembly.xml.vtl # maven-assembly-plugin template to generate ZIP/TGZ bundles for Mac OS X
| βββ customize-dmg.applescript.vtl # DMG customization Applescript template
| βββ Info.plist.vtl # Info.plist template
β βββ startup.vtl # Startup script template
βββ windows/
βββ assembly.xml.vtl # maven-assembly-plugin template to generate ZIP/TGZ bundles for Windows
βββ exe.manifest.vtl # exe.manifest template
βββ ini.vtl # WinRun4J INI template
βββ iss.vtl # Inno Setup Script template
βββ msm.wxs.vtl # WiX Toolset WXS template to generate Merge Module
βββ startup.vbs.vtl # Startup script template (VB Script)
βββ why-ini.vtl # WHY INI template
βββ wxs.vtl # WiX Toolset WXS template to generate MSI
An object called info
of type PackagerSettings
is passed to all templates with all plugin properties.
You can use default templates as examples to create your own templates, and use the extra
map property to add your own properties in the plugin settings to use in your custom templates (e.g. ${info.extra["myProperty"]}
).
When you build your app, all configuration details are hardcoded into the executable and cannot be changed without recreating it or hacking with a resource editor. JavaPackager introduces a feature that allows to pass additional JVM options at runtime from an .l4j.ini
file (like Launch4j does, but available for all platforms in the same way). So, you can specify these options in the packager's configuration (packaging time), in INI file (runtime) or in both.
The INI file's name must correspond to ${name}.l4j.ini
and it has to be located next to the executable on Windows and GNU/Linux, and in Resources
folder on Mac OS X.
The options should be separated with spaces or new lines:
# Additional JVM options
-Dswing.aatext=true
-Dsomevar="%SOMEVAR%"
-Xms16m
An VM argument per line.
And then bundle this file with your app:
<additionalResources>
<additionalResource>${name}.l4j.ini</additionalResource>
</additionalResources>
Last property copies
${name}.l4j.ini
file next to the EXE/binary on Windows/Linux, and inResources
folder on MacOS.
Useful to try SNAPSHOT versions.
Execute next commands in BASH (GNU/Linux or macOS) or CMD (Windows):
- Download source code and change to the project directory:
git clone https://github.com/fvarrui/JavaPackager.git [--branch x]
cd JavaPackager
where
x
is branch name (e.g.devel
)
- Compile, package and install the plugin in your local repository (ommit
./
on Windows):
./gradlew publishToMavenLocal
Run next command (ommit ./
on Windows):
./gradlew -Prelease uploadArchives closeAndReleaseRepository
Related guide.
Check the TO-DO list to know the features we plan to add to JavaPackager.