Finding unused jar files

Like a lot of corporate software projects, we have all our dependency jars checked into source control, rather than pulling them from a public repository. I wrote this script to find unused jar files: jars that are checked in, but not referenced from any project. It’s not perfect, though:

  • It lists source and javadoc jars as unused, even though the eclipse and idea tasks use those. * It considers jars for things like the findbugs plugin to be unused.

I’m posting it here in hopes that it will be useful to others, and maybe somebody will be interested in helping me make it better. :slight_smile:

// Finds jar files which aren't referenced by any subproject. Such jar
// files should probably be removed from revision control.
  import org.gradle.api.artifacts.repositories.*
task unusedJars() {
    description = "Finds jars which aren't used by any project"
    ext.baseDir = rootDir
    ext.usedJars = new HashSet<File>()
    ext.allJars = new HashSet<File>()
    ext.unusedJars = new HashSet<File>()
      doLast {
        // Find all the jars which are actually used.
        rootProject.allprojects.each { proj ->
            proj.configurations.each { conf ->
                conf.resolvedConfiguration.resolvedArtifacts.each { art ->
        println("Projects reference " + usedJars.size() + " jars")
          // Find all the jars under our root directory.
        def tree = fileTree(baseDir)
        tree.each { File file ->
        println("Tree contains " + allJars.size() + " jars")
          unusedJars = allJars - usedJars
        println("Found " + unusedJars.size() + " unused jars:")
        unusedJars.sort().each { file ->