26 January 2014

Gradle installation and example / tutorial for Linux (Fedora 20)

When I wanted to re-learn Ant, I found Maven. When I found Maven, I found out about Gradle being better than Maven. And indeed it did seem to be so.
The Gradle build script appeared a lot shorter and more powerful than Maven. There is also a more feature-rich C++ version for Gradle coming up and a demo and cpp plugin already available.

This is intended to be a simple tutorial for Gradle. Recording my path in learning the what and why of Gradle.

To give it a shot, download the Gradle binaries, extract them into /usr/local/ with sudo unzip gradle-1.10-bin.zip.

To run Gradle, you have to add the path of the Gradle bin folder to PATH. So do this:

sudo vim ~/.bash_profile

Add the path this way in .bash_profile


Save, exit and type source ~/.bash_profile to incorporate the changes immediately.

Type gradle -v and you'll see the Gradle version, build time, build number etc. Congratulations.

To try out Gradle, create a file named build.gradle file in any directory you like and as the Gradle documentation says, paste the following content in it:

task hello {
    doLast {
        println 'Hello world!'

Save, exit and then type gradle hello for the script to execute (gradle.build isn't exactly a build script. It's a build configuration script; which means that it's a script we write, which internally creates the necessary build script automatically. Saves us a lot of trouble, really!)

Now that you know Gradle works, you have to understand that Gradle thinks in terms of high-level tasks. You have to learn the syntax of writing tasks.

Type gradle tasks at the terminal (in some other directory which does not have a build.gradle file in it) to see the list of tasks that gradle supports by default. You'll see something like this:

All tasks runnable from root project

Build Setup tasks
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

Help tasks
dependencies - Displays all dependencies declared in root project 'navin'.
dependencyInsight - Displays the insight into a specific dependency in root project 'navin'.
help - Displays a help message
projects - Displays the sub-projects of root project 'navin'.
properties - Displays the properties of root project 'navin'.
tasks - Displays the tasks runnable from root project 'navin'.

To see all tasks and more detail, run with --all.


Total time: 3.327 secs
This means that similar to how you had defined a "hello" task above, Gradle already has such tasks named dependencies, help, properties etc., already defined somewhere. To see proof of it, type gradle help and see the result. These tasks are already part of Gradle by default. That's why you didn't need a build.gradle file.

Now go back to the directory where you created the build.gradle file. Now type gradle tasks and you'll see a similar output as above, with the additional display of "hello" as a task. Which means that in this directory if you type gradle hello, the hello task will be executed.

Gradle encourages convention over configuration, so it'll be easier for us to create a new project with the following directory structure:

src/main/java  for production Java source
src/main/resources for production resources
src/test/java for test Java source
src/test/resources for test resources
src/sourceSet/java for java source for the given source set
src/sourceSet/resources for resources for the given source set

Create a simple java class inside src/main/java, named Main.java with the following contents.
public class Main
    public static void main(String[] args)
        System.out.println("in main");
 }//Main class

Go back to the folder where your build.gradle file is. Now to be able to build a Java project, you haven't created any tasks. Thankfully, Gradle comes with a plugin which has the necessary tasks for building a Java project, predefined. Open your build.gradle file and add this line as the fist line of the file: 

apply plugin: 'java'

Save and exit and type gradle tasks. Now you'll see a lot more tasks listed. Running the gradle build command will automatically find your Main.java file, create a build directory for it, place the ".class" file there and will also create a jar file for you in build/libs.

Now if you try to run the jar file with java -jar jarname.jar, it won't execute because you didn't specify the application entry point in manifest.mf.

So now open your build.gradle file and add the following info to it:

apply plugin: 'java'

jar {
    manifest {
            "Implementation-Title": "Tutorial",
            "Implementation-Version": 1.0,
            "Main-Class": "Main"
Now executing the gradle build command will create the manifest file and you'll be able to execute the jar. Similar to make files in C and C++, you can run a clean command with gradle clean and the build directory will get cleared.

Gradle can  automatically create Eclipse project files for you. Just add to your build.gradle, the following line 
apply plugin: 'eclipse'
Type gradle tasks and see a new set of tasks you've got.

Now type gradle eclipse and when you type ls -altr you'll see the Eclipse project files have been generated. Open Eclipse, go to File > Import > General > Existing projects into workspace and you'll be able to use this newly created Eclipse project.

No comments: