Gradle:Part 3, Java project setup using gradle

Gradle:Part 3, Java project setup using gradle

In this part, we will set up a java project using Gradle. We will also be configuring IntelliJ, so we can run or debug a java project based on Gradle in IntelliJ.

This part is the continuation of Part 2, Installing Gradle on Local Machine. Have quick look before you start this part.

Set up Java Project using Gradle

Now, we already have Gradle installed on local machine. If not, have a quick look here Part 2: Installing Gradle on local Machine and do a quick installation. I am using IntelliJ idea IDE. Now let’s get started.

Let’s create a directory with name test-gradle

mkdir test-gradle
cd test-gradle/

Gradle needs to be initialised in this folder.This will setup the gradle structure inside the folder. It can be done using:

gradle init --type java-application

The following output is displayed:

Starting a Gradle Daemon, 2 incompatible and 1 stopped Daemons could not be reused, use --status for details

Select build script DSL:
  1: groovy
  2: kotlin
Enter selection (default: groovy) [1..2] 

Let’s choose groovy (option 1) and move forward. After that it will ask you about the testing-framework

Select test framework:
  1: junit
  2: testng
  3: spock
Enter selection (default: junit) [1..3] 

Let’s choose junit (option 1) for now.

Then it will ask you for Project and package name

Project name (default: test-gradle): 
Source package (default: test.gradle): 

You can change as you like or else default will be test-gradle. Press enter and you can see the following:

2 actionable tasks: 2 executed

So now lets open this folder in intellij or any IDE. On opening in IntelliJ, you will have a pop up as shown below. Enable auto-imports; you can also set custom gradle location as done in under screen shot

After pressing enter, the IntelliJ will open the project as in the screenshot below. Notice build.gradle file; we will be making changes in this file shortly.

Now open your terminal, change directory(cd) to current folder and then run

 gradle run

> Task :run
Hello world.

The output Hello world is written in class You can change that and run it. Now lets make some more changes so that we can run it by IntelliJ itself.

Configuring IntelliJ idea for Java project

Its pretty simple. Open your build.gradle file and add id 'idea' in the plugins block. So your plugin block will now appear like

plugins {
    // Apply the java plugin to add support for Java
    id 'java'

    // Apply the application plugin to add support for building an application
    id 'application'
    id 'idea'

Now open Edit configuration and add configuration as

Notice the run in tasks section. Run is a task, we will talk about tasks more in next section, this task is already provided by application plugin . You can check these things as

You can play more around these settings. Now press run button and you can see the output in result

So that’s it! Your IntelliJ is ready for Gradle projects. Now lets add some dependencies to the project

Adding dependencies using Gradle to your project

Let’s add google guice as a dependency to our project. Lets open build.gradle. As you can see in the repositories by default jcenter is the default repository; we can change it to maven, ivy, file as needed. Let’s change it to maven. For that, we need to add mavenCentral() to our repositories block. So our repository block will be like

repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.

Now let’s add dependency. We can simply do that by adding compile group: '', name: 'guice', version: '4.0' to the dependencies block. So our dependencies block will look like

dependencies {
    compile group: '', name: 'guice', version: '4.0'
    testImplementation 'junit:junit:4.12'

Now lets modify our main class to use guice dependencies like

We are calculating the time taken in execution. Now we can run it either by command line using the command gradle run or by ide that we just configured above.

So that’s it for this part; now we know how to add dependencies and set up java project using gradle and also how to set up IDE for Gradle project. In next section, Part 4, Creating executable fat jar using Gradle we will cover how to create executable fat jar using Gradle that can be deployed on production.

Share This Post