Code Monkey home page Code Monkey logo

javapackager's Introduction

JavaPackager

Maven Central GPL-3.0

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.

History

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.

How to use this plugin

Package your app with Maven

Add the following plugin tag to your pom.xml:

<plugin>
	<groupId>io.github.fvarrui</groupId>
	<artifactId>javapackager</artifactId>
	<version>1.5.1</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

Package your app with Gradle

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.5.1'
	}
}

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

Generated artifacts

By default it will generate next artifacts in ${outputDirectory} folder:

Artifact Description
${name} Directory with the native application and other needed assets.
${name}-${version}-runnable.jar Runnable JAR file.
${name}_${version}.deb DEB package file if it's executed on GNU/Linux (requires dpkg-deb).
${name}_${version}.rpm RPM package file if it's executed on GNU/Linux (requires rpmbuild).
${name}_${version}.exe Setup file if it's executed on Windows (requires Inno Setup).
${name}_${version}.msi MSI installer file if it's executed on Windows (requires WiX Toolset).
${name}_${version}.msm MSI merge module file if it's executed on Windows (requires WiX Toolset).
${name}_${version}.dmg Disk image file if it's executed on Mac OS X (requires hdiutil).
${name}_${version}.pkg PKG installer file if it's executed on Mac OS X (requires pkgbuild)
${name}-${version}-${platform}.zip Zipball containing generated directory ${name}.
${name}-${version}-${platform}.tar.gz Compressed tarball containing generated directory ${name}.

โš ๏ธ Installers generation will be ommited if target platform is different from current platform (see platform property).

โš ๏ธ DEB and RPM package generation in Gradle is not yet available. Coming soon!

Plugin configuration properties

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.
generateInstaller โŒ true Generates an installer for the app.
iconFile โŒ Path to the app icon file (PNG, XPM, ICO or ICNS). โš ๏ธ Deprecated (see platform specific properties).
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 โŒ [] Defines 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).
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.
url โŒ App website URL.
useResourcesAsWorkingDir โŒ true Uses app resources folder as default working directory (always true on Mac OS).
version โŒ ${project.version} Project version.
vmArgs โŒ [] Adds VM arguments.

Some default values depends on the used building tool.

Platform specific properties

Property Mandatory Default Description
linuxConfig โŒ GNU/Linux specific properties.
macConfig โŒ Mac OS X specific properties.
winConfig โŒ Windows specific properties.

โš ๏ธ Be careful when using the platform 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 using classifiers. Also, intallers generation will be ommited.

Plugin assets

Some assets, such as application icons and Velocity templates, could be placed in ${assetsDir} folder organized by platform.

${assetsDir}/
โ”œโ”€โ”€ linux/
โ”œโ”€โ”€ mac/
โ””โ”€โ”€ windows/

Icons

If icons are located in ${assetsDir} folders, it would not be necessary to specify the iconFile property:

${assetsDir}/
โ”œโ”€โ”€ linux/
โ”‚ย ย  โ”œโ”€โ”€ ${name}.png     # on GNU/Linux it has to be a PNG file for DEB package
โ”‚ย ย  โ””โ”€โ”€ ${name}.xpm     # and XPM file for RPM package
โ”œโ”€โ”€ 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 iconFile plugin property is not specified and it can't find the correct icon in ${assetsDir} folder, it will use an icon by default for all platforms.

Velocity templates

Velocity templates (.vtl files) are used to generate some artifacts which have to be bundled with the app.

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
โ”‚ย ย  โ””โ”€โ”€ 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
    โ”œโ”€โ”€ iss.vtl                        # Inno Setup Script template
    โ”œโ”€โ”€ msm.wxs.vtl                    # WiX Toolset WXS template to generate Merge Module
 ย ย  โ””โ”€โ”€ wxs.vtl                        # WiX Toolset WXS template to generate MSI

Use default templates as examples.

An object called info of type PackagerSettings is passed to all templates with all plugin properties.

Additional JVM options at runtime

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

How to build and install the plugin

Useful to try SNAPSHOT versions.

Execute next commands in BASH (GNU/Linux or macOS) or CMD (Windows):

  1. Download source code and change to the project directory:
git clone https://github.com/fvarrui/JavaPackager.git
cd JavaPackager
  1. Compile, package and install the plugin in your local repository (ommit ./ on Windows):
./gradlew publishToMavenLocal

How to release the plugin to Maven Central

Run next command (ommit ./ on Windows):

./gradlew -Prelease uploadArchives closeAndReleaseRepository

Related guide.

How to release the plugin to Gradle plugin portal

Only the first time, run next command:

./gradlew login

And then, run (ommit ./ on Windows):

./gradlew publishPlugins

Related guide.

Future features

Check the TO-DO list to know the features we plan to add to JavaPackager.

javapackager's People

Contributors

airlenet avatar cristiangreco avatar dexman545 avatar fvarrui avatar gradle-update-robot avatar mlopezfc avatar ogerardin avatar pyeroh avatar ricardovargasleslie avatar swissbuechi avatar treimers avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.