forked from midonet/midonet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.gradle
78 lines (74 loc) · 3.02 KB
/
git.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import org.gradle.api.DefaultTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
apply plugin: GitPropertiesPlugin
class GitPropertiesPlugin implements Plugin<Project> {
def describe
def id
def commitUser
def commitDate
def branch
def buildDate
def buildUser
def propsFile
@Override
void apply(Project project) {
project.ext.gitPropertiesPath = "${project.buildDir}/resources/main/git.properties"
project.task('gitProps', group: 'Build') << {
this.propsFile = new File(project.ext.gitPropertiesPath)
project.exec {
commandLine 'git describe'.split()
standardOutput = new ByteArrayOutputStream()
this.describe = { standardOutput.toString().trim() }
workingDir project.projectDir
}
project.exec {
commandLine 'git rev-parse HEAD'.split()
standardOutput = new ByteArrayOutputStream()
this.id = { standardOutput.toString().trim() }
}
project.exec {
commandLine 'git log -1 HEAD --pretty=format:%ae'.split()
standardOutput = new ByteArrayOutputStream()
this.commitUser = { standardOutput.toString().trim() }
}
project.exec {
commandLine 'git rev-parse --abbrev-ref HEAD'.split()
standardOutput = new ByteArrayOutputStream()
this.branch = { standardOutput.toString().trim() }
}
project.exec {
commandLine 'git log -1 HEAD --pretty=format:%ad'.split()
standardOutput = new ByteArrayOutputStream()
this.commitDate = { standardOutput.toString().trim() }
}
project.exec {
commandLine 'date'.split()
standardOutput = new ByteArrayOutputStream()
this.buildDate = { standardOutput.toString().trim() }
}
project.exec {
commandLine 'whoami'.split()
standardOutput = new ByteArrayOutputStream()
this.buildUser = { standardOutput.toString().trim() }
}
}
project.tasks.gitProps.doLast {
this.propsFile.parentFile.mkdirs()
this.propsFile.write(
"git.commit.id.describe=${describe()}\n" +
"git.commit.id=${id()}\n" +
"git.commit.time=${commitDate()}\n" +
"git.commit.user.name=${commitUser()}\n" +
"git.branch=${branch()}\n" +
"git.build.time=${buildDate()}\n" +
"git.build.user.name=${buildUser()}\n")
}
project.tasks.jar.dependsOn project.tasks.gitProps
project.tasks.gitProps.outputs.file project.ext.gitPropertiesPath
project.tasks.gitProps.inputs.files project.sourceSets.main.output.classesDir
}
}