Run Tasks for Affected Projects
Nx doesn’t just cache your task results, it can also eliminate the need to run unnecessary tasks.
First, commit any outstanding changes to the main
branch locally:
git commit -am "changes"
Next make a small change to the application
code:
package com.example.multimodule.application;
import com.example.multimodule.service.MyService;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication(scanBasePackages = "com.example.multimodule")@RestControllerpublic class DemoApplication {
private final MyService myService;
public DemoApplication(MyService myService) { this.myService = myService; }
@GetMapping("/") public String home() { return myService.message() + " changed!"; }
public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}
As a developer, we know that this change only affects the application
project, not the library
project. We would
run ./nx run application:test
to verify our changes. In CI, teams often run all test tasks rerunning
the library:test
task unnecessarily.
For a repository with only a few projects, you can manually calculate which projects are affected. As the repository grows, it becomes critical to have a tool like Nx that understands the project dependency graph and eliminates wasted time in CI.
The ./nx affected
command solves this problem. Nx uses its project graph in conjunction with git history to only run
tasks for projects that may have been affected by the changes that you made.
To run the test
tasks for projects affected by this change, run:
./nx affected -t test
Notice that this command does not run the test
task for the library
project, since it could not have been affected by the code change.