@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:

Add Nx to a Gradle Workspace

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

npx nx@latest init

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

./nx build <your gradle library>

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

How @nx/gradle Infers Tasks

The @nx/gradle plugin will create an Nx project for each Gradle configuration file present. Any of the following files will be recognized as a Gradle configuration file:

  • build.gradle
  • build.gradle.kts

@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 "ciTargetName": "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 E2E Tests

The @nx/gradle plugin will automatically split your e2e tasks by file if you provide a ciTargetName. You can read more about the Atomizer feature here. This will create a target with that name which can be used in CI to run the tests for each file in a distributed fashion.

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

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 --web in the command line.

Package reference

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

Guides

Executors

Generators

  • init

    Initializes a Gradle project in the current workspace

  • ci-workflow

    Setup a CI Workflow to run Nx in CI