The installApp task installs the application into the build/install/[project name] directory.

The distZip task creates the binary distribution and packages it into a zip file that is found from the build/distributions directory.

The distTar task creates the binary distribution and packages it into a tar file that is found from the build/distributions directory.

We can create a binary distribution by running one of the following commands in the root directory of our project: gradle distZip or gradle distTar. If we create a binary distribution that is packaged to a zip file, see the following output:

We can now create a binary distribution that fulfils almost all of our requirements. However, we still need to add the license of our application to the root directory of our binary distribution. Let’s move on and find out how we can do it.

Adding the License File of Our Application to the Binary Distribution

We can add the license of our application to our binary distribution by following these steps:

Create a task that copies the license file from the root directory of our project to the build directory.

Add the license file to the root directory of the created binary distribution.

Let’s move on and take a closer look at these steps.

Copying the License File to the Build Directory

The name of the file that contains the license of our application is LICENSE, and it is found from the root directory of our project.

We can copy the license file to the build directory by following these steps:

We have now created a task that copies the LICENSE file from the root directory of our project to the build directory. However, when we run the command gradle distZip in the root directory of our project, we see the following output:

In other words, our new task is not invoked and this naturally means that the license file is not included in our binary distribution. Let’s fix this problem.

Adding the License File to the Binary Distribution

We can add the license file to the created binary distribution by following these steps:

Transform the copyLicense task from a Copy task to a “regular” Gradle task by removing the string ‘(type: Copy)’ from its declaration.

Modify the implementation of the copyLicense task by following these steps:

Configure the output of the copyLicense task. Create a new File object that points to the license file found from the build directory and set it as the value of the outputs.file property.

Copy the license file from the root directory of our project to the build directory.

The application plugin sets a CopySpec property called the applicationDistribution to our project. We can use it to include the license file to the created binary distribution. We can do this by following these steps:

Configure the location of the license file by using the from() method of the CopySpec interface and pass the output of the copyLicense task as method parameter.

Configure the target directory by using the into() method of the CopySpec interface and pass an empty String as a method parameter.

After we have followed these steps, our build.gradle file looks as follows (the relevant part is highlighted):

I “know” that task copyLicense{} copies the file but it would be good to know why the outputs.file property and applicationDistribution.from() method are implemented. I know that this is a beginners guide but I think that adding a sentence or two explaining this wouldn’t hurt.