Cliabhach
Cliabhach

Reputation: 1550

How can I get a list of all configurations for a Gradle project?

I'm trying to get a list of all valid values for the --configuration flag of the dependencyInsight or dependencies gradle tasks. How would I go about doing this with Gradle 3.2.1?

Upvotes: 96

Views: 72229

Answers (11)

Alexey Semenyuk
Alexey Semenyuk

Reputation: 704

Add the following snippet to one of your projects:

allprojects {
  // For debugging
  tasks.register('printConfigurations') {
    doLast {
      if (!configurations.empty) {
        println "==="
        println "Configurations of ${project.path} project"
        println "==="
        configurations.all {
          println "${name}${canBeResolved ? '' : ' resolvable'}${canBeConsumed ? '' : ' consumable'}${canBeDeclared ? '' : ' scope'}"
          extendsFrom.each {
            println "  ${it.name}"
          }
        }
      }
    }
  }
}

Run gradlew printConfigurations

Output:

===
Configurations of :foo project
===
annotationProcessor consumable
apiElements resolvable scope
archives resolvable scope
compileClasspath consumable scope
  compileOnly
  implementation
compileOnly resolvable consumable
default resolvable scope
  runtimeElements
implementation resolvable consumable
mainSourceElements resolvable scope
  implementation
runtimeClasspath consumable scope
  runtimeOnly
  implementation
runtimeElements resolvable scope
  implementation
  runtimeOnly
runtimeOnly resolvable consumable
testAnnotationProcessor consumable
testCompileClasspath consumable scope
  testCompileOnly
  testImplementation
testCompileOnly resolvable consumable
testImplementation resolvable consumable
  implementation
testResultsElementsForTest resolvable scope
testRuntimeClasspath consumable scope
  testRuntimeOnly
  testImplementation
testRuntimeOnly resolvable consumable
  runtimeOnly

Not fancy, but fills that gap between the standard outgoingVariants, dependencies, and dependencyInsight tasks.

Upvotes: 0

Mahozad
Mahozad

Reputation: 24542

This is the Kotlin DSL (build.gradle.kts) equivalent of other answers:

configurations.forEach(::println)

Put the above statement at the top of your build.gradle.kts file. It will print something like below whenever you run any task (like build):

configuration ':app:androidApis'
configuration ':app:androidJdkImage'
configuration ':app:androidTestAnnotationProcessor'
...

You can also create a dedicated task for this:

tasks.register("myConfigs") {
    doLast {
        configurations.forEach { println(it) }
    }
}

Run the task from the command line like this:

./gradlew myConfigs

Upvotes: 7

Ivan Bartsov
Ivan Bartsov

Reputation: 21066

gradle proj:resolvableConfigurations | grep "^Configuration"

And you probably want the runtimeClasspath configuration for :dependencyInsight.

Explanation:

There's actually a built-in task resolvableConfigurations (since Gradle 7.5), so there's no need for having custom configuration-printing logic in your build scripts any more.

Its output is rather verbose and unwieldy as means of just figuring out the list of your configurations -- the grep extracts the name lines. Of course, you can go further and cut the word "Configuration" to get just the names:

gradle proj:resolvableConfigurations | grep "^Configuration" | sed "s/Configuration //g"

Upvotes: 17

Raman
Raman

Reputation: 19595

A variation of @Mike Hanafey's answer using Gradle Kotlin DSL, and adding another task that prints only resolvable configurations (useful for passing to the --configuration parameter of dependencyInsight).

allprojects {
  fun printConfigurations(filter: (Configuration) -> Boolean = { true }) {
    configurations.filter(filter).forEach {
      println("\t${it.name}")
    }
  }

  task("printConfigurations") {
    doLast {
      println("${project.name} configurations:")
      printConfigurations()
    }
  }
  task("printResolvableConfigurations") {
    doLast {
      println("${project.name} resolvable configurations:")
      printConfigurations { it.isCanBeResolved }
    }
  }
}

Upvotes: 1

tricknology
tricknology

Reputation: 1138

If anyone is looking to do this on the command line:

gradle outgoingVariants

You'll have to do some parsing but you'll see something like:

--------------------------------------------------
Variant yummyDebugRuntimeElements
--------------------------------------------------
Description = Runtime elements for debug
Capabilities
    - group:artifact:0.1 (default capability)
Attributes
    - com.android.build.api.attributes.BuildTypeAttr               = debug
    - com.android.build.api.attributes.ProductFlavor:default.      = yummy
    - com.android.build.api.attributes.VariantAttr                 = debug
    - com.android.build.gradle.internal.dependency.AndroidTypeAttr = Aar
    - org.gradle.usage                                             = java-runtime
    - org.jetbrains.kotlin.platform.type                           = androidJvm


...

Upvotes: 2

Blundell
Blundell

Reputation: 76506

Here are all the configurations for the Java plugin:

https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_plugin_and_dependency_management

compile(Deprecated) Compile time dependencies. Superseded by implementation.

implementation extends compile Implementation only dependencies.

compileOnly Compile time only dependencies, not used at runtime.

compileClasspath extends compile, compileOnly, implementation Compile classpath, used when compiling source. Used by task compileJava.

annotationProcessor Annotation processors used during compilation.

runtime(Deprecated) extends compile Runtime dependencies. Superseded by runtimeOnly.

runtimeOnly Runtime only dependencies.

runtimeClasspath extends runtimeOnly, runtime, implementation Runtime classpath contains elements of the implementation, as well as runtime only elements.

testCompile(Deprecated) extends compile Additional dependencies for compiling tests. Superseded by testImplementation.

testImplementation extends testCompile, implementation Implementation only dependencies for tests.

testCompileOnly Additional dependencies only for compiling tests, not used at runtime.

testCompileClasspath extends testCompile, testCompileOnly, testImplementation Test compile classpath, used when compiling test sources. Used by task compileTestJava.

testRuntime(Deprecated) extends runtime, testCompile Additional dependencies for running tests only. Superseded by testRuntimeOnly.

testRuntimeOnly extends runtimeOnly Runtime only dependencies for running tests.

testRuntimeClasspath extends testRuntimeOnly, testRuntime, testImplementation Runtime classpath for running tests. Used by task test.

archives Artifacts (e.g. jars) produced by this project. Used by task uploadArchives.

default extends runtimeClasspath The default configuration used by a project dependency on this project. Contains the artifacts and dependencies required by this project at runtime.

Upvotes: 2

Mike Hanafey
Mike Hanafey

Reputation: 5643

Add this to root project:

allprojects {
    repositories {
        // ....
    }

    task printConfigurations {
        doLast {task ->
            println "Project Name: $project.name configurations:"
            configurations.each {
                println "    $it.name"
            }
        }
    }
}

Then, for example:

$ ./gradlew -q :SubProjA:printConfigurations
Project Name: SubProjA configurations:
    -api
    -runtime
    annotationProcessor
    api
    apiDependenciesMetadata
    apiElements
    archives
    compile
    compileClasspath
    compileOnly
    compileOnlyDependenciesMetadata
    default
    implementation
    implementationDependenciesMetadata
    kotlinCompilerClasspath
    kotlinCompilerPluginClasspath
    kotlinKlibCommonizerClasspath
    kotlinNativeCompilerPluginClasspath
    kotlinScriptDef
    kotlinScriptDefExtensions
    runtime
    runtimeClasspath
    runtimeElements
    runtimeOnly
    runtimeOnlyDependenciesMetadata
    sourceArtifacts
    testAnnotationProcessor
    testApi
    testApiDependenciesMetadata
    testCompile
    testCompileClasspath
    testCompileOnly
    testCompileOnlyDependenciesMetadata
    testImplementation
    testImplementationDependenciesMetadata
    testKotlinScriptDef
    testKotlinScriptDefExtensions
    testRuntime
    testRuntimeClasspath
    testRuntimeOnly
    testRuntimeOnlyDependenciesMetadata

Upvotes: 16

Niel de Wet
Niel de Wet

Reputation: 8398

With Gradle 5 it's very simple with the --info option. For example:

./gradlew projects --info

Now look in the Configure project section which lists all the configurations.

Upvotes: 21

user1942586
user1942586

Reputation: 161

Just run these commands without the --configuration flag and the first lines of the output will be the list of available configurations

Upvotes: -5

Mark
Mark

Reputation: 1854

Try

gradle --console plain dependencies | fgrep ' - '

The dependencies task lists all configurations (along with their dependencies), and the fgrep will just show you the configuration names (along with a brief description of each). It's not great, but doesn't require you to put stuff in your build script.

Upvotes: 59

Opal
Opal

Reputation: 84804

Have you tried:

configurations.each { println it.name }

?

Upvotes: 67

Related Questions