Gradle Tutorial

In this tutorial, you’ll learn how to add Nx to a repository with an existing Gradle setup.

What will you learn?

  • how to add Nx to a Gradle project
  • how to run a single task (i.e. serve your app) or run multiple tasks in parallel
  • how to leverage code generators to scaffold components
  • how to modularize your codebase and impose architectural constraints for better maintainability
  • how to speed up CI with Nx Cloud ⚡

Prerequisites

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:

Terminal window
gradle --version

To streamline this tutorial, we’ll install Nx globally on your system. You can use Homebrew (Mac only) or a manually installed Node version (any OS).

Homebrew

If installing Nx with Homebrew, make sure Homebrew is installed, then install Nx globally with these commands:

Terminal window
brew tap nrwl/nx
brew install nx
Node

If installing Nx with Node, install node from the NodeJS website, then install Nx globally with this command:

Terminal window
npm install --global nx

Getting Started

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:

Example repository/nrwl/gradle-tutorial

Terminal window
git clone https://github.com/<your-username>/gradle-tutorial.git

The Multi-Module Spring Tutorial left us with 2 projects:

  • The main application project which contains the Spring DemoApplication
  • A library project which contains a Service used in the DemoApplication

You can see the above 2 projects by running ./gradlew projects

Terminal window
./gradlew projects
> Task :projects
------------------------------------------------------------
Root project 'gradle-tutorial'
------------------------------------------------------------
Root project 'gradle-tutorial'
+--- Project ':application'
\--- Project ':library'
Powered by WebContainers