2024-04-09 15:51:18 -04:00

83 lines
2.5 KiB
Markdown

---
title: Overview of the Nx Gradle Plugin
description: The Nx Plugin for Gradle allows Gradle tasks to be run through Nx.
---
[Gradle](https://gradle.org/) 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](/ci/intro/ci-with-nx).
Nx adds the following features to your workspace:
- [Cache task results](/features/cache-task-results)
- [Distribute task execution](/ci/features/distribute-task-execution)
- [Run only tasks affected by a PR](/ci/features/affected)
- [Interactively explore your workspace](/features/explore-graph)
## Add Nx to a Gradle Workspace
In any Gradle workspace, run the following command to add Nx and select @nx/gradle:
```shell {% skipRescope=true %}
npx nx@latest init
```
Then, you can run Gradle tasks using Nx. For example:
{% tabs %}
{% tab label="Mac/Linux" %}
```shell {% skipRescope=true %}
./nx build <your gradle library>
```
{% /tab %}
{% tab label="Windows" %}
```shell {% skipRescope=true %}
nx.bat build <your gradle library>
```
{% /tab %}
{% /tabs %}
## Setting Up @nx/gradle in a Nx Workspace
In any Nx workspace, you can install `@nx/gradle` by running the following command:
```shell {% skipRescope=true %}
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:
- `gradle.build`
- `gradle.build.kts`
### @nx/gradle Configuration
The `@nx/gradle` is configured in the `plugins` array in `nx.json`.
```json {% fileName="nx.json" %}
{
"plugins": [
{
"plugin": "@nx/gradle",
"options": {
"testTargetName": "test",
"classesTargetName": "classes",
"buildTargetName": "build"
}
}
]
}
```
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`.
## View Inferred Tasks
To view inferred tasks for a project, open the [project details view](/features/explore-graph#explore-projects-in-your-workspace) in Nx Console or run `nx show project my-project --web` in the command line.