gradle构建工具学习系列
用于监听task执行图事件的接口是由TaskExecutionGraphListener实现的。 通过监听器挂接到生命周期只需要两个步骤 1. 实现特定监听器接口
class ReleaseVersionListener implements TaskExecutionGraphListener {
final static String releaseTaskPath = ':release'
@Override
void graphPopulated(TaskExecutionGraph taskGraph) {
if (taskGraph.hasTask(releaseTaskPath)) {
List<Task> allTasks = taskGraph.allTasks
Task releaseTask = allTasks.find { it.path == releaseTaskPath }
Project project = releaseTask.project
if (!project.version.release) {
project.version.release = true
project.ant.propertyfile(file: project.versionFile) {
entry(key: 'release', type: 'string', operation: '=', value: 'true')
}
}
}
}
}
2. 注册监听器实现
gradle.taskGraph.addTaskExecutionGraphListener(new ReleaseVersionListener())
以下为构造脚本build.gradle完整代码
apply plugin: 'war'
ext.versionFile = file('version.properties')
task loadVersion {
project.version = readVersion()
}
ProjectVersion readVersion() {
logger.quiet 'Reading the version file.'
if (!versionFile.exists()) {
throw new GradleException("Required version file does not exit: $versionFile.canonicalPath")
}
Properties versionProps = new Properties()
versionFile.withInputStream { stream ->
versionProps.load(stream)
}
new ProjectVersion(versionProps.major.toInteger(), versionProps.minor.toInteger(), versionProps.release.toBoolean())
}
class ReleaseVersionListener implements TaskExecutionGraphListener {
final static String releaseTaskPath = ':release'
@Override
void graphPopulated(TaskExecutionGraph taskGraph) {
if (taskGraph.hasTask(releaseTaskPath)) {
List<Task> allTasks = taskGraph.allTasks
Task releaseTask = allTasks.find { it.path == releaseTaskPath }
Project project = releaseTask.project
if (!project.version.release) {
project.version.release = true
project.ant.propertyfile(file: project.versionFile) {
entry(key: 'release', type: 'string', operation: '=', value: 'true')
}
}
}
}
}
gradle.taskGraph.addTaskExecutionGraphListener(new ReleaseVersionListener())
task createDistribution(type: Zip) {
from war.outputs.files
from(sourceSets*.allSource) {
into 'src'
}
from(rootDir) {
include versionFile.name
}
}
task backupReleaseDistribution(type: Copy) {
from createDistribution.outputs.files
into "$buildDir/backup"
}
task release(dependsOn: backupReleaseDistribution) << {
doLast{
logger.quiet 'Releasing the project...'
}
}
tasks.addRule("Pattern: increment<Classifier>Version – Increments the project version classifier.") { String taskName ->
if (taskName.startsWith('increment') && taskName.endsWith('Version')) {
task(taskName){
doLast {
String classifier = (taskName - 'increment' - 'Version').toLowerCase()
String currentVersion = version.toString()
switch (classifier) {
case 'major': ++version.major
break
case 'minor': ++version.minor
break
default: throw new GradleException("Invalid version type '$classifier. Allowed types: ['Major', 'Minor']")
}
String newVersion = version.toString()
logger.info "Incrementing $classifier project version: $currentVersion -> $newVersion"
ant.propertyfile(file: versionFile) {
entry(key: classifier, type: 'int', operation: '+', value: 1)
}
}
}
}
}
class ProjectVersion {
Integer major
Integer minor
Boolean release
ProjectVersion(Integer major, Integer minor) {
this.major = major
this.minor = minor
this.release = Boolean.FALSE
}
ProjectVersion(Integer major, Integer minor, Boolean release) {
this(major, minor)
this.release = release
}
@Override
String toString() {
"$major.$minor${release ? '' : '-SNAPSHOT'}"
}
}
执行后结果版本文件内容被修改,如下所示
|