Skip to content

Instantly share code, notes, and snippets.

@meistermeier
Last active February 21, 2017 08:20
Show Gist options
  • Save meistermeier/b506d225e6ce4b54b904028b767789e8 to your computer and use it in GitHub Desktop.
Save meistermeier/b506d225e6ce4b54b904028b767789e8 to your computer and use it in GitHub Desktop.
mvn dependency tree filtered
# uses mvn dependency plugin to show why a certain dependency or dependencies are used
function mvnwhy() {
CANDIDATES=(`mvn dependency:list | grep $1 | awk '{print $2}'`)
for CANDIDATE in ${CANDIDATES[@]}; do
echo "Why $CANDIDATE?"
declare -a CANDIDATE_SEGMENTS
CANDIDATE_SEGMENTS=(`echo $CANDIDATE | awk 'BEGIN{FS=":"}{for (i=1; i<=2; i++) print $i}'`)
# this just works in zsh for bash use: DEPENDENCY=${CANDIDATE_SEGMENTS[0]}:${CANDIDATE_SEGMENTS[1]}
DEPENDENCY=${CANDIDATE_SEGMENTS[1]}:${CANDIDATE_SEGMENTS[2]}
# scans and shows dependency tree for every dep (this might get optimized later)
# background: no way found to get always the right line before the pattern matches
mvn dependency:tree -Dincludes=$DEPENDENCY | grep -B 1 "\\\-"
done
}
# single match
mvnwhy com.jayway.jsonpath:json-path
# Output:
# Why com.jayway.jsonpath:json-path:jar:2.2.0:test?
# [INFO] com.meistermeier:rest-docs-sample:jar:0.0.1-SNAPSHOT
# [INFO] \- org.springframework.boot:spring-boot-starter-test:jar:1.4.1.RELEASE:test
# [INFO] \- com.jayway.jsonpath:json-path:jar:2.2.0:test
#
#
# multi match
# call:
mvnwhy json
# Output:
# Why com.jayway.jsonpath:json-path:jar:2.2.0:test?
# [INFO] com.meistermeier:rest-docs-sample:jar:0.0.1-SNAPSHOT
# [INFO] \- org.springframework.boot:spring-boot-starter-test:jar:1.4.1.RELEASE:test
# [INFO] \- com.jayway.jsonpath:json-path:jar:2.2.0:test
# Why org.json:json:jar:20140107:test?
# [INFO] com.meistermeier:rest-docs-sample:jar:0.0.1-SNAPSHOT
# [INFO] \- org.springframework.boot:spring-boot-starter-test:jar:1.4.1.RELEASE:test
# [INFO] \- org.skyscreamer:jsonassert:jar:1.3.0:test
# [INFO] \- org.json:json:jar:20140107:test
# Why net.minidev:json-smart:jar:2.2.1:test?
# [INFO] com.meistermeier:rest-docs-sample:jar:0.0.1-SNAPSHOT
# [INFO] \- org.springframework.boot:spring-boot-starter-test:jar:1.4.1.RELEASE:test
# [INFO] \- com.jayway.jsonpath:json-path:jar:2.2.0:test
# [INFO] \- net.minidev:json-smart:jar:2.2.1:test
# Why org.skyscreamer:jsonassert:jar:1.3.0:test?
# [INFO] com.meistermeier:rest-docs-sample:jar:0.0.1-SNAPSHOT
# [INFO] \- org.springframework.boot:spring-boot-starter-test:jar:1.4.1.RELEASE:test
# [INFO] \- org.skyscreamer:jsonassert:jar:1.3.0:test
@darookee
Copy link

I don't have maven installed but the first awk may be replaced by cut -f2 (with the appropriate -d option) and the second awk could be replaced by cut -d: -f1-2, making the whole for-part just two lines:

[...]
    echo "Why $CANDIDATE?"
    mvn dependency:tree -Dincludes=$(echo $CANDIDATE | cut -d: -f1-2) | grep -B 1 "\\\-"
[...]

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