Gradle Getting Started: Creating a binary release version

Source: Internet
Author: User

After creating a useful application, we may want to share it with others. One way is to create a binary file that can be downloaded from a Web site.

This tutorial describes how to create a binary release version that meets the following requirements:

    • Binary releases must not use the so-called "fat jar" approach. In other words, all dependencies in our application must not be packaged in the same jar package as the program.

    • The binary publication must contain a boot copy for *nix and the Windows operating system.

    • The root directory of a binary publication must contain a license.

Here we go.

Create a binary publishing file

The application plugin is a Gradle plugin that allows us to run, install, and create binary release versions in a non-"fat jar" fashion.

Remember one of the examples we mentioned in the previous tutorial? Make some corresponding changes in its Build.gradle file so that it can be released in binary.

    1. Removes the configuration from the jar task.

    2. Apply the application plugin to the project.

    3. Configure the main class of the application to set the Mainclassname property.

After making the above changes in the Build.gradle file, the result is as follows (the relevant section has been highlighted):

12345678910111213 apply plugin: ‘application‘apply plugin: ‘java‘repositories {    mavenCentral()} dependencies {   compile ‘log4j:log4j:1.2.17‘   testCompile ‘junit:junit:4.11‘}mainClassName = ‘net.petrikainulainen.gradle.HelloWorld‘

The application plugin adds 5 tasks to the project:

    • The run task is used to start the application.

    • The startscripts task creates a startup script in the Build/scripts directory that is created for the Windows and *nix operating systems.

    • The Installapp task installs the application in the Build/install/[project name] directory.

    • The Distzip task is used to create a binary publication and package it as a zip file. Can be found under the Build/distributions directory.

    • The Disttar task is used to create a binary publication and package it as a tar file. Can be found under the Build/distributions directory.

We can create a binary file by running the following command under the project root: Gradle distzip or Gradle Disttar. Suppose we create a binary file packaged as a zip file, and the output is as follows:

1234567891011 > gradle distZip:compileJava:processResources:classes:jar:startScripts:distZipBUILD SUCCESSFUL Total time: 4.679secs

If the binaries created by the application plug-in are decompressed, the following directory structure can be obtained:

    • Bin directory: Includes startup scripts.

    • LIB directory: includes the application's jar file and its dependencies.

You can read the Gradle Application Plugin User Guide (chapter 45th) To learn more about application plugin information.

Now we can create a binary release that almost meets all the requirements. However, we still need to add the license for the application under the root directory of our binary release. I'll take a look at how to do that.

add an application license in the binary release versionWe can add an application license to a binary publication by following these steps:
    1. Create a task to copy the license from the project's root directory to the build directory.

    2. Add the license to the root of the binary publication you created.

Let's take a closer look at the details of these steps.

Copy the license file to the build directory

The license file contains the license information for our application and can be found in the root directory of the project.

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

    1. Create a new copy task, named Copylicense.

    2. Use the From () method in the Copyspec interface to configure the source file and call "LICENSE" as a parameter.

    3. Use the Into () method in the Copyspec interface to configure the target directory and call the $builddir property as a parameter.

After completing these steps, the Build.gradle file is as follows (the relevant section is highlighted):

123456789101112131415161718 apply plugin: ‘application‘apply plugin: ‘java‘repositories {    mavenCentral()}dependencies {    compile ‘log4j:log4j:1.2.17‘    testCompile ‘junit:junit:4.11‘}mainClassName = ‘net.petrikainulainen.gradle.HelloWorld‘task copyLicense(type: Copy) {    from "LICENSE"    into "$buildDir"}

Now we have created a task to copy the LICENSE file from the project's root directory to the build directory. However, when we run the command in the project root directory: Gradle Distzip, we see the following output:

1234567891011 > gradle distZip:compileJava:processResources:classes:jar:startScripts:distZipBUILD SUCCESSFUL Total time: 4.679secs

In other words, our new task has not yet been introduced. So, of course, no license is included in the binary release. Let's fix this problem.

to add a license file to a binary release file

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

1, change the Copylicense task from a Copy task to a normal gradle task by simply removing the "(type:copy)" string in its declaration.
2, follow the steps below to modify the Copylicense task

    • Configure the Copylicense task output. Create a new file object, point to the license file for the build directory, and set it to the Outputs.file property value.

    • Copy the license file from the project's root directory to the build directory.

3, the application plugin set a Copyspec property in the project named Applicationdistribution. We can use this property to add a license file to a binary file that has already been created, in the following steps:

    • Use the From () method in the Copyspec interface to configure the location of the license file, using the output of the Copylicense task as the method parameter.

    • Use the Into () method in the Copyspec interface to configure the target directory to invoke an empty string as a parameter.

After implementing these steps, the Build.gradle file is as follows (the relevant section is highlighted):

123456789101112131415161718192021222324252627 apply plugin: ‘application‘apply plugin: ‘java‘repositories {    mavenCentral()}dependencies {    compile ‘log4j:log4j:1.2.17‘    testCompile ‘junit:junit:4.11‘}mainClassName = ‘net.petrikainulainen.gradle.HelloWorld‘ task copyLicense {    outputs.file new File("$buildDir/LICENSE")    doLast {          copy {               from "LICENSE"               into "$buildDir"          }    }}applicationDistribution.from(copyLicense) {    into ""}

When you run the command Gradle Distzip under the project root, you see the following output:

123456789101112 > gradle distZip:copyLicense:compileJava:processResources:classes:jar:startScripts:distZipBUILD SUCCESSFUL Total time: 5.594secs

As you can see, the Copylicense task is now being introduced. We can extract the binary files and we can find the license files in the root directory.

Finally, we summarize what we have learned in this tutorial.

Summary

This tutorial teaches us three things:

    • Learn to use the application plugin to create a binary release.

    • Learn to copy a file from the source directory to the destination directory using the copy task.

    • Learn if you are adding files to a binary release file created by the application plug-in.

If you want to run the sample program in this tutorial, get it from GitHub.

Reprint Please specify: Android Development Chinese station»gradle Getting started: Creating a binary release version

Gradle Getting Started: Creating a binary release version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.