apply plugin: 'java'

defaultTasks 'execTask', 'execByMethod'

task execTask(type: Exec) {
    dependsOn sourceSets.main.runtimeClasspath
    testFile = file("$buildDir/$name")
    executable = Jvm.current().getJavaExecutable()
    workingDir = buildDir
    args '-cp', sourceSets.main.runtimeClasspath.asPath, 'org.gradle.TestMain', testFile.name
    doFirst {
        mkdir buildDir
    }
    doLast {
        assert testFile.exists()
    }
}

task execByMethod {
    dependsOn sourceSets.main.runtimeClasspath
    testFile = file("$buildDir/$name")
    doFirst {
        mkdir buildDir
        exec {
            executable = Jvm.current().getJavaExecutable()
            workingDir = buildDir
            args '-cp', sourceSets.main.runtimeClasspath.asPath, 'org.gradle.TestMain', testFile.name
        }
    }
    doLast {
        assert testFile.exists()
    }
}
