我怎样才能得到一个所有配置的一个 Gradle 项目的列表?

我正在尝试获得 洞察力依赖关系级任务的 --configuration标志的所有有效值的列表。我应该如何用 Gradle 3.2.1来做这件事呢?

47656 次浏览

Have you tried:

configurations.each { println it.name }

?

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.

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

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.

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

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.

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

configurations.forEach { println(it) }

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) }
}
}

从命令行运行任务,如下所示:

./gradlew myConfigs

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




...

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 }
}
}
}