OnJava8-Examples/build.gradle

128 lines
4.7 KiB
Groovy
Raw Normal View History

2016-07-07 12:43:10 -06:00
subprojects {
apply plugin: 'java'
2015-12-15 15:35:04 -07:00
2016-07-07 12:43:10 -06:00
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
2015-12-15 15:35:04 -07:00
2016-07-07 12:43:10 -06:00
repositories {
mavenLocal()
mavenCentral()
}
2015-12-15 15:35:04 -07:00
2016-07-07 12:43:10 -06:00
sourceSets {
main {
java {
srcDir projectDir
}
2015-12-15 15:35:04 -07:00
}
}
2016-07-07 12:43:10 -06:00
List tasks = []
2015-12-15 15:35:04 -07:00
2016-07-07 12:43:10 -06:00
projectDir.eachFileRecurse {
if (it.name.endsWith('.java')) {
2015-12-15 15:35:04 -07:00
2016-07-07 12:43:10 -06:00
List lines = it.readLines()
2015-12-15 15:35:04 -07:00
2016-07-07 12:43:10 -06:00
Boolean hasMainMethod = lines.join('').contains('main(String[] args)')
Boolean hasMainParam = lines.find { it.startsWith('// {main: ')}
2015-12-15 15:35:04 -07:00
2016-07-07 12:43:10 -06:00
// add tasks for java sources with main methods
if (hasMainMethod || hasMainParam) {
2015-12-15 15:35:04 -07:00
2016-07-07 12:43:10 -06:00
String maybeArgsLine = lines.find { it.startsWith('// {Args: ')}
List maybeArgs = []
if (maybeArgsLine != null) {
maybeArgs = maybeArgsLine.trim().replaceAll('\\/\\/ \\{Args: ', '').reverse().replaceFirst('}', '').reverse().split(' ')
}
String mainClass = it.name.replaceAll('.java', '')
String taskName = mainClass
tasks.add(taskName)
// some java sources with a main method specify a different main class
String maybeMainLine = lines.find { it.startsWith('// {main: ')}
if (maybeMainLine != null) {
mainClass = maybeMainLine.trim().replaceAll('\\/\\/ \\{main: ', '').replaceAll('}', '')
}
// some java sources with JVM args
List maybeJvmArgs = []
String maybeJvmArgsLine = lines.find { it.startsWith('// {JVMArgs: ')}
if (maybeJvmArgsLine != null) {
maybeJvmArgs = maybeJvmArgsLine.trim().replaceAll('\\/\\/ \\{JVMArgs: ', '').replaceAll('}', '').split(' ')
}
2015-12-15 15:35:04 -07:00
2016-07-07 12:43:10 -06:00
// some java sources depend on others to be run first
String maybeRunFirst = null
String maybeRunFirstLine = lines.find { it.startsWith('// {RunFirst: ')}
if (maybeRunFirstLine != null) {
maybeRunFirst = maybeRunFirstLine.trim().replaceAll('\\/\\/ \\{RunFirst: ', '').replaceAll('}', '')
}
task "$taskName"(type: JavaExec, dependsOn: maybeRunFirst) {
main = mainClass
classpath = sourceSets.main.runtimeClasspath
args = maybeArgs
jvmArgs = maybeJvmArgs
}
Integer expectedOutputStartLine = lines.findIndexOf { it.startsWith('/* Output:') }
Integer expectedOutputEndLine = -1
List expectedOutputLines = []
if (expectedOutputStartLine != -1) {
expectedOutputStartLine += 1
expectedOutputEndLine = lines.findIndexOf(expectedOutputStartLine, { it.startsWith('*/') })
if (expectedOutputEndLine == -1) {
expectedOutputEndLine = expectedOutputStartLine
}
expectedOutputLines = lines.subList(expectedOutputStartLine, expectedOutputEndLine)
}
OutputStream testStandardOutput = new ByteArrayOutputStream()
OutputStream testErrorOutput = new ByteArrayOutputStream()
task "test$taskName"(type: JavaExec, dependsOn: maybeRunFirst) {
main = mainClass
classpath = sourceSets.main.runtimeClasspath
args = maybeArgs
jvmArgs = maybeJvmArgs
standardOutput = testStandardOutput
errorOutput = testErrorOutput
doLast {
// test the output here
println("Expected")
println(expectedOutputLines)
println("STDOUT")
println(testStandardOutput.toString().readLines())
println("STDERR")
println(testErrorOutput.toString().readLines())
throw new Exception("Expected output did not match the actual output")
}
}
}
// exclude java sources that will not compile
if (lines.find { it.startsWith('// {CompileTimeError') } != null) {
sourceSets.main.java.excludes.add(it.name)
}
}
}
task run(dependsOn: tasks)
List testTasks = tasks.collect { "test" + it }
task test(overwrite:true, dependsOn: testTasks)
2015-12-15 15:35:04 -07:00
}
2016-07-07 12:43:10 -06:00
configure(subprojects - project(':onjava')) {
dependencies {
compile project(':onjava')
}
}