Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven/Gradle way to calculate the total size of a dependency with all its transitive dependencies included

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?

like image 614
Shawn Sherwood Avatar asked Mar 04 '14 15:03

Shawn Sherwood


People also ask

Does Gradle handle transitive dependencies?

Transitive dependencyBy default, Gradle resolves transitive dependencies automatically. The version selection for transitive dependencies can be influenced by declaring dependency constraints.

How does Maven determine transitive dependency?

You can get this information in the Maven Tool Window. First go to View → Tool Windows → Maven, to make sure that the Maven window is visible. The top-level elements in the tree are your direct dependencies, and the child elements are the transitive dependencies.

How do you find the source of transitive dependency?

Use the -p option to run on a sub-project. Show activity on this post. This solution finds any dependencies on log4j-slf4j-impl and instructs it to choose the one from logback (this is a spring app). This solution was surprisingly difficult to track down, but likely very useful in many situations.


2 Answers

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

like image 101
Slava Medvediev Avatar answered Sep 18 '22 14:09

Slava Medvediev


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.

like image 41
Alex Yursha Avatar answered Sep 18 '22 14:09

Alex Yursha