Enterprise Java

How to install Gradle

Gradle is a simple and yet strong build tool. It is similar to the Ant build tool. It manages the build well and also handles build dependencies. The best part of Gradle is that it is open source project. If you are thinking about installing and giving it a try, then you are at the right place. Gradle development cycle is of 4 weeks, so after every four weeks they roll out a new version of Gradle. Here, I am assuming that you are going to install Gradle on linux/Ubuntu machine.

Gradle setup steps

  1. Download Gradle from here. Gradle download comes in three different flavors.
    • Binaries, Documentation and Source code
    • Binaries only
    • Source code only

    The first one is recommended since it comes with documentation and source code. If you are not interested in documentation and source file, you can download Binaries only package. 

  2.  unzip the download file
      $unzip gradle-[version]-[type].zip
    

    here type can be all, bin, or src. It is based on type of downloaded flavor.

  3. The bin file should be in the system path, only then you can execute the Gradle command. It can be done either by executing the command given below or by editing the .bashrc to set the PATH variable.
      #modify the directory path according to the setup.
      $ export PATH=$PATH:/gradle-directory-path/gradle-directory/bin
    
  4. Now execute the following command on terminal
     $ gradle     #you will see the output below.
    
     :help
    
    Welcome to Gradle 1.1.
    
    To run a build, run gradle  ...
    To see a list of available tasks, run gradle tasks
    To see a list of command-line options, run gradle --help
    BUILD SUCCESSFUL
    Total time: 2.607 secs
    

    If you don’t the the above print, then you need to recheck the PATH variable.

Hello wold!! in Gradle

After installing Gradle, let’s try a simple Gradle file. Create a file named build.gradle and copy the code given below in this file.

task("hello"){
  println "Hello world!!"
}

The way we define targets in Makefile, similarly we define tasks in gradle script. In the above code we have created a simple task called hello which prints Hello world!!.
For executing this script, just execute the command below on the terminal in the same directory.

$ gradle hello   #you will get similar out put as shown below.

Hello world!!
:hello UP-TO-DATE

BUILD SUCCESSFUL

Gradle command looks for build.gradle file in the current directory and executes the specified task(s) similar to make command which looks for Makefile in the current directory and executes the specified target(s).

Reference: how to install gradle from our JCG partner Rakesh Cusat at the Code4Reference blog.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
MintMan
MintMan
10 years ago

Thanks for this, I was so confused about something so basic. Very clear, and worked right away!

Raul Sena Ferreira
Raul Sena Ferreira
8 years ago

Thanks for the post, simple and straightforward

Back to top button