This tutorial walks you through adding Nx to an existing Gradle project. You'll see how Nx enhances your Gradle workflow with caching, task orchestration, and better developer experience.
What you'll learn:
- How to integrate Nx with your existing Gradle build system
- How Nx caching speeds up your Gradle builds locally and in CI
- How to visualize and understand project dependencies in your Gradle workspace
- How to run Gradle tasks more efficiently with Nx task runner
- How to set up Nx Cloud for remote caching
Ready to start?
Section titled βReady to start?βPrerequisites: this tutorial requires a GitHub account to demonstrate the full value of Nx, including task running, caching, and CI integration.
Step 1: Setup local env
Section titled βStep 1: Setup local envβMake sure that you have Gradle installed on your system. Consult Gradle's installation guide for instruction that are specific to your operating system.
To verify that Gradle was installed correctly, run this command:
gradle --versionTo streamline this tutorial, we'll install Nx globally on your system. You can use your preferred installation method below based on your OS:
Homebrew (macOS, Linux)
Section titled βHomebrew (macOS, Linux)βMake sure Homebrew is installed, then install Nx globally with these commands:
brew install nxChocolatey (Windows)
Section titled βChocolatey (Windows)βchoco install nxapt (Ubuntu)
Section titled βapt (Ubuntu)βsudo add-apt-repository ppa:nrwl/nxsudo apt updatesudo apt install nxNode (any OS)
Section titled βNode (any OS)βInstall node from the NodeJS website, then install Nx globally with this command:
npm install --global nxStep 2: Fork sample repository
Section titled βStep 2: Fork sample repositoryβThis tutorial picks up where Spring framework's guide for Multi-Module Projects leaves off.
Fork the sample repository, and then clone it on your local machine:
git clone https://github.com/<your-username>/gradle-tutorial.gitThe Multi-Module Spring Tutorial left us with 2 projects:
- The main
applicationproject which contains the SpringDemoApplication - A
libraryproject which contains a Service used in theDemoApplication
You can see the above 2 projects by running ./gradlew projects
> Task :projects
------------------------------------------------------------Root project 'gradle-tutorial'------------------------------------------------------------
Root project 'gradle-tutorial'+--- Project ':application'\--- Project ':library'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. Explore the features of Nx by adding it to the Gradle workspace above.
To add Nx, run
nx initThis command will download the latest version of Nx and help set up your repository to take advantage of it. Nx will also detect Gradle is used in the repo so it will propose adding the @nx/gradle plugin to integrate Gradle with Nx.
- You'll also be prompted to add Nx Cloud, select "yes"
- Select the plugin and continue with the setup.
Finally, commit and push all the changes to GitHub and proceed with finishing your Nx Cloud setup.
Finish Nx Cloud setup
Section titled βFinish Nx Cloud setupβNx Cloud provides remote caching and many other features. Learn more about Nx Cloud features.
Click the link printing in your terminal, or you can finish setup in Nx Cloud
Verify your setup
Section titled βVerify your setupβPlease verify closely that you have the following setup:
- A new Nx workspace on your local machine
- A corresponding GitHub repository for the workspace
- You completed the full Nx Cloud onboarding, and you now have a Nx Cloud dashboard that is connected to your example repository on GitHub.
You should see your workspace in your Nx Cloud organization.
Explore your workspace
Section titled βExplore your workspaceβLike Gradle, Nx understands your workspace as a graph of projects. Nx uses this graph for many things which we will learn about in following sections. To visualize this graph in your browser, Run the following command and click the "Show all projects" button in the left sidebar.
You will recognize that the projects which are shown, are the same projects which Gradle shows. The @nx/gradle plugin reflects the graph of projects in Gradle into the Nx Project Graph. As projects are created, deleted, and change their dependencies, Nx will automatically recalculate the graph. Exploring this graph visually is vital to understanding how your code is structured and how Nx and Gradle behaves.
nx graphRunning tasks
Section titled βRunning tasksβNx is a task runner built for monorepos. It can run a single task for a single project, a task for all projects, and even intelligently run a subset of tasks based on the changes you've made in your repository. Nx also has sophisticated Computation caching to reuse the results of tasks. Explore how Nx adds to the task running Gradle provides.
Before we start running tasks, let's explore the tasks available for the application project. The @nx/gradle plugin that we've installed reflects Gradle's tasks to Nx, which allows it to run any of the Gradle tasks defined for that project. You can view the available tasks either through Nx Console or from the terminal:
nx show project applicationThe Nx command to run the build task for the application project is:
nx run application:buildWhen Nx runs a Gradle task, it hands off the execution of that task to Gradle, so all task dependencies and configuration settings in the Gradle configuration are still respected.
By running the task via Nx, however, the task computation was cached for reuse. Now, running nx run application:build again, will complete almost instantly as the result from the previous execution will be used.
β 1/1 dependent project tasks succeeded [1 read from cache]
Hint: you can run the command with --verbose to see the full dependent project outputs
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
> nx run application:classes [existing outputs match the cache, left as is]
> ./gradlew :application:classes
> Task :library:compileJava UP-TO-DATE> Task :library:processResources NO-SOURCE> Task :library:classes UP-TO-DATE> Task :library:jar UP-TO-DATE> Task :application:compileJava UP-TO-DATE> Task :application:processResources UP-TO-DATE> Task :application:classes UP-TO-DATE
BUILD SUCCESSFUL in 647ms4 actionable tasks: 4 up-to-date
> nx run application:build [existing outputs match the cache, left as is]
> ./gradlew :application:build
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.5/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 768ms9 actionable tasks: 1 executed, 8 up-to-date
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
NX Successfully ran target build for project application and 3 tasks it depends on (30ms)
Nx read the output from the cache instead of running the command for 4 out of 4 tasks.Now that we've run one task, let's run all the build tasks in the repository with the Nx run-many command. This is similar to Gradle's ./gradlew build command.
β nx run library:classes [existing outputs match the cache, left as is] β nx run library:build [existing outputs match the cache, left as is] β nx run application:classes [existing outputs match the cache, left as is] β nx run application:build [existing outputs match the cache, left as is] β nx run gradle-tutorial:classes (1s) β nx run gradle-tutorial:build (1s)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
NX Successfully ran target build for 3 projects and 3 tasks they depend on (2s)
Nx read the output from the cache instead of running the command for 4 out of 6 tasks.Again, because Nx cached the tasks when the application was built, most of the tasks here were near instant. The only ones which needed to be done is the root project's build. Running the command one more time, will be near instant as then all the tasks will be restored from the cache.
Remote cache for faster development
Section titled βRemote cache for faster developmentβWith Nx Cloud connected, your task results are now cached remotely. This means that if another developer runs the same tasks, or if you run them in CI, the results will be retrieved from the remote cache instead of being re-executed.
Try running the build again after making a small change to see how Nx intelligently determines which tasks need to be re-run:
nx run-many -t buildYou'll notice that only the affected projects need to rebuild, while others are restored from cache.
Summary
Section titled βSummaryβNow that you have added Nx to this sample Gradle repository, you have learned several ways that Nx can help your organization:
- Nx reflects the Gradle graph into the Nx graph
- Nx dependency graph visualisation helps you understand your codebase
- Nx caches task results and reuses them when the same task is rerun later
- Nx Cloud provides remote caching to speed up local development and CI
- Nx intelligently determines which tasks are
affectedby code changes to reduce waste in CI
Next steps
Section titled βNext stepsβConnect with the rest of the Nx community with these resources:
- βοΈ Star us on GitHub to show your support and stay updated on new releases!
- Join the Official Nx Discord Server to ask questions and find out the latest news about Nx.
- Follow Nx on Twitter to stay up to date with Nx news
- Read our Nx blog
- Subscribe to our Youtube channel for demos and Nx insights