--- primary_color: '#31bf2a' secondary_color: '#f5f5f5' text_color: black shuffle_questions: false shuffle_answers: false --- ## Which format CAN'T you use to define a Gradle build script 1. [ ] Kotlin 1. [ ] Groovy 1. [x] XML ## In which scenario should you use a local Gradle installation? 1. [ ] To run a Gradle task 1. [x] To initialise a Gradle project 1. [ ] To clean your Gradle project ## Which command should you run? You're working on a Windows machine, and have been asked to make some changes to a Java project built with Gradle.

You've checked out the project, and now wish to build it and run tests within a Windows command prompt.

Which command should you run, choosing the option which will most likely result in a successful build?
1. [x] `gradlew.bat build` 1. [ ] `./gradlew build` 1. [ ] `gradle build` ## Which repositories does Gradle support for downloading dependencies? - [x] Maven central - [x] Google - [x] A custom Maven repository ## Where is the Gradle project name configured? 1. [ ] *build.gradle.kts* 1. [ ] *gradlew* 1. [ ] *gradlew.bat* 1. [x] *settings.gradle.kts* ## Which of the following directories and files SHOULD NOT go into version control? You're helping out a colleague who's created a Gradle project from scratch, without using the setup wizard.

He's not sure which directories should go into version control and which shouldn't.

Which of the following directories and files SHOULD NOT go into version control? - [x] *.gradle* (directory) - [x] *build* (directory) - [ ] *build.gradle.kts* - [ ] *gradle* (directory) - [ ] *gradlew* - [ ] *gradlew.bat* - [ ] *settings.gradle.kts* ## What's the most likely cause of this issue? You've been asked to make some changes to a very old Java project, which is built using Gradle. You checkout the project, and run the following command:

`gradle build`

You see a lot of errors. You're confused because your colleague shows the same project building on his machine. You've got the latest version of Gradle installed.

What's the most likely cause of this issue? 1. [x] Your local Gradle version is incompatible with the version the project expects 1. [ ] Your Gradle installation is broken 1. [ ] The project's build is broken ## Which of the following options would fix this problem? You're creating a Gradle build for an existing Java project.

You've noticed that when you run `./gradlew assemble`, it says *BUILD SUCCESSFUL* but no compiled classes get generated in the *build* directory.

You're confused because you have lots of Java classes located in *src/java*.

Which of the following options would fix this problem? 1. [ ] Apply the Java plugin in build.gradle.kts 1. [ ] Run `./gradlew build` instead of `./gradlew assemble` 1. [x] Move the Java classes into *src/main/java* 1. [ ] Run `gradle init` to reset the project ## What should you do to resolve this problem? You're working on a small Java application which relies on the *commons-lang3* Java library.

You've configured the dependency like this in your *build.gradle.kts* file: ```kotlin dependencies { testImplementation("org.apache.commons:commons-lang3:3.11") } ``` You want to generate a *jar* file for the application, but when you run `./gradlew assemble` you're seeing the following error: ```plaintext error: package org.apache.commons.lang3 does not exist` ``` What should you do to resolve this problem? 1. [ ] Run `./gradlew test` instead. 1. [x] Update the dependency to `implementation("org.apache.commons:commons-lang3:3.11")` 1. [ ] Use a different library, since *commons-lang3* is obviously not fit for purpose. ## How should you fix this? You're adding some tests to a project built using Gradle, and see the following error when you run *./gradlew test*: ```plaintext error: package org.junit.jupiter.api does not exist ``` How should you fix this? 1. [ ] Add a *junit-jupiter* dependency to *settings.gradle.kts* 1. [x] Add a *junit-jupiter* dependency to *build.gradle.kts* 1. [ ] Configure the Java plugin in *build.gradle.kts* 1. [ ] Run the test again. It was probably a one-off. ## What would be the behaviour when running the following command? In a Gradle project there are two tasks, *taskA* and *taskB*.

If *taskA* depends on *taskB*, what would be the behaviour when running the following command? `./gradlew taskA` 1. [x] Task B will run before task A 1. [ ] Task A will run before task B 1. [ ] Tasks A and B will run simultaneously ## Which Gradle task should you run? You're working on a large code project for a media website, built using Gradle.

You make some code changes and now want to generate the jar file. You're in a hurry and don't want to run all the tests, which are known to be slow.

Which Gradle task should you run to generate the jar file in the shortest time possible? 1. [ ] *build* 1. [ ] *check* or *test* 1. [x] *assemble* or *jar* 1. [ ] *classes*

Question 1

Gradle doesn’t support XML format for its builds, but does support Kotlin and Groovy. In fact, not using XML is one advantage of Gradle over other build tools such as Maven.

Question 2

A local Gradle installation should only be used for initialising a fresh Gradle project.

Question 3

When running Gradle tasks you should always use the provided Gradle wrapper script. On Windows, this script is gradlew.bat, so gradlew.bat build is the correct answer.

  • ./gradlew build is incorrect as this is specific to Linux or Mac environments
  • gradle build might work if you have a local Gradle installation, but running gradlew.bat is always the better option to ensure you’re using the correct version of Gradle for the project. Hence, this answer is incorrect.

Question 4

That’s right, Gradle supports all these types of repositories for downloading dependencies.

Question 5

You can configure the project name in settings.gradle.kts like this:

rootProject.name = "get-going-with-gradle"

Question 6

Everything listed except the .gradle directory and the build directory should be committed into version control.

  • the .gradle directory is a local cache used by Gradle during your build
  • the build directory is reserved for any locally built artifacts. It changes based on what Gradle tasks you run, so it doesn’t make sense to commit it into version control.

Question 7

A Gradle project should come bundled up with the Gradle wrapper. You should always use the wrapper script to run Gradle tasks against the project.

In this case it would have been better to run:

./gradlew build

This would ensure that the version of Gradle you’re using to build the project is the same one specified by the project itself.

The problem you’re seeing is most likely caused because your new version of Gradle doesn’t support the old version of Gradle used by the project.

Question 8

The Java plugin expects Java classes to exist in the src/main/java directory. Since the code is currently located in src/java, this explains why no classes are being compiled.

Moving the classes to src/main/java is the correct fix.

  • applying the Java plugin is incorrect since the fact that running ./gradlew assemble was successful implies it’s already been applied
  • running ./gradlew build is incorrect since that will just run the test task as well, which won’t fix the issue.
  • running gradle init is incorrect as that won’t fix the incorrect location of the Java classes. Besides, once you’ve already setup a Gradle project gradle init will have no effect.

Question 9

If your application relies on the commons-lang3 library, then it must be present on the Java classpath during compilation. Currently the dependency is declared on the testImplementation dependency configuration, which means it can only be used within test classes.

Moving the dependency to the implementation configuration is the correct solution, as it means the library will be on the Java classpath when compiling both main and test classes.

  • running the test task is incorrect as this won’t help with the compilation issue within the main application classes
  • using a different library is incorrect, since the failure is related to a misconfigured Gradle build and not the library itself

Question 10

This error implies a failure during test compilation, caused by a missing dependency for junit-jupiter.

Dependencies are configured in build.gradle.kts, so you’d want to add a dependency like this:

dependencies {
  testImplementation("org.junit.jupiter:junit-jupiter:5.6.3")
}

Question 11

If taskA depends on taskB, then taskA cannot be run until taskB has run. Therefore, taskB runs first, followed by taskA.

Question 12

The correct answer is assemble or jar, as these tasks are the most specific task for what you want to achieve. This is the quickest way to get the jar file built.

  • build is incorrect because although it would build the jar file, it would also run the tests which would slow you down
  • check or test is incorrect because this would also run the tests which would slow you down. Also, it wouldn’t actually build the jar file
  • classes is incorrect because this task compiles your main code and processes your main resources, but doesn’t build the jar file