Gradle 插件 - getArtifacts() 结果 'Could Not resolve all dependencies for configuration...'
Gradle Plugin - getArtifacts() Results in 'Could Not resolve all dependencies for configuration...'
我正在尝试编写一个 gradle 任务(下面的代码)来聚合多项目构建中 ResolvedArtifact
对象的信息。但是,当我 运行 使用 gradle 4.8.1 在示例 android 项目 (https://github.com/gmetal/sample-dependency-check-app) 上执行任务时,我收到以下错误:
07:29 $ gradle clean resolveArtifacts
> Task :app:resolveArtifacts FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:resolveArtifacts'.
> Could not resolve all dependencies for configuration ':app:debugAndroidTestCompileClasspath'.
> More than one variant of project :mylibrary matches the consumer attributes:
- Configuration ':mylibrary:debugApiElements' variant android-aidl:
- Found artifactType 'android-aidl' but wasn't required.
- Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':mylibrary:debugApiElements' variant android-classes:
- Found artifactType 'android-classes' but wasn't required.
- Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':mylibrary:debugApiElements' variant android-manifest:
- Found artifactType 'android-manifest' but wasn't required.
- Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':mylibrary:debugApiElements' variant android-renderscript:
- Found artifactType 'android-renderscript' but wasn't required.
- Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':mylibrary:debugApiElements' variant jar:
- Found artifactType 'jar' but wasn't required.
- Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
resolveArtifacts 任务的代码如下:
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ResolvedArtifact
import org.gradle.api.tasks.TaskAction
import static org.gradle.api.specs.Specs.SATISFIES_ALL
class ResolveArtifacts extends DefaultTask {
ResolveArtifacts() {
group = 'Sample Task'
description = 'Attempts to resolve artifacts.'
}
@TaskAction
analyze() {
project.allprojects.collectMany { Project currentProject ->
currentProject.configurations.findAll {
canBeResolved(it)
}.each { Configuration configuration ->
configuration.resolvedConfiguration.lenientConfiguration.getArtifacts(SATISFIES_ALL).each { ResolvedArtifact artifact ->
logger.lifecycle("------ resolved ${artifact}")
}
}
}
}
def canBeResolved(configuration) {
configuration.metaClass.respondsTo(configuration, "isCanBeResolved") ?
configuration.isCanBeResolved() : true
}
}
关于如何解决上述错误的任何建议?原始问题已发布到 github 上的 dependency-check-gradle。
解决方案是使用 configuration.incoming.artifactView 并仅过滤所需的工件类型:
def artifactType = Attribute.of('artifactType', String)
def types = ['jar', 'aar']
types.each { type ->
configuration.incoming.artifactView {
attributes {
it.attribute(artifactType, type)
}
}.artifacts.each {
println "artifact file: ${it.file}"
}
}
看看我的 github issue 和 gradle 团队。
我正在尝试编写一个 gradle 任务(下面的代码)来聚合多项目构建中 ResolvedArtifact
对象的信息。但是,当我 运行 使用 gradle 4.8.1 在示例 android 项目 (https://github.com/gmetal/sample-dependency-check-app) 上执行任务时,我收到以下错误:
07:29 $ gradle clean resolveArtifacts
> Task :app:resolveArtifacts FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:resolveArtifacts'.
> Could not resolve all dependencies for configuration ':app:debugAndroidTestCompileClasspath'.
> More than one variant of project :mylibrary matches the consumer attributes:
- Configuration ':mylibrary:debugApiElements' variant android-aidl:
- Found artifactType 'android-aidl' but wasn't required.
- Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':mylibrary:debugApiElements' variant android-classes:
- Found artifactType 'android-classes' but wasn't required.
- Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':mylibrary:debugApiElements' variant android-manifest:
- Found artifactType 'android-manifest' but wasn't required.
- Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':mylibrary:debugApiElements' variant android-renderscript:
- Found artifactType 'android-renderscript' but wasn't required.
- Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':mylibrary:debugApiElements' variant jar:
- Found artifactType 'jar' but wasn't required.
- Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
resolveArtifacts 任务的代码如下:
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ResolvedArtifact
import org.gradle.api.tasks.TaskAction
import static org.gradle.api.specs.Specs.SATISFIES_ALL
class ResolveArtifacts extends DefaultTask {
ResolveArtifacts() {
group = 'Sample Task'
description = 'Attempts to resolve artifacts.'
}
@TaskAction
analyze() {
project.allprojects.collectMany { Project currentProject ->
currentProject.configurations.findAll {
canBeResolved(it)
}.each { Configuration configuration ->
configuration.resolvedConfiguration.lenientConfiguration.getArtifacts(SATISFIES_ALL).each { ResolvedArtifact artifact ->
logger.lifecycle("------ resolved ${artifact}")
}
}
}
}
def canBeResolved(configuration) {
configuration.metaClass.respondsTo(configuration, "isCanBeResolved") ?
configuration.isCanBeResolved() : true
}
}
关于如何解决上述错误的任何建议?原始问题已发布到 github 上的 dependency-check-gradle。
解决方案是使用 configuration.incoming.artifactView 并仅过滤所需的工件类型:
def artifactType = Attribute.of('artifactType', String)
def types = ['jar', 'aar']
types.each { type ->
configuration.incoming.artifactView {
attributes {
it.attribute(artifactType, type)
}
}.artifacts.each {
println "artifact file: ${it.file}"
}
}
看看我的 github issue 和 gradle 团队。