Gradle Flashcards

1
Q

Key features of gradle?

A
  1. Gradle is a general-purpose build tool
  2. The core model is based on tasks
    1. Gradle models its builds as Directed Acyclic Graphs (DAGs) of tasks (units of work). What this means is that a build essentially configures a set of tasks and wires them together — based on their dependencies — to create that DAG. Once the task graph has been created, Gradle determines which tasks need to be run in which order and then proceeds to execute them.
  3. Gradle has several fixed build phases
  4. Gradle is extensible in more ways than one
  5. Build scripts operate against an API
    1. It’s easy to view Gradle’s build scripts as executable code, because that’s what they are. But that’s an implementation detail: well-designed build scripts describe what steps are needed to build the software, not how those steps should do the work. That’s a job for custom task types and plugins.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What do tasks consist of?

A
  1. Actions
    1. pieces of work that do something, like copy files or compile source
  2. Inputs
    1. values, files and directories that the actions use or operate on
  3. Outputs
    1. files and directories that the actions modify or generate

One last thing: Gradle’s incremental build support is robust and reliable, so keep your builds running fast by avoiding the clean task unless you actually do want to perform a clean

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the build phases of Gradle?

A
  1. Initialization
    1. Sets up the environment for the build and determine which projects will take part in it.
  2. Configuration
    1. Constructs and configures the task graph for the build and then determines which tasks need to run and in which order, based on the task the user wants to run.
  3. Execution
    1. Runs the tasks selected at the end of the configuration phase.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does gradle can be extended?

A
  • Custom task types
    • When you want the build to do some work that an existing task can’t do, you can simply write your own task type. It’s typically best to put the source file for a custom task type in the buildSrc directory or in a packaged plugin. Then you can use the custom task type just like any of the Gradle-provided ones.
  • Custom task actions.
    • You can attach custom build logic that executes before or after a task via the Task.doFirst() and Task.doLast() methods.
  • Extra properties on projects and tasks.
    • These allows you to add your own properties to a project or task that you can then use from your own custom actions or any other build logic. Extra properties can even be applied to tasks that aren’t explicitly created by you, such as those created by Gradle’s core plugins.
  • Custom conventions
    • Conventions are a powerful way to simplify builds so that users can understand and use them more easily. This can be seen with builds that use standard project structures and naming conventions, such as Java builds. You can write your own plugins that provide conventions — they just need to configure default values for the relevant aspects of a build.
  • A custom model
    • Gradle allows you to introduce new concepts into a build beyond tasks, files and dependency configurations. You can see this with most language plugins, which add the concept of source sets to a build. Appropriate modeling of a build process can greatly improve a build’s ease of use and its efficiency
  • Build scripts operate against an API
    • It’s easy to view Gradle’s build scripts as executable code, because that’s what they are. But that’s an implementation detail: well-designed build scripts describe what steps are needed to build the software, not how those steps should do the work. That’s a job for custom task types and plugins.
      *
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does gradle projects command do?

A

Displays the sub-projects of root project if there are any?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does gradle tasks command do?

A

Displays the tasks runnable from root project.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to get a task description?

A

gradle help –task taskname

The help task can display extra information about a task, including which projects contain that task and what options the task supports.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does the gradle can be debugged?

A

gradle help -Dorg.gradle.debug=true

will allow you to set breakpoints and attach debugger?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does compare maven to gradle?

A

Gradle and Maven have fundamentally different views on how to build a project. Gradle provides a flexible and extensible build model that delegates the actual work to a graph of task dependencies.

Maven uses a model of fixed, linear phases to which you can attach goals (the things that do the work).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Maven build phases vs gradle lifecycle tasks?

A

Maven builds are based around the concept of build lifecycles that consist of a set of fixed phases. This can prove an impediment for users migrating to Gradle because its build lifecycle is something different, although it’s important to understand how Gradle builds fit into the structure of initialization, configuration, and execution phases. Fortunately, Gradle has a feature that can mimic Maven’s phases: lifecycle tasks.

  • clean
    • Use the clean task provided by the Base Plugin.
  • compile
    • Use the classes task provided by the Java Plugin and other JVM language plugins. This compiles all classes for all source files of all languages and also performs resource filtering via the processResources task.
  • test
    • Use the test task provided by the Java Plugin. It runs just the unit tests, or more specifically, the tests that make up the test source set.
  • package
    • Use the assemble task provided by the Base Plugin. This builds whatever is the appropriate package for the project, for example a JAR for Java libraries or a WAR for traditional Java webapps.
  • verify
    • Use the check task provided by the Base Plugin. This runs all verification tasks that are attached to it, which typically includes the unit tests, any static analysis tasks — such as Checkstyle — and others. If you want to include integration tests, you will have to configure these manually, which is a simple process.
  • install
    • Use the publishToMavenLocal task provided by the Maven Publish Plugin. Note that Gradle builds don’t require you to “install” artifacts as you have access to more appropriate features like inter-project dependencies and composite builds. You should only use publishToMavenLocal for interoperating with Maven builds. Gradle also allows you to resolve dependencies against the local Maven cache, as described in the Declaring repositories section.
  • deploy
    • Use the publish task provided by the Maven Publish Plugin — making sure you switch from the older Maven Plugin (ID: maven) if your build is using that one. This will publish your package to all configured publication repositories. There are also other tasks that allow you to publish to a single repository even when multiple ones are defined.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to initialize gradle project?

A

> gradle init

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Managing dependencies strategies Maven vs Gradle?

A

Maven uses a “closest” (to the root) match algorithm, whereas Gradle picks the newest.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to declare a dependency in Gradle?

A

configuration ‘group:artifact:version’

dependencies {

implementation ‘log4j:log4j:1.2.12’ ①

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Gradle configurations mapping to Maven scopes?

A
  1. compile
    1. Gradle has two configurations that can be used in place of the compile scope: implementation and api. The former is available to any project that applies the Java Plugin, while api is only available to projects that specifically apply the Java Library Plugin.
  2. runtime
    1. Use the runtimeOnly configuration
  3. test
    1. Gradle distinguishes between those dependencies that are required to compile a project’s tests and those that are only needed to run them.
      1. Dependencies required for test compilation should be declared against the testImplementation
      2. Those that are only required for running the tests should use testRuntimeOnly
  4. provided
    1. Use the compileOnly configuration.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the default maven scope?

A

It is compile

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Does Gradle define the default repository?

A

No.

In order to have the same behavior as your Maven build, just configure Maven Central in your Gradle build.

17
Q

How to get behavior of the section?

A

If you want to ensure consistency of versions across all projects in a multi-project build, similar to how the block in Maven works, you can use the Java Platform Plugin.

18
Q

What is BOM?

A

Bills of materials (BOMs)

Maven allows you to share dependency constraints by defining dependencies inside a section of a POM file that has a packaging type of pom. This special type of POM (a BOM) can then be imported into other POMs so that you have consistent library versions across your projects.

19
Q

How does Gradle can use BOM?

A

Gradle can use such BOMs for the same purpose, using a special dependency syntax based on platform() and enforcedPlatform() methods.

20
Q

How can we add modules to Gradle project?

A

By modifying gradle.settings file

rootProject.name = ‘simple-multi-module’
include ‘simple-weather’, ‘simple-webapp’

21
Q

Maven profiles in gradle?

A

Gradle supports parametrized build via ext variables

To activate pass variable

> gradle -PbuildProfile=test greeting

22
Q

Where can we add Gradle properties?

A

In gradle properties file.

  • org.gradle.caching.debug=(true,false)
    • When set to true, individual input property hashes and the build cache key for each task are logged on the console. Learn more about task output caching.
  • org.gradle.console=(auto,plain,rich,verbose)
    • Customize console output coloring or verbosity. Default depends on how Gradle is invoked. See command-line logging for additional details.
  • org.gradle.java.home=(path to JDK home)
    • Specifies the Java home for the Gradle build process. The value can be set to either a jdk or jre location, however, depending on what your build does, using a JDK is safer. A reasonable default is derived from your environment (JAVA_HOME or the path to java) if the setting is unspecified. This does not affect the version of Java used to launch the Gradle client VM (see Environment variables).
  • Or any other property you like
    • someProperty.subProperty = value
23
Q

How to reorder tasks in Gradle?

A

>test.mustRunAfter checkstyleMain, checkstyleTest

24
Q

How to reference property value that was defined in gradle.properties?

A

Simply be referencing property name

25
Q

What is the Project object?

A

This interface is the main API you use to interact with Gradle from your build file. From a Project, you have programmatic access to all of Gradle’s features.