-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
174 lines (145 loc) · 5.74 KB
/
build.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import groovy.json.JsonOutput
import java.text.SimpleDateFormat
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'antlr'
sourceCompatibility = 1.8
version = "0.3"
task wrapper(type: Wrapper) {
gradleVersion = '4.1'
}
repositories {
mavenCentral()
}
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.github.jengelman.gradle.plugins:shadow:2.0.0"
}
}
apply from: 'dependencies.gradle'
dependencies {
antlr 'org.antlr:antlr4:4.5.3'
compile libs.gsCollections
compile libs.commonsLang
compile libs.guava
compile libs.slf4j
compile libs.jackson
compile libs.jacksonAnnotations
compile libs.jacksonDatabind
compile libs.jacksonYaml
testCompile 'junit:junit:4.12'
testCompile 'org.assertj:assertj-core:3.8.0'
testCompile 'nl.jqno.equalsverifier:equalsverifier:2.3.2'
}
// Adapted from https://github.com/rossng/gradle-antlr-intellij/blob/master/build.gradle
generateGrammarSource {
def targetDir = file("${project.buildDir}/generated-src/main/java/")
outputDirectory = targetDir
arguments += ["-visitor", "-package", "tdanford.battleship.antlr"]
doLast {
// copy the java generated by ANTLR into a folder outside the build directory for IntelliJ
copy {
from targetDir
into "${projectDir}/src/generated/java"
}
}
}
// make sure that ANTLR generates java before compiling the project
// include the generated java code in the compilation
compileJava.dependsOn generateGrammarSource
compileJava {
source += ["${project.buildDir}/generated-src/main/java"]
}
// when cleaning, remove the generated sources too
task removeGeneratedSource(type: Delete) {
delete "${projectDir}/src/generated/"
}
clean.dependsOn removeGeneratedSource
// mark the generated source as such for IntelliJ IDEA
def ideaAntlrDir = file("${projectDir}/src/generated/java")
sourceSets.main.java.srcDirs += ideaAntlrDir
idea {
module {
generatedSourceDirs += ideaAntlrDir
}
}
getGitProperties()
configureShadowJarTask(project, 'battleship', version, 'tdanford.battleship.Main')
def configureShadowJarTask(
Project targetProject,
String implTitle,
String implVersion,
String mainClassName
) {
targetProject.apply plugin: "com.github.johnrengelman.plugin-shadow"
targetProject.jar {
manifest {
attributes "Implementation-Title": implTitle
attributes "Implementation-Version": implVersion
attributes "Main-Class": mainClassName
}
// Easiest way to make sure people don't use the output of the normal 'jar' task
archiveName = 'BROKEN-IGNORE'
}
File versionDir = file("${targetProject.buildDir}/git")
File versionFile = file("${versionDir}/version.json")
targetProject.task('writeVersionInformation') {
inputs.property('gitCommitId', gitProperties.'git.commit.id')
inputs.property('version', version)
outputs.file(versionFile)
doLast {
println targetProject.configurations.getByName("runtime").artifacts
versionDir.mkdirs()
def json = JsonOutput.toJson([
version: "${version}",
gitDescribe: "${gitProperties.'git.tags'}",
gitCommitId: "${gitProperties.'git.commit.id'}",
gitCommitShort: "${gitProperties.'git.commit.short'}",
gitCommitTime: "${gitProperties.'git.commit.time'}",
buildTime: "${gitProperties.'git.build.time'}"
]);
versionFile.write(JsonOutput.prettyPrint(json));
}
}
targetProject.shadowJar {
dependsOn targetProject.writeVersionInformation
from versionFile
mergeServiceFiles()
}
targetProject.assemble.dependsOn targetProject.shadowJar
}
Map getGitProperties() {
def outDateFmt = new SimpleDateFormat('yyyy-MM-dd hh:mm:ss a z')
def inDateFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss X")
def gitCmd = new File("${rootProject.projectDir}", ".git").exists() ?
"git --git-dir ${rootProject.projectDir}/.git --work-tree ${rootProject.projectDir}" : "git"
Map gitProps = [
'git.branch': "${gitCmd} symbolic-ref --short HEAD",
'git.build.user.email': "${gitCmd} config --get user.email",
'git.build.user.name': "${gitCmd} config --get user.name",
'git.commit.id': "${gitCmd} log -1 --pretty=format:%H",
'git.commit.id.abbrev': "${gitCmd} log -1 --pretty=format:%h",
'git.commit.short': "${gitCmd} rev-parse --short HEAD",
'git.commit.message.short': "${gitCmd} log -1 --pretty=format:%s",
'git.commit.user.email': "${gitCmd} log -1 --pretty=format:%ce",
'git.commit.user.name': "${gitCmd} log -1 --pretty=format:%cn",
'git.dirtyFiles': "${gitCmd} status --porcelain",
'git.remote.origin.url': "${gitCmd} ls-remote --get-url origin",
'git.tags': "${gitCmd} describe --tags"
].collectEntries { e -> [(e.key) : e.value.execute().text.trim()] }
String rawCommitTime = "${gitCmd} log -1 --pretty=format:%ci".execute().text.trim()
try {
gitProps.'git.commit.time' = outDateFmt.format(inDateFmt.parse(rawCommitTime))
} catch (Exception e) {
gitProps.'git.commit.time' = outDateFmt.format(new Date())
}
gitProps.'git.build.time' = outDateFmt.format(new Date())
gitProps.'git.dirtyFiles' = gitProps.'git.dirtyFiles'.tokenize('\n').collect({ x -> x.tokenize(' ')})
gitProps.'git.isClean' = gitProps.'git.dirtyFiles'.size() == 0
return gitProps
}