如何加快缓慢的fatJAR任务?

时间:2016-04-15 10:20:46

标签: performance gradle gradle-2

以下部分是我的build.gradle文件的一部分:

project(":App") {
    dependencies {
        compile(project(':Shared'))
        compile(project(':Subproject1'))
        compile(project(':Subproject2'))
        compile(project(':Subproject3'))
        compile(project(':Subproject4'))
        compile(project(':Subproject5'))
    }

    jar {
        manifest {
            attributes(
                    'Main-Class': 'com.my.App.Main',
            )
        }
    }

    // To run, use: $ gradle :App:fatJar
    task fatJar(type: Jar) {
        println "fatJarBuild(): 1"
        manifest.from jar.manifest
        println "fatJarBuild(): 2"
        classifier = 'all'
        println "fatJarBuild(): 3"
        from {
            configurations.runtime.collect {
                println "fatJarBuild(): collect" + it.absolutePath
                it.isDirectory() ? it : zipTree(it)
            }
        } {
            exclude "META-INF/*.SF"
            exclude "META-INF/*.DSA"
            exclude "META-INF/*.RSA"
        }

        duplicatesStrategy = DuplicatesStrategy.EXCLUDE

        println "fatJarBuild(): 4"
        with jar
    }       
}

现在,每当我稍微修改代码库中的文件时,完成fatJar任务需要花费大量时间:

[... End of "./gradlew :App:fatJar --info" follows ... ] 

:App:compileJava (Thread[Daemon worker Thread 13,5,main]) completed. Took 0.164 secs.
:App:processResources (Thread[Daemon worker Thread 13,5,main]) started.
:App:processResources
Skipping task ':App:processResources' as it is up-to-date (took 0.002 secs).
:App:processResources UP-TO-DATE
:App:processResources (Thread[Daemon worker Thread 13,5,main]) completed. Took 0.002 secs.
:App:classes (Thread[Daemon worker Thread 13,5,main]) started.
:App:classes
Skipping task ':App:classes' as it has no actions.
:App:classes (Thread[Daemon worker Thread 13,5,main]) completed. Took 0.0 secs.
:App:fatJar (Thread[Daemon worker Thread 13,5,main]) started.
:App:fatJar
Executing task ':App:fatJar' (up-to-date check took 0.003 secs) due to:
  Input file /home/work/App/BrainThread.class has changed.
:App:fatJar (Thread[Daemon worker Thread 13,5,main]) completed. Took 5.993 secs.

BUILD SUCCESSFUL

Total time: 7.051 secs
Stopped 0 compiler daemon(s).
Received result Success[value=null] from daemon DaemonInfo{pid=30179, address=[c5e7f6f0-985b-48cc-88b0-ebc8aed7e75b port:33465, addresses:[/0:0:0:0:0:0:0:1%lo, /127.0.0.1]], idle=true, context=DefaultDaemonContext[uid=cc8b9da5-88b5-476a-9cf5-430af98f7f5a,javaHome=/usr/lib/jvm/java-8-openjdk-amd64,daemonRegistryDir=/home/user/.gradle/daemon,pid=30179,idleTimeout=10800000,daemonOpts=-XX:MaxPermSize=256m,-XX:+HeapDumpOnOutOfMemoryError,-Xmx1024m,-Dfile.encoding=UTF-8,-Duser.country=US,-Duser.language=en,-Duser.variant]} (build should be done).

您可以看到BrainThread.class已被修改,并且一些重型机械开始生效。

我可以让build.gradle更有效率吗?

1 个答案:

答案 0 :(得分:2)

也许其他人会来并告诉我我错了,但IMO 5秒并不是制造胖子的不合理时间。定义的任务

  • 解压缩每个依赖项的jar文件,
  • 合并解压缩的文件以及项目类文件,然后
  • 将所有东西重新包装成新罐子。

根据您拥有的依赖项数量,5秒对我来说似乎相当不错。除非基础Zip任务以某种方式进行优化,否则我不会看到此时序更改。

你可以说几件事情:

  1. 您可以将fatjar任务移出标准构建周期,该周期针对每个更改运行。你只能在发布时运行fatjar任务。在大多数情况下,您应该能够在本地测试更改,而无需构建fatjar,因为您仍然可以使用所有依赖项。

  2. 对于单个文件更改,您正在使用大量依赖jar进行unpack-repack舞蹈。也许你可以编写一个优化的fatjar任务,解压缩旧的fatjar ,替换已更改的文件并重新打包,减少所需的文件操作次数。

  3. 编辑:我只是看了gradle shadow jar plugin,并意识到它完全按照我上面描述的#2进行,对fatjar进行了增量更改,这应该让你明白性能改进。