The Application plugin facilitates creating an executable JVM application. It makes it easy to start the application locally during development, and to package the application as a TAR and/or ZIP including operating system specific start scripts.

Applying the Application plugin also implicitly applies the Java plugin. The main source set is effectively the “application”.

Applying the Application plugin also implicitly applies the Distribution plugin. A main distribution is created that packages up the application, including code dependencies and generated start scripts.

Usage

To use the application plugin, include the following in your build script:

Example 1. Using the application plugin
GroovyKotlin
build.gradle
plugins {
    id 'application'
}

The only mandatory configuration for the plugin is the specification of the main class (i.e. entry point) of the application.

Example 2. Configure the application main class
GroovyKotlin
build.gradle
application {
    mainClassName = 'org.gradle.sample.Main'
}

You can run the application by executing the run task (type: JavaExec). This will compile the main source set, and launch a new JVM with its classes (along with all runtime dependencies) as the classpath and using the specified main class. You can launch the application in debug mode with gradle run --debug-jvm (see JavaExec.setDebug(boolean)).

Since Gradle 4.9, the command line arguments can be passed with --args. For example, if you want to launch the application with command line arguments foo --bar, you can use gradle run --args="foo --bar" (see JavaExec.setArgsString(java.lang.String).

If your application requires a specific set of JVM settings or system properties, you can configure the applicationDefaultJvmArgs property. These JVM arguments are applied to the run task and also considered in the generated start scripts of your distribution.

Example 3. Configure default JVM settings
GroovyKotlin
build.gradle
application {
    applicationDefaultJvmArgs = ['-Dgreeting.language=en']
}

If your application’s start scripts should be in a different directory than bin, you can configure the executableDir property.

Example 4. Configure custom directory for start scripts
GroovyKotlin
build.gradle
application {
    executableDir = 'custom_bin_dir'
}

The distribution

A distribution of the application can be created, by way of the Distribution plugin (which is automatically applied). A main distribution is created with the following content:

Table 1. Distribution content
Location Content

(root dir)

src/dist

lib

All runtime dependencies and main source set class files.

bin

Start scripts (generated by createStartScripts task).

Static files to be added to the distribution can be simply added to src/dist. More advanced customization can be done by configuring the CopySpec exposed by the main distribution.

Example 5. Include output from other tasks in the application distribution
GroovyKotlin
build.gradle
task createDocs {
    def docs = file("$buildDir/docs")
    outputs.dir docs
    doLast {
        docs.mkdirs()
        new File(docs, 'readme.txt').write('Read me!')
    }
}

distributions {
    main {
        contents {
            from(createDocs) {
                into 'docs'
            }
        }
    }
}

By specifying that the distribution should include the task’s output files (see more about tasks), Gradle knows that the task that produces the files must be invoked before the distribution can be assembled and will take care of this for you.

Example: Automatically creating files for distribution

Output of gradle distZip
> gradle distZip
> Task :createDocs
> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :jar
> Task :startScripts
> Task :distZip

BUILD SUCCESSFUL in 0s
5 actionable tasks: 5 executed

You can run gradle installDist to create an image of the application in build/install/projectName. You can run gradle distZip to create a ZIP containing the distribution, gradle distTar to create an application TAR or gradle assemble to build both.

Customizing start script generation

The application plugin can generate Unix (suitable for Linux, macOS etc.) and Windows start scripts out of the box. The start scripts launch a JVM with the specified settings defined as part of the original build and runtime environment (e.g. JAVA_OPTS env var). The default script templates are based on the same scripts used to launch Gradle itself, that ship as part of a Gradle distribution.

The start scripts are completely customizable. Please refer to the documentation of CreateStartScripts for more details and customization examples.

Tasks

The Application plugin adds the following tasks to the project.

runJavaExec

Depends on: classes

Starts the application.

startScriptsCreateStartScripts

Depends on: jar

Creates OS specific scripts to run the project as a JVM application.

installDistSync

Depends on: jar, startScripts

Installs the application into a specified directory.

distZipZip

Depends on: jar, startScripts

Creates a full distribution ZIP archive including runtime libraries and OS specific scripts.

distTarTar

Depends on: jar, startScripts

Creates a full distribution TAR archive including runtime libraries and OS specific scripts.

Application extension

The Application Plugin adds an extension to the project, which you can use to configure its behavior. See the JavaApplication DSL documentation for more information on the properties available on the extension.

You can configure the extension via the application {} block shown earlier, for example using the following in your build script:

GroovyKotlin
build.gradle
application {
    executableDir = 'custom_bin_dir'
}

Licensing

The Gradle start scripts that are bundled with your application are licensed under the Apache 2.0 Software License. This does not affect your application, which you can license as you choose.

Convention properties (deprecated)

This plugin also adds some convention properties to the project, which you can use to configure its behavior. These are deprecated and superseded by the extension described above. See the Project DSL documentation for information on them.

Unlike the extension properties, these properties appear as top-level project properties in the build script. For example, to change the application name you can just add the following to your build script:

GroovyKotlin
build.gradle
applicationName = 'my-app'