27

I would like to be able to perform an analysis on each of my project POMs to determine how many bytes each direct dependency introduces to the resulting package based on the sum of all of its transitive dependencies.

For example, if dependency A brings in B, C, and D, I would like to be able to see a summary showing A -> total size = (A + B + C + D).

Is there an existing Maven or Gradle way to determine this information?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Shawn Sherwood
  • 1,968
  • 2
  • 19
  • 30

4 Answers4

19

Here's a task for your build.gradle:

task depsize  {
    doLast {
        final formatStr = "%,10.2f"
        final conf = configurations.default
        final size = conf.collect { it.length() / (1024 * 1024) }.sum()
        final out = new StringBuffer()
        out << 'Total dependencies size:'.padRight(45)
        out << "${String.format(formatStr, size)} Mb\n\n"
        conf.sort { -it.length() }
            .each {
                out << "${it.name}".padRight(45)
                out << "${String.format(formatStr, (it.length() / 1024))} kb\n"
            }
        println(out)
    }
}

The task prints out sum of all dependencies and prints them out with size in kb, sorted by size desc.

Update: latest version of task can be found on github gist

Slava Medvediev
  • 1,431
  • 19
  • 35
15

I keep the a small pom.xml template on my workstation to identify heavy-weight dependencies.

Assuming you want to see the weight of org.eclipse.jetty:jetty-client with all of its transitives create this in a new folder.

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>not-used</groupId>
  <artifactId>fat</artifactId>
  <version>standalone</version>

  <dependencies>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-client</artifactId>
      <version>LATEST</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-shade-plugin</artifactId>
        <executions>
          <execution>
           <phase>package</phase>
           <goals>
              <goal>shade</goal>
           </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Then cd to the folder and run mvn package and check the size of the generated fat jar. On Unix-like systems you can use du -h target/fat-standalone.jar for that.

In order to test another maven artifact just change groupId:artifactId in the above template.

dustin.schultz
  • 13,076
  • 7
  • 54
  • 63
Alex Yursha
  • 3,208
  • 3
  • 26
  • 25
  • 3
    Thanks, that is the closest thing that I've found as an answer. I still can't believe that there isn't a proper (built-in) way to do this. – Shawn Sherwood Feb 01 '16 at 15:40
  • There is no built-in way I'm aware of as of Jan 2016. [Maven Dependency Plugin](https://maven.apache.org/plugins/maven-dependency-plugin/) is the closest which could have been be helpful, but unfortunately `mvn dependency:get` has deprecated downloading into a custom location; `mvn dependency:copy` doesn't support transitive dependencies and `mvn dependency:copy-dependencies` requires a `pom.xml` file which makes it **at least** as cumbersome as the solution above. – Alex Yursha Feb 01 '16 at 21:39
1

I do not know any way to show the totals but you may get a report for your project which can show per dependency size information. Please check this maven plugin : http://maven.apache.org/plugins/maven-project-info-reports-plugin/dependencies-mojo.html

Özgür Eroğlu
  • 1,230
  • 10
  • 16
  • Not exactly what I was hoping for as I can already get this info just by opening the resulting war file. Still, thanks for pointing this plug-in out as it does have some nice features like the license summary. – Shawn Sherwood Mar 05 '14 at 15:30
1

If you have a configuration which includes all the necessary dependencies that you wish to calculate the size for you can simply put the following snippet in your build.gradle file:

def size = 0
configurations.myConfiguration.files.each { file ->
    size += file.size()
}
println "Dependencies size: $size bytes"

This should print out when you run any gradle task after the build file is compiled.

ngutzmann
  • 51
  • 1
  • 2