Gradle Flashcards
Key features of gradle?
- Gradle is a general-purpose build tool
- The core model is based on tasks
- 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.
- Gradle has several fixed build phases
- Gradle is extensible in more ways than one
- 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.
What do tasks consist of?
- Actions
- pieces of work that do something, like copy files or compile source
- Inputs
- values, files and directories that the actions use or operate on
- Outputs
- 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
What are the build phases of Gradle?
- Initialization
- Sets up the environment for the build and determine which projects will take part in it.
- Configuration
- 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.
- Execution
- Runs the tasks selected at the end of the configuration phase.
How does gradle can be extended?
- 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.
*
- 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.
What does gradle projects command do?
Displays the sub-projects of root project if there are any?
What does gradle tasks command do?
Displays the tasks runnable from root project.
How to get a task description?
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 does the gradle can be debugged?
gradle help -Dorg.gradle.debug=true
will allow you to set breakpoints and attach debugger?
How does compare maven to gradle?
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).
Maven build phases vs gradle lifecycle tasks?
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 to initialize gradle project?
> gradle init
Managing dependencies strategies Maven vs Gradle?
Maven uses a “closest” (to the root) match algorithm, whereas Gradle picks the newest.
How to declare a dependency in Gradle?
configuration ‘group:artifact:version’
dependencies {
implementation ‘log4j:log4j:1.2.12’ ①
}
Gradle configurations mapping to Maven scopes?
- compile
- 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.
- runtime
- Use the runtimeOnly configuration
- test
- Gradle distinguishes between those dependencies that are required to compile a project’s tests and those that are only needed to run them.
- Dependencies required for test compilation should be declared against the testImplementation
- Those that are only required for running the tests should use testRuntimeOnly
- Gradle distinguishes between those dependencies that are required to compile a project’s tests and those that are only needed to run them.
- provided
- Use the compileOnly configuration.
What is the default maven scope?
It is compile