Gradle Application Plugin: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 65: Line 65:
   gradle tasks --all
   gradle tasks --all


The "startupScripts" task is a [https://docs.gradle.org/4.7/javadoc/org/gradle/api/tasks/application/CreateStartScripts.html CreateStartScripts] type task.
The "startupScripts" task is a [https://docs.gradle.org/4.7/javadoc/org/gradle/api/tasks/application/CreateStartScripts.html CreateStartScripts] type task. The actual script generation is implemented in [https://docs.gradle.org/4.7/javadoc/org/gradle/jvm/application/scripts/ScriptGenerator.html ScriptGenerator] instances, returned by <tt>CreateStartScripts.getUnixStartScriptGenerator()</tt> and <tt>CreateStartScripts.getWindowsStartScriptGenerator()</tt>.


==Generate Multiple Start Scripts==
==Generate Multiple Start Scripts==

Revision as of 22:05, 23 July 2018

External

Internal

Overview

The application plugin facilitates creating an executable JVM application. It bundles all application JARS, transitive dependencies and operating system specific scripts into a TAR or ZIP file, and it also generates the bash and Windows wrapper scripts to run the application with. The plugin also allows executing the application from the project's work area with the "run" task.

Typical Setup

This setup describes configuration of a multi-project build, whose sub-project create various components of the application and the top level project bundles all those in a distributable artifact. The dependencies between the sub-projects must be configured in the corresponding build.gradle files. To build the application artifact, we assume that there's a sub-project that contains the "main()" static method that triggers the execution, that method exists in "io.example.Main" class, and the sub-project in question is named "main".

As such, the top-level project build.gradle should contain the following relevant configuration:

...

apply plugin: 'application'

mainClassName = "io.example.Main"

dependencies {

    ...

    implementation project(':main')
}

Provided that the rest of the dependencies between sub-projects are correctly declared in the corresponding build.gradle files, the execution of

gradle distZip

will compile everything and will create a ./build/distributions/<rootProject.name>.zip file. The file includes the sub-project artifacts, their transitive dependencies, and wrapper scripts for bash and Windows.

For more details about rootProject.name, see:

rootProject

Properties

mainClassName

mainClassName = 'greeter.Greeter'

Executing the Application from Project Work Area

gradle run

However, if the application requires command line parameters, they are not propagated. TODO: find a solution. A temporary solution is to unzip the application ZIP in a temp directory and then repeatedly overwrite the relevant JARs

Start Script Customization

This section explains the mechanics behind creating the application start (wrapper) script and shows how the process can be customized: use a different template, create multiple scripts, etc.

Start Script Mechanics

The Application plugin generates by default Unix and Windows start scripts that are capable launching a configured "main" class in an environment where the classpath is correctly set. The default script templates are based on the same scripts used to launch Gradle, which ship as part of the Gradle distribution. The start scripts are completely customizable though. The Application plugin adds a task named "startScripts" to the project's task list. The task can be view with:

 gradle tasks --all

The "startupScripts" task is a CreateStartScripts type task. The actual script generation is implemented in ScriptGenerator instances, returned by CreateStartScripts.getUnixStartScriptGenerator() and CreateStartScripts.getWindowsStartScriptGenerator().

Generate Multiple Start Scripts

Configure the Start Script Template

Configure Start Script Paramenters