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

前端 未结 4 2094
花落未央
花落未央 2021-02-07 06:05

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

4条回答
  •  春和景丽
    2021-02-07 06:37

    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

提交回复
热议问题