Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Gradle Init Script to Documentation? #106

Open
handstandsam opened this issue Feb 14, 2024 · 0 comments
Open

Add Gradle Init Script to Documentation? #106

handstandsam opened this issue Feb 14, 2024 · 0 comments
Labels
documentation Improvements or additions to documentation

Comments

@handstandsam
Copy link
Collaborator

handstandsam commented Feb 14, 2024

The configuration phase to appropriately run dependency-guard is very expensive. An alternate option to having it "always installed" is to run it via a Gradle init script.

In this example, it will run the dependencyGuardBaseline tasks for all projects that have the plugin applied. The code iterates through all projects and only applies it to those who are com.android.library or com.android.application. This can be modified as desired.

dependency-guard.gradle

/**
 * Gradle init-script which applies the plugin to existing Gradle projects.
 *
 * Run it with the following:
 * ./gradlew --init-script dependency-guard.gradle dependencyGuardBaseline
 */
settingsEvaluated {
  rootProject {
    buildscript {
      repositories {
        mavenCentral()
        gradlePluginPortal()
        google()
        mavenLocal()
      }

      dependencies {
        classpath("com.dropbox.dependency-guard:dependency-guard:0.4.3")
      }
    }
    afterEvaluate {
      allprojects {
        project.afterEvaluate {
          // filtering for android library and app plugins in this case, but you could change this logic as needed.
          if (plugins.findPlugin("com.android.library") != null || plugins.findPlugin("com.android.application") != null) {
            plugins.apply("com.dropbox.dependency-guard")
            dependencyGuard {
              configuration("debugRuntimeClasspath") {
                modules = true
              }
            }
          }
        }
      }
    }
  }
}

I can then just run ./gradlew --init-script dependency-guard.gradle dependencyGuardBaseline

Additionally I can target a project specifically with./gradlew --init-script dependency-guard.gradle :app:dependencyGuard if desired. (to avoid configuring all projects when configuration on demand is enabled)

@handstandsam handstandsam added the documentation Improvements or additions to documentation label Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant