Design a site like this with WordPress.com
Get started

Publish Build Scans

You can publish your build and details that will provide with you with the insights of what happened when you ran the build. The build are published to a centralized server.

How to publish

When you run the gradle build, pass the parameter –scan with the build. This will deploy the build to the centralized server.

When requested, accept the terms by typing “yes”

The build will also display a url where you can see details as below of your build. Follow the instructions to view details

Advertisement

Gradle – Task

A Gradle build is made up of tasks. Each task represents a set of instructions to gradle for execution.

A task might consist of compiling classes, copying files from source derectory to destination.

A simple task is defined below.

task hello {
   doLast {
      println 'helloworld'
   }
}

put the above code in a build.gradle file and execute gradle hello, it should print helloworld.

The “doLast” keyword tells the gradle to execute the statements within doLast at the last of the task execution. Similarly there is “doFirst“. As the name suggest, the doFirst will execute before any other sub task is executed within a task.

if there are multiple doLast, the last doLast will be executed in then end and then the prior and so on.

if there are multiple doFirst, the last doFirst will execute first and then prior one and so on.

You can also view the task you created by running gradle task –all under “Other Tasks“.

Task dependencies

It is possible to add dependencies in a task. so a task B might be dependent on the execution of task A. To do this, you have to add set the dependsOn attribute of a task

e.g.

task taskA {
   println 'taskA'
}
task taskB(dependsOn: 'taskA') {
   println "taskB"
}

The above build.gradle will always execute taskA before taskB as we have mentioned the dependency in the build file. So if you run the task taskB (gradle taskB) it will execute taskA first and then taskB

Java Gradle Dependency management

One of the key feature of the build is managing the dependencies in the project. If we have to use a third party library, we declare that library in the build file i.e. build.gradle and then expect the build tool to download the library and put in the classpath of our project for consumption.

Let say we want to use google guava in our project, so we add the below dependency in the build.gradle file.

compile group: 'com.google.guava', name: 'guava', version: '29.0-jre'

The above line will put the guava library in our project classpath and make the library available for us to use.

Dependency scope basically tells the gradle tool of our intention of when we want the library to be available. Lets go through one – by one.

  • Implementation
    It is used to declare dependencies that we don’t want to expose to consumers compile time. So it means that if we do not want the dependencies in our project to be exposed to the consumers of our project, we declare the dependency as implementation. The is the default scope and is an improvement over the compile scope. The compile scope is deprecated and will no longer be available in latest build scrips. So this scope avoids pollution of the transitive dependencies which will happen otherwise with compile scope.

  • api
    Use the api configuration do declare dependencies that are part of our API, i.e. for dependencies that we explicitly want to expose to our consumers. So if you want the dependency to be the part of the api for the consumers, then you declare the dependency as api.

  • compileOnly
    As the name suggests the dependencies which are supposed to be available only at compile time, such dependencies are marked compileOnly. E.g. of such a dependency is lombok. The lombok library is not required at runtime as it creates the required class files at compile time based on the annotations.

  • runtimeOnly
    Dependencies available only at the runtime are declared runtime dependencies.

testImplementation / testCompileOnly / testRuntimeOnly
testImplementation dependencies are only available during compilation and runtime of tests.

testCompileOnly dependencies are only available during compilation of tests and not at runtime.

testRuntimeOnly dependencies are only available during runtime of tests and not at compile time.

Create a new Gradle Project – Intellij

Pre-requisite

  1. IntelliJ
  2. Gradle enabled in intelliJ

Steps

  1. Open Intellij
  2. Navigate to File -> New Project and it will show below window
  3. Select Gradle project as shown below, select Java installation location if not already set and click on next.

  4. As shown below, provide project name, location, group id and artifact id and click on finish
  5. This should create your new Gradle project. The build.gradle will look below.
  6. Next will create a class and run the project to display Hello World. It is pretty much self explanatory from here on. The gradle project creation is completed at the 5th step already. The below steps are just to try and create a new class and make it run.

    Create new class


    print hello world



    Right Click on the HelloWorld class and select run to run the program.


How to install Gradle – 6 easy steps

  1. Install Java – version 1.8 and above if not already installed.
    To verify, open command prompt and type java -version

  2. Download the binary version of the latest gradle distribution from the link https://gradle.org/install/
  3. Create a new directory at C:\Gradle and unzip the content into the directory

  4. Add a new environmental variable “Gradle” and point the variable to C:\Gradle\bin

  5. Append the Gradle variable to the path environmental variable as shown below

  6. Gradle setup is done. Verify by opening a new command window and type Gradle -v