惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
The Cloudflare Blog
IT之家
IT之家
V
V2EX
雷峰网
雷峰网
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
Stack Overflow Blog
Stack Overflow Blog
博客园 - Franky
Engineering at Meta
Engineering at Meta
S
SegmentFault 最新的问题
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
小众软件
小众软件
博客园 - 叶小钗
Blog — PlanetScale
Blog — PlanetScale
C
Check Point Blog
A
About on SuperTechFans
B
Blog
月光博客
月光博客
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI

Nx Blog

Sharing Tailwind CSS Styles Across Apps in a Monorepo | Nx Blog How SiriusXM Stays Competitive by Iterating and Getting to Market Fast | Nx Blog Agentic Experience Is the New Developer Experience | Nx Blog Nx Joins the Linux Foundation and the Agentic AI Foundation | Nx Blog A Monorepo Is NOT a Monolith | Nx Blog Why we deleted (most of) our MCP tools | Nx Blog Teach Your AI Agent How to Work in a Monorepo | Nx Blog How Broadcom stays efficient and nimble with monorepos | Nx Blog Why Monorepos are King in the Age of AI | Nx Blog Nx 2026 Roadmap: Expanding Agent Autonomy, Improving Performance, Better Polyglot and More | Nx Blog End to End Autonomous AI Agent Workflows with Nx | Nx Blog Autonomous Agents at Scale | Nx Blog Scaling 700+ Projects: How Nx Became a 'No-Brainer' for Caseware | Nx Blog Configure Tailwind v4 with Angular in an Nx Monorepo | Nx Blog The Missing Multiplier for AI Agent Productivity | Nx Blog A Year of Nx Webinars | Nx Blog Wrapping Up 2025 | Nx Blog Nx 22.3 Release: Angular 21 Support, tsgo Compiler, and Prettier v3 | Nx Blog Nx Cloud Release: Agent Resource Usage | Nx Blog Nx Platform Outperforms DIY Cache by 5x | Nx Blog An Nx Carol: Past, Present, and Future of Your Monorepo | Nx Blog Nx 22.1 Release: Terminal UI on Windows, Storybook 10, Vitest 4, and more! | Nx Blog The Compounding Effect: How Nx Features Multiply Performance Gains | Nx Blog 10 Monorepo Myths Debunked: Separating Fact from Fiction | Nx Blog Nx Cloud Release: Enterprise Task Analytics | Nx Blog Watch and Rebuild Storybook Dependencies with Nx | Nx Blog Book - React for Enterprise: Timeless Architecture for Enterprise Apps | Nx Blog Beyond Remote Cache: Unlock 70% More CI Performance | Nx Blog Nx 22 Release: Expanding the build platform | Nx Blog What's the Point of Generating All This Code If You Can't Merge It? | Nx Blog
Manage Your Gradle Project using Nx | Nx Blog
Emily Xiong · 2024-04-20 · via Nx Blog

Here's my situation: I have a Gradle workspace with multiple Gradle libraries. How do I easily view the relationships between different libraries? I have a monorepo workspace with both Gradle and Javascript libraries, how do I manage these libraries of different tech stacks?

We are very excited to announce our support for Gradle with our new plugin: @nx/gradle.

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

Update: Learn how to import your existing Gradle projects into Nx in our recent blog post.

This blog will show you:

  • What is Nx?
  • How to add Nx to a Gradle workspace
  • How to add @nx/gradle to an existing Nx workspace

What is Nx?

Before we start, let's answer this question: what is Nx and why should we use it?

From nx.dev: "Nx is a monorepo platform with built-in tooling and advanced CI capabilities. It helps you maintain and scale monorepos, both locally and on CI." It sounds good, what benefits does it bring?

Nx adds the following features to your workspace:

  • Cache task results: By storing task outputs in a cache, subsequent runs can skip redundant computations and reuse previously calculated results, significantly speeding up build processes. Nx intelligently manages this caching mechanism, invalidating the cache automatically when relevant inputs change.
  • Distribute task execution: Nx CI efficiently distributes tasks across multiple machines for faster build times. It uses a distributed task execution algorithm to intelligently divide and assign tasks to available resources, minimizing redundant work and maximizing parallelism.
  • Run only tasks affected by a PR: Nx identifies changes made since a specified base commit or branch, and then selectively runs tasks (like tests, linting, or builds) related to those changes.
  • Interactively explore your workspace: Nx allows developers to visualize and understand the dependencies and relationships within their projects.

Example Nx Graph


How to add Nx to a Gradle Workspace?

Now we understand the benefits of Nx, now let's set it up. The setup is pretty easy, just need to run one command.

In the workspace, run the below command:

In the terminal, it should output:

Setting Nx up installation in `.nx`. You can run Nx commands like: `./nx --help`
CREATE nx.json
UPDATE .gitignore
CREATE .nx/nxw.js
CREATE nx.bat
CREATE nx

 NX   Recommended Plugins:

Add these Nx plugins to integrate with the tools used in your workspace.

✔ Which plugins would you like to add? Press <Space> to select and <Enter> to submit. · @nx/gradle


added 111 packages, and audited 112 packages in 2s

21 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

✔ Installing @nx/gradle@latest...
✔ Initializing @nx/gradle...

That's it! Now we have Nx in our Gradle project.

Example: Gradle Init

If you create your Gradle workspace using gradle init, by running npx nx@latest init, you will get:

It adds a .nx folder in the workspace root and nx executable files. Now you can run commands using ./nx (or nx.bat for Windows machines).

For example, you can run Gradle tasks using Nx now:

# macos/linux
./nx <gradle task> <gradle project>

# windows
nx.bat <gradle task> <gradle project>

If you build your Gradle library using the command ./gradlew :app:build or gradlew.bat :app:build, you can run ./nx build app or nx.bat build app to build your Gradle library.

Now you can an alternative way to run Gradle tasks, how can we leverage Nx?

Nx Graph

To see the project graph, run the below command:

# macos/linux
./nx graph

# windows
nx.bat graph

Run Only Tasks Affected by a PR

As mentioned before, Nx enables the ability to run only the tasks affected by a specific PR by running the below command:

# macos/linux
./nx affected -t <task>

# windows
nx.bat affected -t <task>

For example, when you run nx affected -t build, Nx uses your git information to determine the files you changed in your PR. Nx determines the list of projects in the workspace that can be affected by this change and only runs the build task against changed files.

You can also visualize the affected projects highlighted using the Nx graph. Simply run:

# macos/linux
./nx affected:graph

# windows
nx.bat affected:graph

Nx Console

Furthermore, instead of running the command in terminal, you can use the editor tool Nx Console. Anything you can do with Nx, you can do with Nx Console.

Screencap of Nx Console UI

To download:


How to add @nx/gradle to an existing Nx workspace?

If you have an existing Nx workspace, to add @nx/gradle, just run:

That is it, it will add @nx/gradle plugin to your Nx workspace.

You can view inferred tasks for Gradle project in your workspace, open the project details view in Nx Console or run nx show project my-project --web in the command line.

For all the interred tasks, you can run using Nx instead of Gradle:

nx <gradle task> <gradle project> [options]

For example, if you run ./gradlew :app:build or gradlew.bat :app:build using Gradle command, to run using Nx: nx build app.

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:

{
  "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.


Summary

Here is how to set up Nx with the Gradle workspace. Hopefully, this gives you a good insight into how to get started with Gradle with Nx. The plugin is currently experimental, you can submit GitHub issues: https://github.com/nrwl/nx/issues.


Learn more