forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.gradle
More file actions
383 lines (349 loc) · 11 KB
/
build.gradle
File metadata and controls
383 lines (349 loc) · 11 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
buildscript {
repositories {
mavenLocal()
jcenter()
mavenCentral()
}
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M2'
}
}
plugins {
id 'com.github.johnrengelman.shadow' version '1.2.3'
id 'me.champeau.gradle.jmh' version '0.3.1'
}
import org.gradle.internal.jvm.Jvm
import org.apache.tools.ant.util.TeeOutputStream
boolean debug = false
class Tags {
Boolean hasMainMethod = false
Boolean compileTimeError = false
Boolean throwsException = false
Boolean errorOutputExpected = false
Boolean validateByHand = false
Boolean ignoreOutput = false // This tag isn't used in the build...
String fileRoot
String mainClass
String javaCmd = null
List<String> args = []
List<String> jVMArgs = []
String javap = null
String runFirst = null
String outputLine = null
private String block
def Tags(File file) {
block = file.text
hasMainMethod = block.contains('main(String[] args)')
def firstLine = block.substring(0, block.indexOf("\n"))
fileRoot = (firstLine.split("/")[-1] - ".java").trim() // Remove \r if it exists
mainClass = fileRoot
javaCmd = extract('java')
if(javaCmd) {
def pieces = javaCmd.split()
mainClass = pieces[0]
if(pieces.size() > 1)
for(p in pieces[1..-1])
if(p.startsWith("-"))
jVMArgs << p
else
args << p
}
compileTimeError = hasTag('CompileTimeError')
throwsException = hasTag('ThrowsException')
errorOutputExpected = hasTag('ErrorOutputExpected')
validateByHand = hasTag('ValidateByHand')
ignoreOutput = hasTag('IgnoreOutput')
javap = extract('javap') // Includes only arguments to command
runFirst = extract('RunFirst:')
outputLine = extractOutputLine()
}
private def hasTag(String marker) {
return block.contains("// {" + marker + "}")
}
def extractOutputLine() {
def matcher = (block =~ /(?m)^(\/\* Output:.*)$/)
if (matcher) {
return matcher[0][1]
} else {
return null
}
}
private def extract(String marker) {
// Assume some whitespace is after marker
if(!block.contains("// {${marker} "))
return null
def matcher = (block =~ /\/\/ \{${marker}\s+([^}]+)/)
if (matcher) {
def matched = matcher[0][1].trim()
return matched.replaceAll("\n?//", "")
} else {
println "Searching for: " + matcher
println block
System.exit(1)
}
}
public boolean hasTags() {
return compileTimeError ||
throwsException ||
errorOutputExpected ||
validateByHand ||
ignoreOutput ||
javaCmd ||
args ||
jVMArgs ||
javap ||
runFirst
}
public String toString() {
String result = ""
block.eachLine{ ln ->
if(ln.startsWith("//") || ln.startsWith("package "))
result += ln + "\n"
}
"""
hasMainMethod
compileTimeError
throwsException
errorOutputExpected
validateByHand
ignoreOutput
fileRoot
mainClass
javaCmd
args
jVMArgs
javap
runFirst
""".split().each { str ->
if(this[str])
result += str + ": " + this[str] + "\n"
}
result
}
}
ext {
junitJupiterVersion = '5.0.0-M2'
}
subprojects {
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'me.champeau.gradle.jmh'
apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'
//apply plugin: 'checkstyle'
//apply plugin: 'findbugs'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
repositories {
mavenLocal()
jcenter()
mavenCentral()
}
dependencies {
// Logging:
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.+'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.+'
// You can also use the JDK's built-in logging as the back end:
// compile group: 'org.slf4j', name: 'slf4j-jdk14', version: '1.7.5'
// JUnit testing:
compile "org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}"
testCompile "org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}"
testRuntime "org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}"
}
junitPlatform {
includeClassNamePattern '.*'
}
// See: http://blog.jessitron.com/2012/07/using-checkstyle-in-gradle.html
// https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml
// http://checkstyle.sourceforge.net/reports/google-java-style.html
/* checkstyle {
// configFile = new File(rootDir, "checkstyle.xml")
toolVersion = '6.7'
}*/
/* findbugsMain {
reports {
xml.enabled = false
html.enabled = true
}
ignoreFailures = true
}
findbugsJmh {
reports {
xml.enabled = false
html.enabled = true
}
ignoreFailures = true
}
*/
sourceSets {
main {
java {
srcDir projectDir
exclude "tests/**"
}
resources {
srcDir projectDir
include '*.xml'
}
}
jmh {
java {
srcDir projectDir
}
}
test {
java {
srcDir new File(projectDir, "tests")
}
}
}
jmh {
jmhVersion = '1.13'
duplicateClassesStrategy = 'warn'
}
List createdTasks = []
projectDir.eachFileRecurse { file ->
if (file.name.endsWith('.java')) {
Tags tags = new Tags(file)
if(debug && tags.hasTags()) println tags
// Exclude java sources that will not compile
if (tags.compileTimeError) {
sourceSets.main.java.excludes.add(file.name)
} else {
JavaExec javaTask = null
// Add tasks for java sources with main methods
if (tags.hasMainMethod || tags.javaCmd) {
javaTask = tasks.create(name: tags.fileRoot, type: JavaExec, dependsOn: tags.runFirst) {
main = tags.mainClass
classpath = sourceSets.main.runtimeClasspath
args = tags.args
jvmArgs = tags.jVMArgs
}
} else if (tags.javap) {
// Create task for running javap
javaTask = tasks.create(name: "${tags.fileRoot}", type: JavaExec, dependsOn: tags.runFirst) {
main = "com.sun.tools.javap.Main"
classpath = sourceSets.main.runtimeClasspath + files(Jvm.current().toolsJar)
// Assuming javap represents all the args and there's no need to jVMArgs
args tags.javap.split()
}
}
if (javaTask) {
def baseName = file.name.substring(0, file.name.lastIndexOf('.'))
File outFile = new File(file.parentFile, baseName + '.out')
File errFile = new File(file.parentFile, baseName + '.err')
javaTask.configure {
ignoreExitValue = tags.validateByHand || tags.throwsException
doFirst {
if(outFile.exists())
outFile.delete()
if(tags.outputLine)
outFile << tags.outputLine + "\n"
standardOutput = new TeeOutputStream(new FileOutputStream(outFile, true), System.out)
errorOutput = new TeeOutputStream(new FileOutputStream(errFile), System.err)
}
doLast {
if(outFile.size() == 0)
outFile.delete()
else if(!outFile.text.contains("/* Output:"))
outFile.delete()
if(errFile.size() == 0) errFile.delete()
}
}
if (!tags.validateByHand) {
// Only add tasks that we know we can run successfully to the task list
createdTasks.add(javaTask)
}
}
}
}
}
task run(dependsOn: createdTasks)
/* Store test output in $projectName/tests
JUnit 5's junitPlatformTest runs as a "javaExec" rather than a "test",
so we can't hook into the before/after test behavior.
*/
tasks.findByPath(":$name:junitPlatformTest").configure {
File testDir = new File(project.name + "/tests")
if(testDir.exists()) {
File outFile = new File(testDir, 'report.txt')
doFirst {
standardOutput = new TeeOutputStream(new FileOutputStream(outFile, true), System.out)
}
doLast {
if(outFile.size() == 0)
outFile.delete()
else if(outFile.text.contains("0 tests found"))
outFile.delete()
}
}
}
}
project(':validating') {
jmh {
include = 'validating.jmh.*'
}
}
project(':understandingcollections') {
dependencies {
compile project(':typeinfo')
compile project(':collections')
}
jmh {
include = 'understandingcollections.jmh.*'
}
}
project(':threads') {
dependencies {
compile project(':enums')
}
}
project(':strings') {
dependencies {
compile project(':generics')
}
}
project(':serialization') {
configurations.all {
resolutionStrategy {
force 'xml-apis:xml-apis:1.0.b2'
}
}
dependencies {
compile 'com.io7m.xom:xom:1.2.10'
}
}
project(':interfaces') {
dependencies {
compile project(':polymorphism')
}
}
project(':hiding') {
dependencies {
compile project(':com')
}
}
project(':generics') {
dependencies {
compile project(':typeinfo')
}
}
project(':collections') {
dependencies {
compile project(':typeinfo')
}
}
configure(subprojects - project(':onjava')) {
dependencies {
compile project(':onjava')
compile group: 'com.google.guava', name: 'guava', version: '19.0'
compile "org.openjdk.jmh:jmh-core:${jmh.jmhVersion}"
compile 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M2'
}
}
task verify(type:Exec) {
description 'Uses Python tool to verify example output'
commandLine 'python', '_verify_output.py'
doFirst {
println("execute 'gradlew run' first")
}
}