Skip to content

Instantly share code, notes, and snippets.

@ngtignacio
Last active October 15, 2021 09:25
Show Gist options
  • Save ngtignacio/d0720b7a565729037d0fef1936655793 to your computer and use it in GitHub Desktop.
Save ngtignacio/d0720b7a565729037d0fef1936655793 to your computer and use it in GitHub Desktop.
// download the sources of all runtime dependencies in the release build
// add this script in the file build.gradle of each module
// This is a modified version of https://stackoverflow.com/a/58748741/2439283
// How to use: ./gradlew downloadDependencySources
task downloadDependencySources() {
doLast {
// Use here the build variant you need.
// By default it tries to get "prodRelease", if not found then "release"
// Runtime classpath is handy for FOSS management.
def conf = null
try {
conf = configurations.getByName("prodReleaseRuntimeClasspath")
} catch (Exception ignored) {
}
if (conf == null) {
try {
conf = configurations.getByName("releaseRuntimeClasspath")
} catch (Exception ignored) {
}
}
if (conf == null) {
try {
// this is for java modules
conf = configurations.getByName("runtimeClasspath")
} catch (Exception ignored) {
}
}
if (conf == null) {
println("Gradle configuration not found! Can not download sources. See below the list of configurations:")
configurations.each { println "Configuration: ${it.name}" }
} else {
conf.allDependencies.each { dependency ->
if (dependency.group != null && dependency.name != null && dependency.version != null) {
for (ArtifactRepository repository : repositories.asList()) {
def url = repository.properties.get('url')
//https://repo.maven.apache.org/maven2/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar
def jarUrl = String.format("%s%s/%s/%s/%s-%s-sources.jar", url.toString(),
dependency.group.replace('.', '/'), dependency.name, dependency.version,
dependency.name, dependency.version)
try {
def folderName = "_dependencySources"
def folder = new File(folderName)
if (!folder.exists()) {
folder.mkdirs()
}
def f = new File(String.format("$folderName/%s-%s-%s-sources.jar",
dependency.group, dependency.name, dependency.version))
if (!f.exists()) {
new URL("$jarUrl").withInputStream { i -> f.withOutputStream { it << i } }
println "Downloaded sources :: $jarUrl"
}
} catch (Exception ignored) {
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment