Skip to content
joshfriendPublic

About

A gradle plugin to make management of large gradle builds easier

Resources

Stars

160 stars

Watchers

2 watching

Forks

Repository files navigation

Spotlight

Maven Central Version Gradle Plugin Portal Version IDE Plugin Version License: MIT

A Gradle plugin and IntelliJ plugin that make managing settings.gradle(.kts) for large projects easier:

  • Moves your include directives out of settings.gradle(.kts) into a flat text file
  • Inspired by tools like Focus and dependency-explorer, this plugin lets you easily load subsets of your project into the IDE
  • Only loads the minimum required list of projects required to run your requested build tasks

Tip

Using Spotlight can dramatically decrease your IDE sync times, check out this blog post for more info.

How to use it

Apply the plugin in settings.gradle(.kts):

plugins {
  id 'com.fueledbycaffeine.spotlight'
}

Create gradle/all-projects.txt with your project declarations:

:app
:feature-a
:feature-b
# items may be commented out like this

Tip

If you have an existing project with include statements in settings.gradle(.kts), you can run ./gradlew :fixAllProjectsList to automatically migrate them to all-projects.txt and remove them from your settings file.

IDE Sync

To load a subset of your project in the IDE, list the projects you want to work on in gradle/ide-projects.txt:

:feature-a
...

On next sync, only :feature-a and other projects you choose will be be loaded. The transitive dependencies of these target projects are also loaded (because sync will fail otherwise), but you do not need to identify them yourself. The plugin statically parses your build.gradle(.kts) files at the start of the build to determine the dependency graph.

Task Execution

Spotlight will infer the projects necessary to include in the build from the task request list and the project directory (specified with the -p/--project-dir flags).

For example, running ./gradlew :help will only add the root project. Running something like ./gradlew :example:assemble will only add the projects required by the dependency graph of :example.

When the -p/--project-dir flag is used, Spotlight will expand the list of child projects at the specified directory and add the projects required by their dependency graphs to the build. This enables you to run something like ./gradlew -p example check in a subdirectory.

Implicit rules

It is not uncommon for a conventions plugin setup to add a default set of utilities/testing project dependencies to each project in a build. By default, Spotlight is not able to detect these implicit dependencies added to your projects by other build logic or plugins because those do not appear in your buildscripts.

A config file option is provided to configure some pattern matching rules based on project paths or buildscript contents to implicitly add other projects:

// gradle/spotlight-rules.json
{
  "implicitRules": [
    // Add :tsunami-sea as a dependency to all projects with a path matching ":rotoscope:.*"
    // The pattern strings are regexes
    {
      "type": "project-path-match-rule",
      "pattern": ":rotoscope:.*",
      "includedProjects": [
        ":tsunami-sea"
      ]
    },
    // Add :eternal-blue to any project applying the `com.example.android` convention plugin
    {
      "type": "buildscript-match-rule",
      "pattern": "id 'com.example.android'",
      "includedProjects": [
        ":eternal-blue",
        ":singles-collection"
        // multiple includes can be given for a pattern
      ]
    },
    // Extract project paths dynamically using regex capture groups
    // This example captures the targetProjectPath from `com.android.test` projects
    // The projectTemplate value can also transform the captured values if needed.
    {
      "type": "buildscript-capture-rule",
      "pattern": "targetProjectPath\\s*=\\s*[\"']([^\"']+)[\"']",
      "projectTemplate": "$1"
    }
  ]
}

Implicit rules apply to all Gradle invocations (sync and task execution).

If you are using the buildscript-utils package by itself, you can read this rules list using the SpotlightRulesList class.

Task invocation rules

Some plugins register tasks that operate on projects Spotlight cannot discover by parsing buildscripts. For example, Dependency Analysis Gradle Plugin's :buildHealth root project task aggregates results from every project in the build, but the root project's buildscript doesn't necessarily declare a dependency on every other project in the build.

Task invocation rules include additional projects (or all of them) when specific tasks are requested on the command line:

// gradle/spotlight-rules.json
{
  "taskInvocationRules": [
    // Include all projects when the root :buildHealth task is invoked
    {
      "taskNames": [":buildHealth"],
      "includeAllProjects": true
    },
    // Include specific projects when a task is invoked
    {
      "taskNames": ["generateProtos"],
      "includedProjects": [
        ":proto-common"
      ]
    }
  ]
}

The fully qualified :buildHealth entry only matches invocations that select the root project's task, not :other:buildHealth.

Fully qualified task paths are also useful when an aggregating task is registered on a dedicated project. A rule declaring :reports:aggregateReports matches ./gradlew -p reports aggregateReports because Gradle task selectors are resolved relative to the default project. A root invocation of ./gradlew aggregateReports already includes all projects through Spotlight's normal unqualified-task behavior, so it does not need a task invocation rule.

Unqualified entries continue to match at any project path, so ./gradlew :foo:buildHealth matches a rule declaring buildHealth. All entries are matched exactly, so abbreviated invocations (./gradlew :bH) will not match a rule declaring buildHealth.

Type-safe Project Accessors

Spotlight automatically parses type-safe project accessors in your buildscripts and maps them to the corresponding project paths. This works with any project naming convention.

Important

Don't use type-safe project accessors with Kotlin buildscripts. Doing so causes more configuration cache misses when Spotlight is being used.

Useful Tasks

Spotlight provides several tasks for managing its config files:

  • ./gradlew :checkAllProjectsList - Check that the all-projects.txt file is correct
    • Verifies alphabetic sorting
    • Verifies that settings.gradle(.kts) does not have any includes
    • Validates that all listed projects have build files
    • Ensures all projects found on disk or through dependencies are listed
  • ./gradlew :fixAllProjectsList - Auto-fix issues in the all-projects.txt file
    • Migrates any include statements from settings.gradle(.kts) to all-projects.txt
    • Removes invalid projects (those without build files)
    • Adds missing projects found on disk or through dependencies
    • Sorts the file alphabetically

Directory scanning

Both tasks scan directories for build.gradle or build.gradle.kts files belonging to projects absent from all-projects.txt. Configure this scan in settings.gradle(.kts) through the Spotlight DSL. The same configuration applies to both check and fix tasks.

spotlight {
  // ...
  projectDiscovery {
    directoryScan {
      discoverUnlistedProjects true
      excludeProjectPaths ':optional-tool:public', ':another-project'
      excludeDirectoriesMatchingRegex 'generated/.*', 'fixtures/.*'
    }
  }
}
  • discoverUnlistedProjects defaults to true. Set it to false to disable only the directory scan. Dependency-graph traversal always runs: required projects are still checked and added by the fix task.
  • excludeProjectPaths excludes exact Gradle project paths from the scan.
  • excludeDirectoriesMatchingRegex matches the entire root-relative directory path, using / separators and no trailing slash. Matching directories and their descendants are skipped. For example, generated/.* excludes generated/example, but not tools/generated/example.

Tip

Both tasks fail if an excluded project is required by the dependency graph.

The scan also skips directories named build, buildSrc, src, src-gen, or tmp, and nested builds identified by their own settings.gradle(.kts) file.

Custom Parsers

Spotlight uses an extensible parser system to extract project dependencies from your buildscripts. By default, it uses a regex-based parser (RegexBuildscriptParser), but you can provide your own parser implementations for more sophisticated parsing strategies (e.g., AST-based parsing, PSI-based parsing, or custom DSL support).

Custom parsers are discovered via Java's ServiceLoader mechanism. All parsers are additive and their results are combined together.

1. Implement the BuildscriptParser interface:

package com.example.spotlight

object MyCustomParser : BuildscriptParser {
  override fun parse(
    project: GradlePath,
    rules: Set<DependencyRule>
  ): Set<GradlePath> {
    // Your parsing logic here
    // Return the set of project dependencies found
    return emptySet()
  }
}

2. Implement the BuildscriptParserProvider interface:

package com.example.spotlight

class Provider : BuildscriptParserProvider {
  override fun getParser(): BuildscriptParser = MyCustomParser
}

3. Register your provider via ServiceLoader:

Create a file at src/main/resources/META-INF/services/com.fueledbycaffeine.spotlight.buildscript.parser.impl.BuildscriptParserProvider with the fully qualified class name of your provider:

com.example.spotlight.MyCustomParserProvider

4. Add your parser library to your buildscript dependencies (or the classpath of whatever else is running buildscript-utils):

// settings.gradle(.kts)
buildscript {
  dependencies {
    classpath("com.example:spotlight-custom-parser:1.0.0")
  }
}

Your custom parser will now be automatically discovered and run alongside the built-in parsers.

Differences from Focus

Unlike Focus, which configures your gradle project to select which projects get synced using the :createFocusSettings task provided by the plugin, Spotlight relies on parsing of your buildscripts with regexes to compute the dependency graph, which is much faster.

If your build graph changes, you need to rerun Focus analysis manually to avoid broken sync. Spotlight quickly computes the required projects on every Gradle invocation.

Differences from dependency-explorer

dependency-explorer runs completely outside Gradle, and users must rerun the build graph query whenever their dependency graph changes to avoid errors during IDE sync. Both dependency-explorer and Spotlight use similar approaches for parsing the build graph, but Spotlight just does it automatically inside a settings plugin.

Limitations

This plugin assumes you have a "nice" Gradle build that doesn't do cross-project configuration. Please read "Herding Elephants" and "Stampeding Elephants" for more thoughts on this topic.

Dependency declarations in buildscripts must be formatted to each be on a single line. The following example would not be parsed by the regexes used:

dependencies {
  implementation(
    project(
      ':feature-a'
    )
  )
}

The projectDir for projects listed in all-projects.txt cannot be relocated because the list of all your projects is now a flat text file and not a dynamic script.

When directory scanning is enabled, projects conditionally included by applied scripts must still be listed or explicitly excluded, even when their inclusion condition is false. :checkAllProjectsList rejects direct include statements in settings.gradle(.kts) regardless of the directory scan setting.

About

A gradle plugin to make management of large gradle builds easier

Resources

Stars

160 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages