Gradle configurations for running your Java program using NetBeans 8

What is Gradle?

Gradle is a project automation tool that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the more traditional XML form of declaring the project configuration.” (Wikipedia)

Setting up Gradle project on NetBeans

Go to ‘Files’

Select Gradle from Categories, then select ‘Single Gradle Project’

Provide project name and click on finish

Project skeleton

Project_Folder/build/ – Holds the compiled class and jars

Project_Folder/build/jar – Will hold the jar file of your project

Project_Folder/src/  – Sources and resources goes here

Project_Folder/src/main – Your source codes that’ll be ran from jar

Project_Folder/src/main/java – main source code

Project_Folder/src/main/resources – resources accessed by main source code

Project_Folder/src/test/java – unit tests code (eg. JUnit)

Project_Folder/src/test/resource – unit test code accessed resources

build.gradle – Gradle configurations goes here (This is where most gradle configs go)

settings.gradle – Holds project name and some other stuff

build.gradle Configurations

1. External dependency resolution

Most of the time we will have some external jar libraries that we’ll need import in our code. We tell gradle about them here. There are two ways we can access them 1) From a maven repository 2) from local drive.

a. From maven repository

Sample code : 

dependencies {
compile ‘org.json:json:20140107’
compile ‘net.sf.opencsv:opencsv:2.3’
compile ‘net.sf.supercsv:super-csv:2.2.0’


This tells gradle to download the external jars from a maven repository, store them in a predefined folder set by the IDE and use them for our code. The syntax is

compile ‘group:name:version’

For example, if we want to add the org.jason library to our project we go to Type in ‘org.json’ in the search box and it will give us the link to the repo. Then we chose our selected version of the jar and click on the gradle tab. Which will give us the code that we add in the above script. 


Fig. picture of the org.json repo page

b. From a local directory

Sample script:

dependencies {
 compile fileTree(dir: ‘lib’, include: ‘**/*.jar’)


This will access the jar files from Project_Folder/lib  directory. 

2. Runtime inclusion of external libraries

Even though we’ll able to build the project using the build script so far. But we need to embed the external jars inside our project jar to make it run. We need to use the following snippet for that – 

jar {
    from {
       configurations.compile.collect {
           it.isDirectory() ? it : zipTree(it)
       configurations.runtime.collect {
           it.isDirectory() ? it : zipTree(it)


This will unzip the external jars and include them inside the project jar. 

3. Unsuppressing console output

Gradle will suppress the System.out and System.err functions which is a hassle during bug hunt. To enable add the following snippet to the build script – 

test {
//makes the standard streams (err and out) visible at console when running tests
testLogging.showStandardStreams = true