Saturday, November 9, 2013

Developing a Simple HelloWorld Java Program by using gradle


Step 1: Create the following package Structure.

Java
    └──src
        ├── main
        │   ├── java       
        │   └── resources
        └── test
            ├── java
            └── resources

Step2: Inside src/main/java create a HelloWorld.java with the fowlloing package.

HelloWorld.java

package com.ranga;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Now strutctue look likes

└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── ranga
    │   │           └── HelloWorld.java
    │   └── resources
    └── test
        ├── java
        └── resources

Step3: Inside Java folder, create a new file called build.gradle parellel to src. 

Java
├── build.gradle
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── ranga
    │   │           └── HelloWorld.java
    │   └── resources
    └── test
        ├── java
        └── resources 


Step4: Add the plugin as 'java' and specify the main the class in the build.gradle file.

build.gradle
 
apply plugin: 'java'

task runHelloWorld(dependsOn: 'classes', type: JavaExec) {
    main = 'com.ranga.HelloWorld'
    classpath = sourceSets.main.runtimeClasspath
}

defaultTasks 'runHelloWorld'

Step5: In terminal enter the following command to run the Simple HelloWorld program.

[ranga@ranga java]$ gradle -q runHelloWorld
Hello, Ranga!


Another way is,


edit the build.gradle and the following line

apply plugin: 'application'

mainClassName = "com.ranga.HelloWorld"


Then run the following command in the terminal

[ranga@ranga java1]$ gradle -q run
Hello, Ranga!


Thats it............

0 comments: