# Chapter 42. The Application Plugin

The Gradle application plugin extends the language plugins with common application related tasks. It allows running and bundling applications for the jvm.

## 42.1. Usage

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

Example 42.1. Using the application plugin

`build.gradle`

    apply plugin:'application'

To define the main-class for the application you have to set the `mainClassName` property as shown below

Example 42.2. Configure the application main class

`build.gradle`

    mainClassName = "org.gradle.sample.Main"

Then, you can run the application by running `gradle run`. Gradle will take care of building the application classes, along with their runtime dependencies, and starting the application with the correct classpath.

The plugin can also build a distribution for your application. The distribution will package up the runtime dependencies of the application along with some OS specific start scripts. All files stored in `src/dist` will be added to the root of the distribution. You can run `gradle installApp` to create an image of the application in `build/install/`projectName``. You can run `gradle distZip` to create a ZIP containing the distribution.

## 42.2. Tasks

The Application plugin adds the following tasks to the project.

Table 42.1. Application plugin - tasks

Task name | Depends on | Type | Description
`run` | `classes` | `JavaExec` | Starts the application.
`startScripts` | `jar` | `CreateStartScripts` | Creates OS specific scripts to run the project as a JVM application.
`installApp` | `jar`, `startScripts` | `Sync` | Installs the application into a specified directory.
`distZip` | `jar`, `startScripts` | `Zip` | Creates a full distribution ZIP archive including runtime libraries and OS specific scripts.

## 42.3. Convention properties

The application plugin adds some properties to the project, which you can use to configure its behaviour. See `Project`.

## 42.4. Including other resources in the distribution

One of the convention properties added by the plugin is `applicationDistribution` which is a `CopySpec`. This specification is used by the `installApp` and `distZip` tasks as the specification of what is to be include in the distribution. Above copying the starting scripts to the `bin` dir and necessary jars to `lib` in the distribution, all of the files from the `src/dist` directory are also copied. To include any static files in the distribution, simply arrange them in the `src/dist` directory.

If your project generates files to be included in the distribution, e.g. documentation, you can add these files to the distribution by adding to the `applicationDistribution` copy spec.

Example 42.3. Include output from other tasks in the application distribution

`build.gradle`

    task createDocs {
        def docs = file("$buildDir/docs")
        outputs.dir docs
        doLast {
            docs.mkdirs()
            new File(docs, "readme.txt").write("Read me!")
        }
    }

    applicationDistribution.from(createDocs) {
        into "docs"
    }

By specifying that the distribution should include the task's output files (see Section 15.8.1, “Declaring a task's inputs and outputs”), 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 42.4. Automatically creating files for distribution

Output of `gradle distZip`

    > gradle distZip
    :createDocs
    :compileJava
    :processResources UP-TO-DATE
    :classes
    :jar
    :startScripts
    :distZip

    BUILD SUCCESSFUL

    Total time: 1 secs