Gradle Interview Questions And Answers

gradle interview questions and answers   Top 15 Gradle Interview Questions And Answers

1) What is Gradle?

Gradle is an open source build automation system. It helps teams build, automate and deliver better software, faster. Gradle came into picture in 2012 and has some efficient features from both Apache Maven and Apache Ant build tools.


2) What are the key features of the Gradle?

Following are the list of key features that Gradle provides:

● Gradle provides a declarative language elements. Those elements also provides build-by-convention support for Java, Groovy, OSGI, Web and Scala.
● With Gradle one can apply common design principles to the build. It gives a perfect structure for build, so that it can design well-structured and easily maintained, comprehensible build.
● Gradle has deep API which enables to monitor and customize the configuration and execution behavior to its core.
● Gradle can easily scale from simple and single project builds to huge enterprise multi-project builds.
● Gradle supports multi-project builds.
● It provides different ways to manage the builds.
● Gradle is the first build integration tool as it fully supports ANT tasks, Maven and lvy repository infrastructure for publishing and retrieving dependencies. Also it provides a converter for turning a Maven pom.xml to Gradle script.
● Gradle can easily adapt to any structure.
● Gradle Wrapper enables execution of Gradle builds on machines where Gradle is not installed. This is useful for continuous integration of servers.
● Gradle is an open source project, and licensed under the Apache Software License (ASL).
● Gradle's build script are written in Groovy.


3) Why Groovy was chosen for Gradle?

The complete Gradle API is designed using Groovy language. Gradle is general purpose build tool at its core and its main focus is Java projects. Groovy was chosen because it offers by far the greatest transparency for people using Java. The base syntax of Groovy is same as Java.


4) Each Gradle build script is encoded using which format?

Each Gradle build script is encoded using UTF-8, saved offline and named as build.gradle.


5) How to run Gradle build using the Gradle command?

Every Gradle build represents one or more projects. Each project has different tasks. Gradle command looks for file build.gradle. Below is the sample build.gradle file:

task hey {
   doLast {
      println 'Site is testfry.com'
   }
}

Below is the command to access this build.gradle file:

C:\> gradle -q hey

Output:

Site is testfry.com


6) What are the different type of objects that Gradle uses?

Following are the real objects that Gradle uses:
● Project Object
● Script Object


7) What are the Project Objects that Gradle uses?

Each Gradle script describes one or multiple projects.In the execution Gradle scripts configures the Project Object.


8) What are the Script Objects that Gradle uses?

Gradle puts script code into classes which implements Script Interface and executes. It means that all the properties and methods declared by the script interface are available for script.


9) What will be the output of below Gradle code snippet?
task count << {
   4.times { 
      print "$it " 
   }
}

The output of the given Gradle program is..

0 1 2 3


10) In Gradle how to accesses appropriate getter and setter methods of a particular property by specifying its reference?

The following Gradle snippet defines the syntaxes of getter and setter methods of a property buildDir.

// Using a getter method
println project.buildDir
println getProject().getBuildDir()

// Using a setter method
project.buildDir = 'new target'
getProject().setBuildDir('new target')


11) In Gradle can we call methods like below:
test.systemProperty 'a.prop', 'value'

Yes, this is a valid method calling in Gradle. Groovy contains a special feature in methods calling that is the parentheses are optional for method calling.


12) What are Closure is Gradle?

Gradle DSL uses closures in many places. Where the last parameter of a method is a closure, closure can be placed after the method call. Below is the syntax:

repositories({ println "in a closure" })


13) In Gradle how to add dependencies to the task?

Here is an example which adds a dependency from taskX to taskY:

task taskX << {
   println 'taskX'
}
task taskY(dependsOn: 'taskX') << {
   println "taskY"
}


14) What is task collection in Gradle?

Collection of task names is referred by its tasks collection.


15) How to set the dependencies in Gradle?

Dependency configuration is nothing but defines set of dependencies. This feature can be used to declare external dependencies means which need to download from the web. Here is the code snippet:

apply plugin: 'java'

repositories {
   mavenCentral()
}

dependencies {
   compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
   testCompile group: 'junit', name: 'junit', version: '4.+'
}