@nx/gradle

Gradle is a fast, dependable, and adaptable open-source build automation tool with an elegant and extensible declarative build language. Gradle supports Android, Java, Kotlin Multiplatform, Groovy, Scala, Javascript, and C/C++.

The Nx Gradle plugin registers Gradle projects in your Nx workspace. It allows Gradle tasks to be run through Nx. Nx effortlessly makes your CI faster.

Nx adds the following features to your workspace:

Java Compatibility

This plugin requires Java 17 or newer. Using older Java versions is unsupported and may lead to issues. If you need support for an older version, please create an issue on Github!

Setup @nx/gradle

Install Nx

You can install Nx globally. Depending on your package manager, use one of the following commands:

brew tap nrwl/nx

brew install nx

Add Nx to a Gradle Workspace

In any Gradle workspace, run the following command to add Nx and the @nx/gradle plugin:

nx init

Then, you can run Gradle tasks using Nx. For example:

nx build <your gradle library>

How @nx/gradle Infers Tasks

The @nx/gradle plugin relies on a companion Gradle plugin, dev.nx.gradle.project-graph, to analyze your Gradle build structure. When using nx add, the Gradle plugin is added as a dependency to the root Gradle build file. In most cases, the generator will add the task definition to trigger the plugin but if it's missing, add the following configuration to your Gradle configuration:

build.gradle.kts
1plugins { 2 id("dev.nx.gradle.project-graph") version("+") 3} 4 5allprojects { 6 apply { 7 plugin("dev.nx.gradle.project-graph") 8 } 9} 10

The dev.nx.gradle.project-graph plugin introduces a task named nxProjectGraph. This task analyzes your Gradle projects and their tasks, outputting the structure as JSON. The @nx/gradle plugin then uses this JSON data to accurately build the Nx project graph. If Nx has any issue generate the project graph JSON, you can run the nxProjectGraph task manually:

./gradlew nxProjectGraph

View Inferred Tasks

To view inferred tasks for a project, open the project details view in Nx Console or run nx show project my-project in the command line.

Setting Up @nx/gradle in a Nx Workspace

In any Nx workspace, you can install @nx/gradle by running the following command:

nx add @nx/gradle

@nx/gradle Configuration

The @nx/gradle is configured in the plugins array in nx.json.

nx.json
1{ 2 "plugins": [ 3 { 4 "plugin": "@nx/gradle", 5 "options": { 6 "testTargetName": "test", 7 "classesTargetName": "classes", 8 "buildTargetName": "build", 9 "ciTestTargetName": "test-ci" 10 } 11 } 12 ] 13} 14

Once a Gradle configuration file has been identified, the targets are created with the name you specify under testTargetName, classesTargetName or buildTargetName in the nx.json plugins array. The default names for the inferred targets are test, classes and build.

Splitting Tests

The @nx/gradle plugin will automatically split your testing tasks by test class if you provide a ciTestTargetName. You can read more about the Atomizer feature here. Nx will create a task with the name that you specify which can be used in CI to run the tests for each test class in a distributed fashion.

nx.json
1{ 2 "plugins": [ 3 { 4 "plugin": "@nx/gradle", 5 "options": { 6 "ciTestTargetName": "test-ci" 7 } 8 } 9 ] 10} 11

Continuous Tasks

Gradle doesn't have a standard way to identify tasks which are continuous, like bootRun for serving a Spring Boot project. To ensure Nx handles these continuous tasks correctly, you can explicitly mark them as continuous.

In the nx.json, you can specify the target default configuration like so:

nx.json
1{ 2 "targetDefaults": { 3 "someTask": { 4 "continuous": true 5 } 6 } 7} 8

Package reference

Here is a list of all the executors, generators and migrations available from this package.

Guides

Executors

  • gradle

    Runs gradle tasks via the Gradle Tooling API or by invoking gradlew.

Generators

  • initInternal

    Initializes a Gradle project in the current workspace

  • ci-workflow

    Setup a CI Workflow to run Nx in CI

Migrations

  • 21.0.x

  • change-ciTargetName-to-ciTestTargetName

    Change @nx/gradle option from ciTargetName to ciTestTargetName

    Version: 21.0.0-beta.13

    Change @nx/gradle plugin option ciTargetName to ciTestTargetName

    Change @nx/gradle plugin option ciTargetName to ciTestTargetName in nx.json

    Sample Code Changes

    nx.json
    1{ 2 "plugins": [ 3 "plugin": "@nx/gradle", 4 "options": { 5 "ciTargetName": "ci" 6 } 7 ] 8} 9

    change-plugin-to-v1

    Change @nx/gradle plugin to version 1

    Version: 21.0.0-beta.5

    Change @nx/gradle plugin to @nx/gradle/plugin-v1

    Change @nx/gradle plugin to version 1 in nx.json

    Sample Code Changes

    nx.json
    1{ 2 "plugins": ["@nx/gradle"] 3} 4
  • 20.2.x

  • add-include-subprojects-tasks

    Add includeSubprojectsTasks to build.gradle file

    Version: 20.2.0-beta.4

    Add includeSubprojectsTasks to @nx/gradle Plugin Options

    Add includeSubprojectsTasks to @nx/gradle plugin options in nx.json file

    Sample Code Changes

    nx.json
    1{ 2 "plugins": ["@nx/gradle"] 3} 4
  • 19.4.x

  • change-regex-production-test

    This function changes !{projectRoot}/test/**/* in nx.json for production to !{projectRoot}/src/test/**/*

    Version: 19.4.1-beta.0

    add-project-report-all

    Add task projectReportAll to build.gradle file

    Version: 19.4.0-beta.1