Gradle

Gradle is a popular build automation tool that can be used for a wide range of projects, from small scripts to large enterprise applications.

Basic Gradle Commands

  • gradle init: Initializes a new Gradle project in the current directory. This command will generate a basic build.gradle file and a project structure.

  • gradle build: Builds the project and produces an output in the build/ directory. This command compiles the source code, runs the tests, and generates the output.

  • gradle clean: Deletes the build/ directory. This command removes all the compiled output and test results.

  • gradle tasks: Displays a list of available tasks for the current project. This command shows a list of all the tasks that can be executed for the project.

Gradle Configuration

  • build.gradle file: This file is the main configuration file for your Gradle project. It defines the dependencies, plugins, and tasks for your project. The build.gradle file is written in Groovy or Kotlin.

Dependencies

  • dependencies {}: This block is used to declare the dependencies for the project. The most common dependency scopes are:

    • compile: Specifies dependencies that are required for compiling the code and for the project to run.

    • testCompile: Specifies dependencies that are required for testing the code.

    • implementation: Specifies dependencies that are required for the project to run but not for compiling the code.

Plugins

  • plugins {}: This block is used to declare the plugins for the project. Gradle provides many built-in plugins, such as java, application, war, and ear.

  • apply plugin: 'plugin-name': This command applies a plugin to the project.

Gradle Tasks

  • task <task-name> { ... }: This command creates a new task with the specified name and configuration. A task can be a simple script or a complex process that performs multiple actions.

  • dependsOn <task-name>: This command specifies that a task depends on another task. The dependent task will be executed before the specified task.

  • doFirst { ... }: This command executes a block of code before the task executes.

  • doLast { ... }: This command executes a block of code after the task executes.

Built-in Tasks

  • assemble: Compiles and packages the application code into a distributable format.

  • test: Runs the tests for the project.

  • check: Runs both the test task and other verification tasks.

  • build: Runs the assemble and check tasks.

  • clean: Deletes the build/ directory.

Gradle Build Scripts

  • settings.gradle file: This file is used to define the Gradle settings for the project, such as the project name and the project hierarchy.

  • gradle.properties file: This file is used to define the Gradle properties for the project, such as the Java version and the project version.

  • buildSrc directory: This directory contains the build scripts that are used by the project. The build scripts can be written in Groovy or Kotlin and can be used to define custom tasks or to configure the build process.

Last updated