Last active
May 1, 2020 15:19
-
-
Save Tapchicoma/bc8479abaa60dcde954c4dfeceab349f to your computer and use it in GitHub Desktop.
Download all Gradle project dependecies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.gradle.api.DefaultTask | |
import org.gradle.api.artifacts.Configuration | |
import org.gradle.api.attributes.Attribute | |
import org.gradle.api.model.ObjectFactory | |
import org.gradle.api.tasks.* | |
import org.gradle.kotlin.dsl.setProperty | |
import java.io.File | |
import javax.inject.Inject | |
/** | |
* Syncs project all configurations artifacts files. | |
* | |
* By default downloads only "jar" or "aar", can be changed by providing another value | |
* to [artifactTypes] property. | |
* | |
* For multi-module project can be applied using following code: | |
* ```gradle | |
* allprojects { subProject -> | |
* subProject.tasks.register("downloadDependencies", com.freeletics.gradle.DownloadDependenciesTask.class) { | |
* group = HelpTasksPlugin.HELP_GROUP | |
* description = "Syncs (downloads if required) all project dependencies" | |
* } | |
* } | |
* ``` | |
*/ | |
// Disabling caching for this task until `projectConfiguration` input will not fail | |
// with AmbiguousVariantSelectionException exception | |
//@CacheableTask | |
open class DownloadDependenciesTask @Inject constructor( | |
objectFactory: ObjectFactory | |
) : DefaultTask() { | |
private val artifactTypeAttribute = Attribute.of("artifactType", String::class.java) | |
// @get:Input | |
@get:Internal | |
val artifactTypes = objectFactory.setProperty(String::class).apply { | |
set(setOf("jar", "aar", "apk")) | |
} | |
// @get:Classpath | |
@get:Internal | |
internal val buildScriptConfiguration = project.buildscript.configurations | |
.matching { it.isCanBeResolved } | |
// @get:Classpath | |
@get:Internal | |
internal val projectConfiguration = project.configurations | |
.matching { it.isCanBeResolved } | |
@get:OutputFiles | |
internal val artifactFiles: MutableList<File> = mutableListOf() | |
@TaskAction | |
fun syncDependencies() { | |
buildScriptConfiguration | |
.forEach { configuration -> | |
configuration.incoming.resolutionResult.allDependencies | |
resolveConfiguration(configuration, artifactTypes.get(), artifactTypeAttribute) | |
} | |
projectConfiguration | |
.forEach { configuration -> | |
configuration.incoming.resolutionResult.allDependencies | |
resolveConfiguration(configuration, artifactTypes.get(), artifactTypeAttribute) | |
} | |
} | |
private fun resolveConfiguration( | |
configuration: Configuration, | |
artifactTypes: Set<String>, | |
artifactTypeAttribute: Attribute<String> | |
) { | |
artifactTypes.forEach { type -> | |
configuration | |
.incoming | |
.artifactView { | |
isLenient = true | |
attributes { | |
attribute(artifactTypeAttribute, type) | |
} | |
} | |
.artifacts | |
.forEach { | |
logger.info("Syncing artifact: $it") | |
artifactFiles.add(it.file) | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tasks.register("downloadDependencies") { | |
group = HelpTasksPlugin.HELP_GROUP | |
description = "Syncs (downloads if required) all project dependencies" | |
doLast { | |
def artifactTypes = ['jar', 'aar'] | |
def artifactTypeAttribute = Attribute.of('artifactType', String) | |
project.rootProject.allprojects.each { subProject -> | |
subProject.buildscript.configurations.each { configuration -> | |
resolveConfiguration(configuration, artifactTypes, artifactTypeAttribute) | |
} | |
subProject.configurations.each { configuration -> | |
resolveConfiguration(configuration, artifactTypes, artifactTypeAttribute) | |
} | |
} | |
} | |
} | |
private static void resolveConfiguration( | |
configuration, | |
artifactTypes, | |
artifactTypeAttribute | |
) { | |
if (configuration.isCanBeResolved()) { | |
artifactTypes.each { type -> | |
configuration.incoming.artifactView { | |
lenient = true | |
attributes { | |
it.attribute(artifactTypeAttribute, type) | |
} | |
}.artifacts.each {} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment