Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Gradle OSS Dependencies

Gradle OSS Dependencies

Read OSS licenses for Gradle Dependencies. The script outputs a JSON file with the dependency, its URL on and the OSS license to STDOUT.

DISCLAIMER: if you use this script too extensively, your IP address might get blocked by!

Put this script into a file and make it executable:

#!/usr/bin/env bash

dep_file=$(mktemp /tmp/dep_file.XXXXXX)
trap "{ rm -f dep_file; }" EXIT

./gradlew allDeps | sed 's/^[^a-zA-Z]*//' | sort | uniq | grep ".*:.*:.*" | grep -v "(\*)" | grep -v " -> " > $dep_file

echo '['

while read dependency; do
    dependency_url=$(echo $dependency | sed 's/\:/\//g' | sed 's/^/https:\/\/\/artifact\//')
    license_and_url=$(curl --noproxy -s $dependency_url | tr '\n' ' ' | perl -pe 's/.*License<\/th><th>URL<\/th><\/tr><\/thead><tbody><tr><td>(.*?)<\/td>.*?a href="(.+?)".*/\1, \2/')
    if (( ${#license_and_url} > 200 )); then
    echo "{ \"lib\" : \"${dependency}\", \"url\" : \"${dependency_url}\", \"license_and_url\" : \"${license_and_url}\"},"

    # use a sleep to avoid getting blocked by
    sleep 4
done < $dep_file

echo ']'

If you want to create a CSV from that, use

./ | jq 'map([.lib, .url, .license_and_url] | join(", ")) | join("\n")' 


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment