gradle

Execute Gradle builds on Jenkins and Gradle Top 40+ Interview Questions and Answers

Share your love

What is Gradle?

Gradle is an open-source build automation tool flexible enough to build almost any type of software. Gradle makes few assumptions about what you’re trying to build or how to build it. This makes Gradle particularly flexible.

Design

Gradle bases its design on the following fundamentals:High performance

Gradle avoids unnecessary work by only running tasks that need to do work because inputs or outputs have changed. Gradle uses various caches to reuse outputs from previous builds. With a shared build cache, you can even reuse outputs from other machines.JVM foundation

Gradle runs on the JVM. This is a bonus for users familiar with Java, since build logic can use the standard Java APIs. It also makes it easy to run Gradle on different platforms.Conventions

Gradle makes common types of projects easy to build through conventions. Plugins set sensible defaults to keep build scripts minimal. But these conventions don’t limit you: you can configure settings, add your own tasks, and make many other customizations in your builds.Extensibility

Most builds have special requirements that require custom build logic. You can readily extend Gradle to provide your own build logic with custom tasks and plugins. See Android builds for an example: they add many new build concepts such as flavors and build types.IDE support

Several major IDEs provide interaction with Gradle builds, including Android Studio, IntelliJ IDEA, Eclipse, VSCode, and NetBeans. Gradle can also generate the solution files required to load a project into Visual Studio.Insight

Build Scan™ provides extensive information about a build that you can use to identify issues. You can use Build Scans to identify problems with a build’s performance and even share them for debugging help.

How to executing Gradle builds on Jenkins?

Building Gradle projects doesn’t stop with the developer’s machine. Continuous Integration (CI) has been a long-established practice for running a build for every single change committed to version control to tighten the feedback loop.

In this guide, we’ll discuss how to configure Jenkins for a typical Gradle project.

What you’ll need

  • A text editor
  • A command prompt
  • The Java Development Kit (JDK), version 1.7 or higher
  • A Jenkins installation (setup steps explained in this post)

Setup a typical project

As example, this guide is going to focus on a Java-based project. More specifically, a Gradle plugin written in Java and tested with Spek. First, we’ll get the project set up on your local machine before covering the same steps on CI.

Just follow these steps:

Clone the Gradle Site Plugin repository

$ git clone https://github.com/gradle/gradle-site-plugin.git
Cloning into 'gradle-site-plugin'...
$ cd gradle-site-plugin

Build the project

As a developer of a Java project, you’ll typical want to compile the source code, run the tests and assemble the JAR artifact. That’s no different for Gradle plugins. The following command achieves exactly that:

$ ./gradlew build

BUILD SUCCESSFUL
14 actionable tasks: 14 executed

The project provides the Gradle Wrapper as part of the repository. It is a recommended practice for any Gradle project as it enables your project to built on CI without having to install the Gradle runtime.

Build scan integration

The sample project is equipped with support for generating build scans. Running the build with the command line option --scan renders a link in the console.

$ ./gradlew build --scan

Publishing build scan...
https://gradle.com/s/7mtynxxmesdio

The following section will describe how to build the project with the help of Jenkins.

Setup Jenkins

Jenkins is one of the most prominent players in the field. In the course of this section, you’ll learn how to set up Jenkins, configure a job to pull the source code from GitHub and run the Gradle build.

Install and start Jenkins

On the Jenkins website you can pick from a variety of distributions. This post uses the runnable WAR file. A simple Java command brings up the Jenkins server.

$ wget https://mirrors.jenkins.io/war-stable/latest/jenkins.war
$ java -jar jenkins.war

In the browser, navigate to localhost with port 8080 to render the Jenkins dashboard. You will be asked to set up an new administration user and which plugins to install.

Installation of plugins

Confirm to install the recommended plugins when starting Jenkins for the first time. Under “Manage Jenkins > Manage Plugins” ensure that you have the following two plugins installed.

  • Git plugin
  • Gradle plugin

Next, we can set up the job for building the project.

Create a Jenkins job

Setting up a new Gradle job can be achieved with just a couple of clicks. From the left navigation bar select “New Item > Freestyle project”. Enter a new name for the project. We’ll pick “gradle-site-plugin” for the project.

Select the radio button “Git” in the section “Source Code Management”. Enter the URL of the GitHub repository: https://github.com/gradle/gradle-site-plugin.git.

jenkins scm

Furthermore, create a “Build step” in the section “Build” by selecting “Invoke Gradle build script”. As mentioned before, we’ll want to use the Wrapper to execute the build. In the “Tasks” input box enter the build and use the “Switches” --scan -s to generate a build scan and render a stack trace in case of a build failure.

jenkins build step

Execute the job

Save the configuration of job and execute an initial build by triggering the “Build Now” button. The build should finish successfully and render a “Gradle Build Scan” icon that brings you directly to the build scan for the given build.

jenkins build scan

There are various options to trigger Jenkins builds continuously: from polling the repository periodically, to building on a set schedule, or via callback URL.

Gradle Fabricating Q&A

Q1. To execute the hello task in the build file, type ___ on the command line.

Answer:- Gradle hello or gradle –q hello

Q2. Consider the following gradle build script:

task a{

ext.myProperty = “Hello”

}

task b{

doLast {

println a.myProperty

println c.myProperty

}}

task c(dependsOn: b){

ext.myProperty = “Hey”

println “Hi”}

If the task gradle -q c is run, what is the output? 

Answer:- hi Hello hey

Q3. In gradle, a disabled task is labelled as _________.

Answer:-  Disabled

Q4. Multiline comments in Gradle build files are included using _____ .

Answer:-  /* Multi line */

Q5. Gradle tasks can also be created and extended dynamically at runtime.

Answer:- True

Q6. Gradle build scripts use a combination of declarative and imperative statements.

Answer:- True

Q7. Which plugins are configured while integrating gradle with Jenkins?

Answer:- Gradle plugin and plugin of repository used

Q8. Which of the following statement is true about Jenkins?

Answer:- All the options

Q9. Jobs are the unit of execution in Jenkins.

Answer: –True

Q10. In Jenkins, a build job performs _______. 

Answer:- All the options

Q11.

Q11. Which are the two types of plugins in gradle? 

Answer:- Script and binary

Q12. Which of the following statements are true?

Answer: – Ant or maven script can be migrated to gradle build script

Q13. When gradle plugins are applied to a project, it is possible to _____________. 

Answer:- All the options

Q14. How to check gradle version?

Answer:- Gradle -v or gradle -version

Q15. How to check gradle version?

Answer:- All the options

Q16. The two build files generated by android studio are in ________________.

Answer:- Root folder of project and app directory

Q17. The war plugin enables the default JAR archive generation of the Java plugin.

Answer:- True

Q18. Running Gradle builds with the Daemon executes the build quickly.

Answer: – True

Q19. In gradle to specify, which projects belong to the build, and which file is used? Answer- Settings.gradle

Q20. Gradle build scripts are written in _________. 

Answer:- Groovy or Kotlin

Q21. In Gradle , the java plugin searches for java test source code in the directory ___________. 

Answer:- src/test/java

Q22. Gradle supports multiple project templates called archetypes ________. 

Answer:- False 

Q23. Gradle requires a Java JDK or JRE and Groovy to be installed.

Answer: – False

Q24. Gradle requires a Java JDK or JRE and Groovy to be installed. 

Answer:- True

Q25. The wrapper plugin can be auto-applied to the root project of the current build without modifying the build.gradle file.

Answer:- False

Q26. Gradle tasks can also be created and extended dynamically at runtime.

Answer:- True

Q27. Gradle is an imperative build tool.

Answer:-False

Q28. Which task is executed to use Gradle Build Init plugin?

Answer: – init

Q29. Which method is used to attach a predicate to a skipping task? 

Answer:- OnlyIf()

Q30. Which of the following is Gradle’s in-built task?

Answer:-  All the options

Q31. Lifecycle tasks typically have task actions.

Answer:- False

Q32. Java Plugin is a binary plugin

Answer: – True

Q33. The first line added in the build script to make the Android plug-in available for a Gradle build is ____________. apply plugin

Answer:- ‘com.android.application’

Q34. Gradle is a _______ based build tool. 

Answer:- JVM

Q35. Gradle Build Init plugin is an automatically applied plugin. 

Answer:- True

Q36. Applying the Application plugin also implicitly applies the Java plugin. 

Answer:- True

Q37. hich of the following is Gradle’s in-built task?

Answer- All the options mentioned

Share your love

Leave a Reply

Your email address will not be published. Required fields are marked *