Created
February 10, 2022 11:27
-
-
Save bric3/cfafa3a47faba4eaefbb0b85120eb425 to your computer and use it in GitHub Desktop.
Gradle init script that adds `depsize` task that will display the size of each dependencies.
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
/* | |
* Registers tasks to get the dependency size | |
*/ | |
fun listConfigurationDependencies(configuration: Configuration ) { | |
val multiplier = 1024.0 | |
val formatStr = "%,10.2f" | |
val size = configuration.map { it.length() / (multiplier * multiplier) }.sum() | |
val out = StringBuffer() | |
out.append("\nConfiguration name: \"${configuration.name}\"\n") | |
if (size > 0) { | |
out.append("Total dependencies size:".padEnd(65)) | |
out.append("${String.format(formatStr, size)} Mb\n\n") | |
configuration.sortedBy { -it.length() } | |
.forEach { | |
out.append(it.name.padEnd(65)) | |
out.append("${String.format(formatStr, (it.length() / multiplier))} kb\n") | |
} | |
} else { | |
out.append("No dependencies found") | |
} | |
println(out) | |
} | |
allprojects { // this is Project | |
plugins.withType(JavaPlugin::class.java) { | |
// println("applied java ${name}") | |
tasks.register("depsize") { | |
group = "help" | |
description = "Prints dependencies for \"runtimeClasspath\" configuration" | |
// TODO allow to pass configuration name as parameter (compileClasspath) | |
doLast { | |
listConfigurationDependencies(configurations["runtimeClasspath"]) | |
} | |
} | |
tasks.register("depsize-all-configurations") { | |
group = "help" | |
description = "Prints dependencies for all available configurations" | |
doLast { | |
configurations | |
.filter { it.isCanBeResolved } | |
.forEach { listConfigurationDependencies(it) } | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment