Integrate Izpack to your gradle project

Asterios Raptis
4 min readApr 25, 2023

--

You have finished a java swing or javafx application in a gradle project and the next step is to package and deploy or distribut it and you desided to use Izpack for it. In this post i will describe how to do this.

We start with the gradle part. First we need to import the gradle plugin for izpack, then we have to declare the dependency to izpack. We declare first the gradle izpack plugin version and all other needed versions in the gradle.properties

###########################
# gradle-plugins versions #
###########################
gradleIzpackPluginVersion=3.2

#########################
# dependencies versions #
#########################
izpackVersion=5.1.3
##################
# jar properties #
##################
mainClass=io.github.astrapi69.silent.mouse.PureSwingSystemTray

In the build.gradle file we add the following entries

buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.bmuschko:gradle-izpack-plugin:$gradleIzpackPluginVersion"
}
}

apply plugin: "com.bmuschko.izpack"
// import gradle files
apply from: "gradle/dependencies.gradle"
apply from: "gradle/izpack.gradle"
apply from: "gradle/packaging.gradle"

I have extracted all gradle tasks to its own files for keep my configuration clearly arranged. Thats for we import them in the main build.gradle file. Note that all gradle files are written in groovy. Lets start to view the dependencies.gradle file which contains as the name predicts the dependencies

dependencies {
izpack("org.codehaus.izpack:izpack-ant:$izpackVersion")
...
}

Now we can define the izpack.gradle that contains the following entries

izpack {
baseDir = file("$buildDir")
installFile = file("src/main/izpack/install.xml")
outputFile = file("$buildDir/distributions/$rootProject.name-${projectVersion}-installer.jar")
compression = "deflate"
compressionLevel = 9
appProperties = ["app.group": "$groupPackage",
"app.name": "$rootProject.name",
"app.title": "$rootProject.name",
"app.version": "$projectVersion",
"app.subpath": "$rootProject.name-${projectVersion}"]
}

Now we can have a look at the packaging.gradle file that is responsible for create the jar files for deployment and distribution

import java.text.SimpleDateFormat
def mainClassName = project.property('mainClass')

jar {
manifest {
attributes(
"Name" : project.name,
"Manifest-Version" : project.version,
"Main-Class" : mainClassName,
"Implementation-Title" : "$groupPackage" + "." + "$rootProject.name",
"Implementation-Version": project.version,
"Implementation-Vendor" : "$projectLeaderName",
"Created-By" : "Gradle ${gradle.gradleVersion}",
"Built-By" : "$projectLeaderName",
"Build-Timestamp" : new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date()),
"Build-Jdk" : "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})",
"Build-OS" : "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}")
}
}

task withAllDependendiesJar(type: Jar) {
duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
manifest.from jar.manifest
archiveClassifier = "all"
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
with jar
}

We continue now with the izpack part. We create a new resource folder izpack under the project folder src/main which result to src/main/izpack. In this folder we create the install.xml file that declare the izpack installation.

<izpack:installation version="5.0"
xmlns:izpack="http://izpack.org/schema/installation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://izpack.org/schema/installation http://izpack.org/schema/5.0/izpack-installation-5.0.xsd">
<variables>
<variable name="appGroup" value="@{app.group}"/>
<variable name="appTitle" value="@{app.title}"/>
<variable name="appName" value="@{app.name}"/>
<variable name="appSubPath" value="@{app.subpath}"/>
<variable name="appVersion" value="@{app.version}"/>
<variable name="RESOURCEROOT" value="src/main/izpack" />
<!-- pre-select desktop shortcut checkbox -->
<variable name="DesktopShortcutCheckboxEnabled" value="true" />
</variables>

<info>
<javaversion>11</javaversion>
<appname>silent-mouse</appname>
<appsubpath>app/silent-mouse</appsubpath>
<appversion>1.1</appversion>
<authors>
<author name="Asterios Raptis" email="asterios.raptis@gmx.net" />
</authors>
<url>https://github.com/astrapi69/silent-mouse</url>
</info>

<locale>
<langpack iso3="eng" />
</locale>

<guiprefs width="800" height="600" resizable="no">
<splash>images/peas_load.gif</splash>
<laf name="kunststoff">
<os family="windows" />
<os family="unix" />
<param name="variant" value="mist-silver" />
</laf>
<laf name="kunststoff">
<os family="mac" />
<param name="variant" value="mist-aqua" />
</laf>
<modifier key="useHeadingPanel" value="yes" />
</guiprefs>

<resources>
<res src="../README.md" id="HTMLInfoPanel.info" />
<res src="../LICENSE" id="LicencePanel.licence" />
</resources>

<panels>
<panel classname="HelloPanel"/>
<panel classname="HTMLInfoPanel"/>
<panel classname="LicencePanel"/>
<panel classname="TargetPanel"/>
<panel classname="PacksPanel"/>
<panel classname="InstallPanel"/>
<panel classname="SimpleFinishPanel"/>
</panels>

<packs>
<pack name="executables" required="yes">
<description>The executables</description>
<file src="../CHANGELOG.md" targetdir="${INSTALL_PATH}" />
<file src="../LICENSE" targetdir="${INSTALL_PATH}" />
<singlefile src="libs/@{app.name}-@{app.version}-all.jar" target="${INSTALL_PATH}/@{app.name}.jar" />
<file src="../README.md" targetdir="${INSTALL_PATH}" />
<file src="resources/main/doc/start.sh" targetdir="${INSTALL_PATH}" />
<file src="resources/main/doc/start.bat" targetdir="${INSTALL_PATH}" />
</pack>
</packs>

</izpack:installation>

We declare here all needed variables for create the izpack installer. For more information go to the izpack documentation. We declared here two start scripts for windows(resources/main/doc/start.bat)

@ECHO OFF
start javaw -jar silent-mouse.jar

and for linux(resources/main/doc/start.sh)

#!/bin/bash
java -jar silent-mouse.jar

Now you we are ready to create the izpack installation file that can be deployed and distributed. As first step we need to create the jar file with all needed dependencies, therefore we execute the task ‘withAllDependendiesJar’ that is defined in the packaging.gradle file. This will generate the jar file with all dependencies in the build directory build/libs with the suffix ‘*-all.jar’.

We can proceed to step two which is to create the izpack installer jar file. The task for this process is called ‘izPackCreateInstaller’. After execution of this task the izpack installer jar file is generated in the build directory build/distributions with the suffix ‘*-installer.jar’. Note that this is all defined in the izpack.gradle file and can be configured to custom settings.

Now everyone can install your application. Additionally you can sign the jar file and than deploy it as an installer jar file. How to do this is described in this post.

Version used for this example:

  • gradle -> 8.0.1
  • gradle-izpack-plugin -> 3.2
  • izpack -> 5.1.3

For this post you can reference the github project silent-mouse and compare version 1 with 1.1 which contains an izpack installer with the signed version.

A good list of all java distribution tools can be found here.

Thanks for reading.

Happy coding

--

--

Asterios Raptis

Asterios Raptis is a Fullstack Developer and Software Consultant with over three decades of experience in the software development