以下部分是我的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
更有时间效率吗?
也许其他人会过来告诉我我错了,但IMO 5秒并不是构建fatjar的合理时间。定义的任务
- 解压缩每个依赖项的jar文件
- 合并未打包的文件以及项目类文件,然后
- 把所有东西重新包装到一个新罐子里
根据你有多少依赖关系,5秒对我来说似乎还可以。除非底层Zip任务以某种方式进行了优化,否则我看不到这种时间变化。
不过,你可以做一些事情:
-
您可以将fatjar任务从标准构建周期中移出,该周期针对每一个更改运行。您只能在发布时运行fatjar任务。在大多数情况下,您应该能够在本地测试更改,而不必构建fatjar,因为您无论如何都有可用的所有依赖项。
-
对于单个文件更改,您需要使用大量的依赖性jar来进行解包-重新打包。也许您可以编写一个优化的fatjar任务,打开旧的fatjar,替换更改后的文件并重新打包,从而减少所需的文件操作次数。
EDIT:我刚刚看了gradle shadow jar插件,意识到它正是我上面描述的#2,对fatjar进行了增量更改,这应该会给你带来明显的性能改进。