Skip to content

Instantly share code, notes, and snippets.

@oseiskar
Created September 15, 2020 17:34
Show Gist options
  • Save oseiskar/937e898de843cce9af1f377246b2ceb9 to your computer and use it in GitHub Desktop.
Save oseiskar/937e898de843cce9af1f377246b2ceb9 to your computer and use it in GitHub Desktop.
Extract dependency information from a Gradle project by examining its build cache
#!/bin/bash
#
# Extract dependency information from a gradle projects by simply checking
# what its build scripts download in the Gradle cache and extracting the POM
# XML files from each.
#
# Usage:
#
# ./this_script.bash path/to/project "./gradlew assembleDebug"
#
# The second argument is optional and has the above default.
#
set -eu
#set -x # debug
TARGET_FOLDER="$1"
BUILD_COMMAND="${2:-./gradlew assembleDebug}"
# Other settings settings can be modified with env vars, for example
#
# CLEAN=OFF WORK_DIR=/my/work/dir ./this_script.bash ...
#
# combining CLEAN=OFF with custom WORK_DIR allows running this for a batch of
# gradle-based projects. The overhead can be high otherwise, if they share
# a lot of dependencies.
: "${WORK_DIR:=`mktemp -d`}" # custom workdir
: "${CLEAN:=ON}" # delete workdir afterwards
: "${GRADLE_CACHE_SUBDIR:=caches/modules-2/files-2.1}" # probably not very stable
: "${OUTPUT_FOLDER=/tmp/extract_gradle_poms}"
ORIG_GRADLE_HOME="$HOME/.gradle"
FILES_TO_LINK="gradle.properties wrapper"
echo "Work dir: $WORK_DIR"
CUR_DIR=`pwd`
export GRADLE_USER_HOME="$WORK_DIR"
cd "$GRADLE_USER_HOME"
for f in $FILES_TO_LINK
do
echo "symlinking $f"
ln -s "$ORIG_GRADLE_HOME/$f" .
done
cd "$CUR_DIR"
cd "$TARGET_FOLDER"
echo "Building $TARGET_FOLDER"
$BUILD_COMMAND
cd "$CUR_DIR"
rm -rf "$OUTPUT_FOLDER"
mkdir -p "$OUTPUT_FOLDER"
echo "Finding POM files and copying to $OUTPUT_FOLDER"
for POM in `find "$WORK_DIR/$GRADLE_CACHE_SUBDIR" | egrep "\.pom$"|sort`; do
cp "$POM" "$OUTPUT_FOLDER/${POM//\//_}"
done
if [[ $CLEAN == "ON" ]]; then
echo "Removing work dir: $WORK_DIR"
rm -rf "$WORK_DIR"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment