OnJava8-Examples/build.gradle

239 lines
7.3 KiB
Groovy
Raw Normal View History

2016-08-03 13:24:58 -06:00
buildscript {
repositories {
mavenLocal()
jcenter()
mavenCentral()
}
}
plugins {
id 'me.champeau.gradle.jmh' version '0.3.0'
}
import org.gradle.internal.jvm.Jvm
2016-07-08 15:39:40 -06:00
import org.apache.tools.ant.util.TeeOutputStream
2016-08-02 09:12:22 -06:00
boolean debug = false
2016-07-07 16:45:20 -06:00
2016-07-13 16:34:14 -06:00
class Tags {
2016-07-28 12:48:23 -06:00
Boolean hasMainMethod = false
2016-07-13 16:34:14 -06:00
Boolean compileTimeError = false
Boolean throwsException = false
Boolean errorOutputExpected = false
Boolean validateByHand = false
2016-07-14 18:18:02 -06:00
Boolean ignoreOutput = false // This tag isn't used in the build...
2016-07-14 13:29:44 -06:00
String fileRoot
String mainClass
2016-07-13 16:34:14 -06:00
String javaCmd = null
2016-07-14 12:23:33 -06:00
List<String> args = []
2016-07-14 13:43:54 -06:00
List<String> jVMArgs = []
2016-07-28 16:47:35 -06:00
String javap = null
2016-07-13 16:34:14 -06:00
String runFirst = null
2016-07-18 14:46:40 -06:00
String outputLine = null
2016-07-13 16:34:14 -06:00
private String block
def Tags(File file) {
block = file.text
2016-07-28 12:48:23 -06:00
hasMainMethod = block.contains('main(String[] args)')
def firstLine = block.substring(0, block.indexOf("\n"))
2016-08-01 23:05:22 -07:00
fileRoot = (firstLine.split("/")[-1] - ".java").trim() // Remove \r if it exists
2016-07-14 13:29:44 -06:00
mainClass = fileRoot
2016-07-27 17:31:28 -06:00
javaCmd = extract('java')
if(javaCmd) {
2016-07-28 12:48:23 -06:00
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
2016-07-27 17:31:28 -06:00
}
2016-07-28 12:48:23 -06:00
compileTimeError = hasTag('CompileTimeError')
throwsException = hasTag('ThrowsException')
errorOutputExpected = hasTag('ErrorOutputExpected')
validateByHand = hasTag('ValidateByHand')
ignoreOutput = hasTag('IgnoreOutput')
2016-07-28 16:55:57 -06:00
javap = extract('javap') // Includes only arguments to command
2016-07-13 16:34:14 -06:00
runFirst = extract('RunFirst:')
outputLine = extractOutputLine()
2016-07-13 16:34:14 -06:00
}
2016-07-28 12:48:23 -06:00
private def hasTag(String marker) {
2016-07-13 16:34:14 -06:00
return block.contains("// {" + marker + "}")
}
def extractOutputLine() {
def matcher = (block =~ /(?m)^(\/\* Output:.*)$/)
if (matcher) {
return matcher[0][1]
} else {
return null
}
}
2016-07-13 16:34:14 -06:00
private def extract(String marker) {
// Assume some whitespace is after marker
if(!block.contains("// {${marker} "))
2016-07-13 16:34:14 -06:00
return null
def matcher = (block =~ /\/\/ \{${marker}\s+([^}]+)/)
if (matcher) {
def matched = matcher[0][1].trim()
return matched.replaceAll("\n?//", "")
2016-07-13 16:34:14 -06:00
} else {
println "Searching for: " + matcher
2016-07-13 16:34:14 -06:00
println block
System.exit(1)
}
}
2016-07-14 05:59:30 -06:00
public boolean hasTags() {
return compileTimeError ||
throwsException ||
errorOutputExpected ||
validateByHand ||
ignoreOutput ||
javaCmd ||
args ||
jVMArgs ||
2016-07-28 16:47:35 -06:00
javap ||
2016-07-14 05:59:30 -06:00
runFirst
}
2016-07-13 16:34:14 -06:00
public String toString() {
String result = ""
block.eachLine{ ln ->
2016-07-14 05:59:30 -06:00
if(ln.startsWith("//") || ln.startsWith("package "))
result += ln + "\n"
}
2016-07-14 18:18:02 -06:00
"""
2016-07-28 12:48:23 -06:00
hasMainMethod
2016-07-14 18:18:02 -06:00
compileTimeError
throwsException
errorOutputExpected
validateByHand
ignoreOutput
fileRoot
mainClass
javaCmd
args
jVMArgs
2016-07-28 16:47:35 -06:00
javap
2016-07-14 18:18:02 -06:00
runFirst
""".split().each { str ->
if(this[str])
result += str + ": " + this[str] + "\n"
}
result
2016-07-13 16:34:14 -06:00
}
}
2016-07-07 12:43:10 -06:00
subprojects {
2016-08-03 13:24:58 -06:00
apply plugin: 'me.champeau.gradle.jmh'
2016-07-07 12:43:10 -06:00
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()
2016-08-05 14:39:40 -06:00
jcenter()
2016-07-07 12:43:10 -06:00
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-08-05 13:55:39 -06:00
jmh {
java {
srcDir projectDir
}
}
}
jmh {
2016-08-09 11:42:12 -06:00
jmhVersion = '1.13'
2015-12-15 15:35:04 -07:00
}
List createdTasks = []
2015-12-15 15:35:04 -07:00
projectDir.eachFileRecurse { file ->
if (file.name.endsWith('.java')) {
2015-12-15 15:35:04 -07:00
Tags tags = new Tags(file)
2016-07-14 18:43:34 -06:00
if(debug && tags.hasTags()) println tags
2015-12-15 15:35:04 -07:00
// Exclude java sources that will not compile
if (tags.compileTimeError) {
2016-07-31 10:21:28 -07:00
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()
}
2016-07-07 12:43:10 -06:00
}
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()
}
2016-07-19 15:42:38 -06:00
}
if (!tags.validateByHand) {
// Only add tasks that we know we can run successfully to the task list
createdTasks.add(javaTask)
2016-07-19 15:42:38 -06:00
}
2016-07-07 12:43:10 -06:00
}
}
}
}
task run(dependsOn: createdTasks)
2015-12-15 15:35:04 -07:00
}
2016-07-07 12:43:10 -06:00
configure(subprojects - project(':onjava')) {
dependencies {
compile project(':onjava')
compile "org.openjdk.jmh:jmh-core:${jmh.jmhVersion}"
2016-08-09 16:15:05 -06:00
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.6.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6.2'
2016-07-07 12:43:10 -06:00
}
}
2016-07-22 17:26:49 -06:00
2016-08-02 11:00:06 -06:00
task verify(type:Exec) {
2016-07-26 10:28:19 -06:00
commandLine 'python', 'verify_output.py'
2016-08-02 11:00:06 -06:00
doFirst {
println("execute 'gradlew run' first")
}
2016-07-22 17:26:49 -06:00
}