Gradle Task 入门 6 发布
2020-08-06 本文已影响0人
这个世界是虚拟的
打包备份文件
此时,打包将依赖之前的makeReleaseVersion方法,执行定义好的版本信息并保持使用此配置,打包使用Zip形式,task的outputs 使用 war插件的war.outputs.files, 打包内容为所有的源文件以及版本配置文件, 此时保存的源文件为java文件,注意此时的task类型为Zip
task createDistribution(type: Zip, dependsOn: makeReleaseVersion){
//use outputs of war.outputs.files as outputs
from war.outputs.files
//put all source into 'src' folder
from(sourceSets*.allSource){
into 'src'
}
from(rootDir){
include project.ext.myTagFile.name
}
}
保存/复制备份文件
使用之前task的createDistribution.outputs.files 作为outputs,保存备份文件到build文件下的backup
task backupReleaseDistribution(type: Copy){
//use outputs of createDistribution.outputs.files as outputs
from createDistribution.outputs.files
into "$buildDir/backup"
}
发布
简单打印日志
task release(dependsOn: backupReleaseDistribution){
doLast{
logger.quiet 'releasing the project...'
}
}
执行
//gradlew release
> Task :release
releasing the project...
同时也可以看到工程下build文件夹下多出了一个backup包,含有一个本工程的zip文件.
执行顺序
我们可以根据 -i 来观察详细的执行顺序来研究依赖关系
D:\code\gradleTuto>gradlew release -i
... ...
All projects evaluated.
Selected primary task 'release' from project :
Tasks to be executed: [task ':makeReleaseVersion', task ':compileJava', task ':processResources', task ':classes', task ':war', task ':createDistri
bution', task ':backupReleaseDistribution', task ':release']
Tasks that were excluded: []
:makeReleaseVersion (Thread[Daemon worker Thread 2,5,main]) started.
> Task :makeReleaseVersion UP-TO-DATE
Caching disabled for task ':makeReleaseVersion' because:
Build cache is disabled
Skipping task ':makeReleaseVersion' as it is up-to-date.
:makeReleaseVersion (Thread[Daemon worker Thread 2,5,main]) completed. Took 0.004 secs.
:compileJava (Thread[Daemon worker Thread 2,5,main]) started.
> Task :compileJava UP-TO-DATE
Resolving global dependency management for project 'demo'
Excluding []
Excluding []
Caching disabled for task ':compileJava' because:
Build cache is disabled
Skipping task ':compileJava' as it is up-to-date.
:compileJava (Thread[Daemon worker Thread 2,5,main]) completed. Took 0.598 secs.
:processResources (Thread[Daemon worker Thread 2,5,main]) started.
> Task :processResources UP-TO-DATE
Caching disabled for task ':processResources' because:
Build cache is disabled
Skipping task ':processResources' as it is up-to-date.
:processResources (Thread[Daemon worker Thread 2,5,main]) completed. Took 0.003 secs.
:classes (Thread[Daemon worker Thread 2,5,main]) started.
> Task :classes UP-TO-DATE
Skipping task ':classes' as it has no actions.
:classes (Thread[Daemon worker Thread 2,5,main]) completed. Took 0.0 secs.
:war (Thread[Daemon worker Thread 2,5,main]) started.
> Task :war SKIPPED
Skipping task ':war' as task onlyIf is false.
:war (Thread[Daemon worker Thread 2,5,main]) completed. Took 0.0 secs.
:createDistribution (Thread[Daemon worker Thread 2,5,main]) started.
> Task :createDistribution UP-TO-DATE
file or directory 'D:\code\gradleTuto\build\libs\demo-0.0.1-SNAPSHOT.war', not found
Caching disabled for task ':createDistribution' because:
Build cache is disabled
Skipping task ':createDistribution' as it is up-to-date.
:createDistribution (Thread[Daemon worker Thread 2,5,main]) completed. Took 0.01 secs.
:backupReleaseDistribution (Thread[Daemon worker Thread 2,5,main]) started.
> Task :backupReleaseDistribution UP-TO-DATE
Caching disabled for task ':backupReleaseDistribution' because:
Build cache is disabled
Skipping task ':backupReleaseDistribution' as it is up-to-date.
:backupReleaseDistribution (Thread[Daemon worker Thread 2,5,main]) completed. Took 0.002 secs.
:release (Thread[Daemon worker Thread 2,5,main]) started.
> Task :release
Caching disabled for task ':release' because:
Build cache is disabled
Task ':release' is not up-to-date because:
Task has not declared any outputs despite executing actions.
releasing the project...
:release (Thread[Daemon worker Thread 2,5,main]) completed. Took 0.002 secs.
BUILD SUCCESSFUL in 2s
6 actionable tasks: 1 executed, 5 up-to-date
总结
这里定义的task, 其参数都是task, 例如: Zip, Copy 和backupReleaseDistribution, 这些task也有自己的实现比如:
public class Zip extends AbstractArchiveTask
public class Copy extends AbstractCopyTask
他们也都有自己的内部逻辑,输入以及使用比如war.outputs.files输出一级一级返回,还有action定义,那么这些action也会被相应的执行,比如zip时打包:
@Override
protected CopyAction createCopyAction() {
DocumentationRegistry documentationRegistry = getServices().get(DocumentationRegistry.class);
return new ZipCopyAction(getArchiveFile().get().getAsFile(), getCompressor(), documentationRegistry, metadataCharset, isPreserveFileTimestamps());
}
那么我们可以根据需要来定义自己的task,使用需要的task type来完成自己的业务需求,不懂的时候可以直接进入Javadocs进行.