Skip to content

Gradle is a build automation tool for JVM-based projects.

The @nx/gradle plugin registers Gradle projects in the Nx graph so you can set up Gradle in Nx, run local tasks, configure task inference, and scale in CI.

You can use Gradle with Nx without the plugin and still get task caching, task orchestration, and the project graph.

  • Java 17 or newer

Install Nx with your preferred package manager:

Terminal window
npm install --global nx@latest

You can use pnpm, yarn, or bun if you prefer as well.

From the root of your Gradle workspace, run:

Terminal window
nx init

Nx will add the @nx/gradle plugin and configure the Gradle companion plugin dev.nx.gradle.project-graph in your build files.

List the projects Nx inferred from Gradle:

Terminal window
nx show projects

Open the project details view for a specific project.

Terminal window
nx show project my-app

Run Gradle tasks through Nx using inferred targets:

Terminal window
nx build my-gradle-lib
nx test my-gradle-lib

Nx maps these targets back to the Gradle tasks (for example build or test) and executes them using the Gradle wrapper. If a task should run in watch or server mode, mark it as continuous so Nx treats it correctly.

Nx relies on the dev.nx.gradle.project-graph Gradle plugin to report your Gradle projects and tasks. The plugin adds an nxProjectGraph task that outputs JSON, which Nx uses to build the project graph. Nx also scans your Gradle build files to locate project roots and inputs so it can infer targets accurately.

The nx init workflow adds this plugin to your Gradle build files. If you ever need to add it manually, configure it in build.gradle or build.gradle.kts and apply it to all projects.

To view inferred tasks for a project, open the project details view in Nx Console or run:

Terminal window
nx show project my-app

Replace my-app with your project name.

Configure the plugin in the plugins array of nx.json:

nx.json
{
"plugins": [
{
"plugin": "@nx/gradle",
"options": {
"testTargetName": "test",
"ciTestTargetName": "test-ci",
"targetNamePrefix": "gradle-",
"gradleExecutableDirectory": "./gradle-projects"
}
}
]
}

Nx passes the plugin options to the Gradle nxProjectGraph task as Gradle properties. Any option that ends with TargetName overrides the inferred Nx target name for that Gradle task. Use this to avoid name clashes or to introduce CI-specific targets.

OptionTypeDefaultDescription
testTargetNamestringtestNx target name to map the Gradle test task.
ciTestTargetNamestringnoneEnables CI test atomization and defines the base name for CI test targets.
targetNamePrefixstringnonePrefix for all inferred Gradle targets (useful in polyglot workspaces).
gradleExecutableDirectorystringnoneDirectory containing the Gradle wrapper to use.
<taskName>TargetNamestringnoneOverride for any Gradle task name (for example assembleTargetName).

Use include/exclude glob patterns to scope the plugin to specific projects:

nx.json
{
"plugins": [
{
"plugin": "@nx/gradle",
"include": ["apps/**/*"],
"exclude": ["apps/legacy-app/**/*"],
"options": { ... }
}
]
}

Individual Gradle projects can also configure Nx-specific settings using the nx { } DSL in build.gradle or build.gradle.kts.

Only run what changed in CI:

Terminal window
nx affected -t build test

Enable remote caching with Nx Cloud:

Terminal window
nx connect

See Set Up CI for provider-specific workflows. If you set ciTestTargetName, Nx will create CI variants that use atomized test targets.

Batch mode runs multiple Gradle tasks in a single invocation. Enable it by setting NX_BATCH_MODE=true:

Terminal window
NX_BATCH_MODE=true nx run-many -t build test

If your Gradle builds require specific CI flags (for example --no-daemon or custom JVM args), add them to gradle.properties so Nx runs consistent settings locally and in CI.