Gradle DevOps Interview Questions and Answers for Freshers and Experienced

Gradle Devops @ Freshers.in

1. What Are Benefits Of Daemon in Gradle 3.0
a. It has good UX
b. It is very powerful
c. It is aware of the resource
d. It is well integrated with the Gradle Build scans
e. It has been default enabled

2. How to execute tasks in multi-project builds ?
When you are in a multi-project build, subproject tasks can be executed with “:” separating subproject name and task name. The following are equivalent when run from the root project:
$ gradle :my-subproject:taskName
$ gradle my-subproject:taskName

3. What are the things that you need to take care when invoking Gradle from within a subproject ?
When invoking Gradle from within a subproject, the project name should be omitted:
$ cd my-subproject
$ gradle taskName
When executing the Gradle Wrapper from subprojects, one must reference gradlew relatively. For example: ../gradlew taskName.

4. Can I Execute multiple tasks ?
Yes, You can also specify multiple tasks. The tasks will be executed as quickly as possible while still honoring task dependencies. Precise order of execution is determined by the tasks’ dependencies, and a task having no dependencies may execute earlier than it is listed on the command-line. For example, the following will execute the test and deploy tasks in the order that they are listed on the command-line and will also execute the dependencies for each task.
$ gradle test deploy

5. How to excluding tasks from execution ?
You can exclude a task from being executed using the -x or –exclude-task command-line option and providing the name of the task to exclude.
$ gradle dist –exclude-task test
> Task :compile
compiling source
> Task :dist
building the distribution
BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed
You can see that the test task is not executed, even though it is a dependency of the dist task.The test task’s dependencies such as compileTest are not executed either. Those dependencies of test that are required by another task, such as compile, are still executed.

6. Why Gradle Is Preferred Over Maven or Ant?
There isn’t a great support for multi-project builds in Ant and Maven. Developersend up doing a lot of coding to support multi-project builds. Also having some build-by-convention is nice and makes build scripts more concise. With Maven, it takes build by convention too far, and customizing your build process becomes a hack.
Maven also promotes every project publishing an artifact. Maven does not support subprojects to be built and versioned together. But with Gradle developers can have the flexibility of Ant and build by convention of Maven. Groovy is easier and clean to code than XML. In Gradle, developers can define dependencies between projects on the local file system without the need to publish artifacts to repository.

7. Explain the differences between Gradle and Apache Maven
Flexibility: Google chose Gradle as the official build tool for Android; not because build scripts are code, but because Gradle is modeled in a way that is extensible in the most fundamental ways. Both Gradle and Maven provide convention over configuration. However, Maven provides a very rigid model that makes customization tedious and sometimes impossible. While this can make it easier to understand any given Maven build, it also makes it unsuitable for many automation problems. Gradle, on the other hand, is built with an empowered and responsible user in mind.
Performance : Both Gradle and Maven employ some form of parallel project building and parallel dependency resolution. The biggest differences are Gradle’s mechanisms for work avoidance and incrementally. Following features make Gradle much faster than Maven:Incrementally:Gradle avoids work by tracking input and output of tasks and only running what is necessary. Build Cache:Reuses the build outputs of any other Gradle build with the same inputs. Gradle Daemon:A long-lived process that keeps build information “hot” in memory.
User Experience : Maven’s has a very good support for various IDE’s. Gradle’s IDE support continues to improve quickly but is not great as of Maven. Although IDEs are important, a large number of users prefer to execute build operations through a command-line interface. Gradle provides a modern CLI that has discoverability features like `gradle tasks`, as well as improved logging and command-line completion.
Dependency Management : Both build systems provide built-in capability to resolve dependencies from configurable repositories. Both are able to cache dependencies locally and download them in parallel. As a library consumer, Maven allows one to override a dependency, but only by version. Gradle provides customizable dependency selection and substitution rules that can be declared once and handle unwanted dependencies project-wide. This substitution mechanism enables Gradle to build multiple source projects together to create composite builds. Maven has few, built-in dependency scopes, which forces awkward module architectures in common scenarios like using test fixtures or code generation. There is no separation between unit and integration tests, for example. Gradle allows custom dependency scopes, which provides better-modeled and faster builds.

8. How can you specify tasks abbreviation instead of full name of the task ?
When you specify tasks on the command-line, you don’t have to provide the full name of the task. You only need to provide enough of the task name to uniquely identify the task. For example, it’s likely gradle che is enough for Gradle to identify the check task. The same applies for project names. You can execute the check task in the library subproject with the gradle lib:che command.
You can use camel case patterns for more complex abbreviations. These patterns are expanded to match camel case and kebab case names. For example the pattern foBa (or even fB) matches fooBar and foo-bar.
More concretely, you can run the compileTest task in the my-personal-library subproject with the gradle mPL:cT command.
$ gradle mPL:cT
> Task :my-awesome-personal:compileTest
compiling unit tests
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

9. What is the command for running applications and running all checks and cleaning outputs?
$ gradle run
$ gradle check
$ gradle clean

10. What is the command for listing projects and listing tasks ?
$ gradle tasks
$ gradle tasks –all
If you need to be more precise,
$ gradle tasks –group=”build setup”

11. What is Gradle Build Life Cycle ?
Gradle Build life cycle consists of following three steps.
a. Initialization phase: In this phase the project layer or objects are organized.
b. Configuration phase: In this phase all the tasks are available for the current build and a dependency graph is created.
c. Execution phase: In this phase tasks are executed.

12. What is Gradle
An open-source build automation system called Gradle expands on the ideas of Apache. Apache Maven and Ant. Instead of using an XML configuration file, Gradle uses a genuine programming language called Groovy. The sequence in which tasks can be executed is determined by Gradle using a directed acyclic graph (or “DAG”). For multi-project builds, which may get fairly massive, Gradle was created. By intelligently identifying the sections of the build tree that have been updated, it allows incremental builds by avoiding the need to repeat the execution of any tasks that are only dependent on those parts.

13. Explain the Advantages of Gradle?
Declarative Builds: Probably one of the biggest advantage of Gradle is Groovy language. Gradle provides declarative language elements. Which providea build-byconvention support for Java, Groovy, Web and Scala.
Structured Build: Gradle allows developers to apply common design principles to their build. It provides a perfect structure for build, so that well-structured and easily maintained, comprehensible build structures can be built.
Deep API: Using this API, developers can monitor and customize its configuration and execution behaviors.
Scalability: Gradle can easily increase productivity, from simple and single project builds to huge enterprise multi-project builds.
Multi-project builds: Gradle supports multi-project builds and also partial builds.
Build management: Gradle supports different strategies to manage project dependencies.
First build integration tool − Gradle completely supports ANT tasks, Maven and lvy repository infrastructure for publishing and retrieving dependencies. It also provides a converter for turning a Maven pom.xml to Gradle script.
Ease of migration: Gradle can easily adapt to any project structure.
Gradle Wrapper: Gradle Wrapper allows developers to execute Gradle builds on machines where Gradle is not installed. This is useful for continuous integration of servers.
Free open source − Gradle is an open source project, and licensed under the Apache Software License (ASL).
Groovy: Gradle’s build scripts are written in Groovy, not XML. But unlike other approaches this is not for simply exposing the raw scripting power of a dynamic language. The whole design of Gradle is oriented towards being used as a language, not as a rigid framework.

14. How can you manage Gradle Daemon ?
You can manage the Gradle Daemon through the following command line options.
–daemon, –no-daemon : Use the Gradle Daemon to run the build. Starts the daemon if not running or existing daemon busy. Default is on.
–foreground : Starts the Gradle Daemon in a foreground process.
–status (Standalone command) : Run gradle –status to list running and recently stopped Gradle daemons. Only displays daemons of the same Gradle version.
–stop (Standalone command) : Run gradle –stop to stop all Gradle Daemons of the same version.
-Dorg.gradle.daemon.idletimeout=(number of milliseconds) : Gradle Daemon will stop itself after this number of milliseconds of idle time. Default is 10800000 (3 hours).

15. How to set log level in Gradle ?
You can customize the verbosity of Gradle logging with the following options,
-Dorg.gradle.logging.level=(quiet,warn,lifecycle,info,debug) : Set logging level via Gradle properties.
-q, –quiet : Log errors only.
-w, –warn : Set log level to warn.
-i, –info : Set log level to info.
-d, –debug : Log in debug mode (includes normal stacktrace).

16. Explain about environment options in Gradle ?
-b, –build-file (deprecated) : Specifies the build file. For example: gradle –build-file=foo.gradle. The default is build.gradle, then build.gradle.kts.
-c, –settings-file (deprecated) : Specifies the settings file. For example: gradle –settings-file=somewhere/else/settings.gradle
-g, –gradle-user-home : Specifies the Gradle user home directory. The default is the .gradle directory in the user’s home directory.
-p, –project-dir : Specifies the start directory for Gradle. Defaults to current directory.
–project-cache-dir : Specifies the project-specific cache directory. Default value is .gradle in the root project directory.
-D, –system-prop : Sets a system property of the JVM, for example -Dmyprop=myvalue. See System Properties.
-I, –init-script : Specifies an initialization script. See Init Scripts.
-P, –project-prop : Sets a project property of the root project, for example -Pmyprop=myvalue. See Project Properties.
-Dorg.gradle.jvmargs : Set JVM arguments.
-Dorg.gradle.java.home : Set JDK home dir.

17. What is Gradle Wrapper?
The wrapper is a shell script for other operating systems and a batch script for Windows. The preferred method for beginning a Gradle build is Gradle Wrapper. Gradle will automatically download and run the build when a Gradle build is launched using the wrapper.

18. What is Gradle Build Script File Name?
This kind of name is written in the build.gradle format. The Gradle scripting language is often configured using it.

19. How To Add Dependencies In Gradle?
To make sure that the dependent for your project is added, you must include the configuration dependency, such as building the block dependencies of the build gradle file.

20. What Is Dependency Configuration?
Dependency configuration includes external dependencies, which you must carefully install and make sure to download from the internet. There are a few crucial aspects to this arrangement, including:
1. Compilation: The project which you would be starting and working on the first needs to be well compiled and ensure that it is maintained in the good condition.
2. Runtime: It is the desired time which is required to get the work dependency in the form of collection.
3. Test Compile: The dependencies check source requires the collection to be made for running the project.
4. Test runtime: This is the final process which needs the checking to be done for running the test that is in a default manner considered to be the mode of runtime

21. What Is Gradle Daemon?
A daemon is a type of computer program that functions in the background without being directly controlled by an interactive user. Gradle utilizes a number of supporting libraries that have a time-consuming initialization process and runs on the Java Virtual Machine (JVM). As a result, it occasionally appears to start off a bit slowly. The Gradle Daemon, a persistent background process that executes your builds far faster than would otherwise be the case, is the answer to this issue. By utilizing caching and skipping the pricey bootstrapping procedure, we are able to achieve this by preserving information about your project in memory. There is no difference between using the Daemon and Gradle to run builds.

22. What Is Dependency Management in Gradle?
Software projects rarely work in isolation. In most cases, a project relies on reusable functionality in the form of libraries or is broken up into individual components to compose a modularized system. Dependency management is a technique for declaring, resolving and using dependencies required by the project in an automated fashion. Gradle has built-in support for dependency management and lives up the task of fulfilling typical scenarios encountered in modern software projects.

23. What are Gradle Build Scripts?
Gradle creates a script file to manage tasks and projects. One or more projects are represented by each Gradle build. A project is a web application or a library JAR.

24. How to continuing the build when a failure occurs ?
By default, Gradle will abort execution and fail the build as soon as any task fails. This allows the build to complete sooner, but hides other failures that would have occurred. In order to discover as many failures as possible in a single build execution, you can use the –continue option.
$ gradle test –continue
When executed with –continue, Gradle will execute every task to be executed where all of the dependencies for that task completed without failure, instead of stopping as soon as the first failure is encountered. Each of the encountered failures will be reported at the end of the build.

25. How to see what dependencies exist on which configurations ?
Build scans give a full, visual report of what dependencies exist on which configurations, transitive dependencies, and dependency version selection.
$ gradle myTask –scan
This will give you a link to a web-based report, where you can find dependency information.

26. How to listing project dependencies ?
Running gradle dependencies gives you a list of the dependencies of the selected project, broken down by configuration. For each configuration, the direct and transitive dependencies of that configuration are shown in a tree. Below is an example of this report.
$ gradle dependencies
Running gradle properties gives you a list of the properties of the selected project.
$ gradle -q api:properties

27. What are the Gradle debugging options ?
-?, -h, –help : Shows a help message with all available CLI options.
-v, –version : Prints Gradle, Groovy, Ant, JVM, and operating system version information and exit without executing any tasks.
-V, –show-version : Prints Gradle, Groovy, Ant, JVM, and operating system version information and continue execution of specified tasks.
-S, –full-stacktrace : Print out the full (very verbose) stacktrace for any exceptions. See also logging options.
-s, –stacktrace : Print out the stacktrace also for user exceptions (e.g. compile error). See also logging options.
–scan : Create a build scan with fine-grained information about all aspects of your Gradle build.
-Dorg.gradle.debug=true : Debug Gradle client (non-Daemon) process. Gradle will wait for you to attach a debugger at localhost:5005 by default.
-Dorg.gradle.debug.port=(port number) : Specifies the port number to listen on when debug is enabled. Default is 5005.
-Dorg.gradle.debug.server=(true,false) : If set to true and debugging is enabled, Gradle will run the build with the socket-attach mode of the debugger. Otherwise, the socket-listen mode is used. Default is true.
-Dorg.gradle.debug.suspend=(true,false) : When set to true and debugging is enabled, the JVM running Gradle will suspend until a debugger is attached. Default is true.
-Dorg.gradle.daemon.debug=true : Debug Gradle Daemon process.

28. What Is Gradle Multi-Project Build?
Multi-project builds helps with modularization. It allows a person to concentrate on one area of work in a larger project, while Gradle takes care of dependencies from other parts of the project. A multi-project build in Gradle consists of one root project, and one or more subprojects that may also have subprojects. While each subproject could configure itself in complete isolation of the other subprojects, it is common that subprojects share common traits. It is then usually preferable to share configurations among projects, so the same configuration affects several subprojects.

29. What Is Gradle Build Task?
Gradle Build Tasks is made up of one or more projects and a project represents what is been done with Gradle.
Some key of features of Gradle Build Tasks are:
1. Task has life cycled methods [do first, do last].
2. Build Scripts are code.
3. Default tasks like run, clean etc.
4. Task dependencies can be defined using properties like dependsOn.

30. What is Gradle Java Plugin?
The Java plugin adds Java compilation along with testing and bundling capabilities to the project. It is introduced in the way of a SourceSet which act as a group of source files complied and executed together.

31. What is Dependency Configuration?
A set of dependencies is termed as dependency configuration, which contains some external dependencies for download and installation.
Here are some key features of dependency configuration are:
Compile : The project must be able to compile together.
Runtime : It is the required time needed to get the dependency work in the collection.
Test Compile : The check source of the dependencies is to be collected in order to run the project.
Test Runtime : The final procedure is to check and run the test which is by default act as a runtime mode.

32. How to execute a task called “myTask” on the root
$ gradle :myTask

33. How to create new Gradle builds ?
Use the built-in gradle init task to create a new Gradle builds, with new or existing projects.
$ gradle init
Most of the time you’ll want to specify a project type. Available types include basic (default), java-library, java-application, and more. See init plugin documentation for details.
$ gradle init –type java-library

Author: user

Leave a Reply