Quantcast
Channel: CodingLoading » Chandan Sharma
Viewing all articles
Browse latest Browse all 10

Installation and Basics of Gradle build

$
0
0

Today we will learn Gradle basic and installation, along with creating a sample gradle build.

Installing gradle:

Gradle requires java installed in your system.
It requires groovy also, but gradle bundle already has groovy and gradle ignore the existing groovy path if you already have groovy installed.

Step 1. Download the latest version(I am using gradle 2.7) from gradle official site. http://gradle.org/gradle-download/
Step 2. extract it in a specified directory. lets say (d:/tools)
Step 3. create a environment variable GRADLE_HOME with d:/tools/gradle-2.7 and add GRADLE_HOME/bin to your PATH environment variable.
Step 4. And test your  installation with the command prompt with “gradle -v” command . if you get the version no  and below output your gradle is properly installed, and you are ready to build a project.

gradle-test

To create the project we need to understand the basic features of gradle build. we understood gradle build tool from my last post gradle has DSL based build script. gradle use groovy to write the build script. here we will understand the technical details to build a sample project.

The basic terms used in gradle is Project and Task:

Project: Project is nothing but an application, jar or web application etc. gradle can build single or multiple project in a single build. we can configure multiple projects and multiple language in properties and configuration files.
Task: The Project may have one or multiple task to do. Task represents an atomic piece of work which build performs.

There are some important files we need to understand to make a successful build.
1. build.gradle file is the main build script where we write the build tasks. You start a build using the “gradle” command, when we run the “gradle” command then gradle looks for this file in the current directory.
2. The gradle.properties file is used to configure the properties of the build.
3. The Gradle Settings file (settings.gradle) is used when Gradle build has more than one projects.

Building the project

In this article we will use just build.gradle file because here we will understand the basic gradle project and task. in the next example we will build a java application with gradle.

The directory structure for this project is nothing but a project folder where we will keep build.gradle file.
Step 1. create a project folder (I am using “D:\blog\gradle_app”)
Step 2. create a build.gradle file in the project folder.
Step 3. Add the below task code in build.gradle. that we will execute.

Note: write now we will not create any source file to compile. we will write the excutable code in build.gradle itself in groovy language and make it run.


task hello {
    doLast {
       println 'Hello world!'
   }
}

or in shortcut task:


task hello << {
   println 'Hello world!'
}

Step 4. Open the command prompt and Go the project folder and run the below command.


>gradle -q hello

gradle-execute

Then you will get the output “Hello world!”


You can write any logic in groovy language in the task. you can write multiple task, set the priority of the task, task dependency, write the method, use the Ant task. For more information go to the link https://docs.gradle.org/current/userguide/tutorial_using_tasks.html

Have a Nice Day!!!

The post Installation and Basics of Gradle build appeared first on CodingLoading.


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images