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

I would like to be able to analyze each of my POM projects to determine how many bytes each direct dependency introduces into the received packet based on the sum of all its transitive dependencies.

For example, if the dependency A introduces B, C and D, I would like to see a summary showing A โ†’ total size = (A + B + C + D).

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

+8
source share
5 answers

I save a small pom.xml template on my workstation to identify heavy dependencies.

Assuming you want to see the weight of org.eclipse.jetty: the jetty client with all its transits creates 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 the mvn package and check the size of the generated fat can. On Unix-like systems, you can use du -h target/fat-standalone.jar to do this.

To test another maven artifact, simply change groupId:artifactId in the above template.

+3
source

Here is the 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 displays the sum of all the dependencies and displays them with a size in kb sorted by desc size.

+8
source

If you have a configuration that includes all the necessary dependencies that you want to calculate the size, you can simply put the following snippet into the build.gradle file:

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

This should be printed when starting any gradle task after compiling the build file.

+1
source

I donโ€™t know a way to show the totals, but you can get a report for your project that can display information about the size of the dependency. Please check this maven plugin: http://maven.apache.org/plugins/maven-project-info-reports-plugin/dependencies-mojo.html

0
source

All Articles