Script to view a jars transitive dependencies

by 5/08/2017 12:26:00 PM 1 comments
Very often when I'm programming in Java, I need to see the dependencies for a particular jar file. If you are working in a project using a build tool like Maven or SBT, it's quite trivial to dump out a dependency tree of all the jars in the project. Outside of the project it's a pain, generally you have to throw together a quick project and then run your tool on it. As a workaround, I threw together a script that does that for you (and cleans up afterwards) Just copy the code into a file named `mvntree`. You should tweak the included repositories to reflect ones that you may need. To run it use:

mvntree groupId artifactId version


For example:

mvntree edu.ucar netcdf4 4.6.9


mvntree

#!/usr/bin/env bash

# Usage: mvntree groupId artifactId version

CUR_DIR=$(pwd)
MY_DIR="/tmp/trashme_one"

mkdir -p "$MY_DIR" && \
  cat > "$MY_DIR/pom.xml" <<EOL
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>DEPENDENCY</groupId>
   <artifactId>TREE.FOR</artifactId>
   <version>IS</version>
   <packaging>jar</packaging>
   <name>Maven Quick Start Archetype</name>
   <url>http://maven.apache.org</url>
   <dependencies>
      <dependency>
         <groupId>$1</groupId>
         <artifactId>$2</artifactId>
         <version>$3</version>
      </dependency>
<!-- add here other dependencies -->
   </dependencies>
   <repositories>
     <repository>
       <snapshots>
         <enabled>false</enabled>
       </snapshots>
       <id>bintray-hohonuuli-maven</id>
       <name>bintray</name>
       <url>http://dl.bintray.com/hohonuuli/maven</url>
     </repository>
     <repository>
       <snapshots>
         <enabled>false</enabled>
       </snapshots>
       <id>ucar</id>
       <name>ucar</name>
       <url>http://artifacts.unidata.ucar.edu/content/repositories/unidata-releases/</url>
     </repository>
   </repositories>
</project>
EOL

cd "$MY_DIR" && \
mvn -e dependency:tree -U

cd "$CUR_DIR"
rm -rf $MY_DIR 

hohonuuli

Developer

Cras justo odio, dapibus ac facilisis in, egestas eget quam. Curabitur blandit tempus porttitor. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.