Ant task: support 'failOnError' attribute in kotlinc and withKotlin

This commit is contained in:
Alexander Udalov
2015-06-02 17:17:59 +03:00
parent 6d00c265e2
commit 0ad6dce7ce
8 changed files with 72 additions and 9 deletions
@@ -56,6 +56,7 @@ public class KotlinCompilerAdapter extends Javac13 {
Javac javac = getJavac(); Javac javac = getJavac();
Kotlin2JvmTask kotlinc = new Kotlin2JvmTask(); Kotlin2JvmTask kotlinc = new Kotlin2JvmTask();
kotlinc.setFailOnError(javac.getFailonerror());
kotlinc.setOutput(javac.getDestdir()); kotlinc.setOutput(javac.getDestdir());
Path classpath = javac.getClasspath(); Path classpath = javac.getClasspath();
@@ -70,6 +71,10 @@ public class KotlinCompilerAdapter extends Javac13 {
kotlinc.setExternalAnnotations(externalAnnotations); kotlinc.setExternalAnnotations(externalAnnotations);
kotlinc.execute(); kotlinc.execute();
if (!Integer.valueOf(0).equals(kotlinc.getExitCode())) {
// Don't run javac if failOnError = false and there were errors on Kotlin sources
return false;
}
javac.log("Running javac..."); javac.log("Running javac...");
@@ -16,16 +16,16 @@
package org.jetbrains.kotlin.ant package org.jetbrains.kotlin.ant
import org.apache.tools.ant.AntClassLoader
import org.apache.tools.ant.BuildException
import org.apache.tools.ant.Task import org.apache.tools.ant.Task
import org.apache.tools.ant.types.Commandline
import org.apache.tools.ant.types.Path import org.apache.tools.ant.types.Path
import org.apache.tools.ant.types.Reference import org.apache.tools.ant.types.Reference
import java.io.File
import org.apache.tools.ant.BuildException
import org.apache.tools.ant.types.Commandline
import java.io.PrintStream
import org.apache.tools.ant.AntClassLoader
import java.lang.ref.SoftReference
import org.jetbrains.kotlin.preloading.ClassPreloadingUtils import org.jetbrains.kotlin.preloading.ClassPreloadingUtils
import java.io.File
import java.io.PrintStream
import java.lang.ref.SoftReference
import java.net.JarURLConnection import java.net.JarURLConnection
object CompilerClassLoaderHolder { object CompilerClassLoaderHolder {
@@ -66,11 +66,14 @@ public abstract class KotlinCompilerBaseTask : Task() {
public var nowarn: Boolean = false public var nowarn: Boolean = false
public var verbose: Boolean = false public var verbose: Boolean = false
public var printVersion: Boolean = false public var printVersion: Boolean = false
public var failOnError: Boolean = false
public var noStdlib: Boolean = false public var noStdlib: Boolean = false
public val additionalArguments: MutableList<Commandline.Argument> = arrayListOf() public val additionalArguments: MutableList<Commandline.Argument> = arrayListOf()
internal var exitCode: Int? = null
public fun createSrc(): Path { public fun createSrc(): Path {
val srcPath = src val srcPath = src
if (srcPath == null) { if (srcPath == null) {
@@ -118,10 +121,10 @@ public abstract class KotlinCompilerBaseTask : Task() {
log("Compiling ${src!!.list().toList()} => [${output!!.canonicalPath}]"); log("Compiling ${src!!.list().toList()} => [${output!!.canonicalPath}]");
val exitCode = exec(compiler, System.err, args.copyToArray()) val result = exec(compiler, System.err, args.toTypedArray())
exitCode = (result as Enum<*>).ordinal()
// TODO: support failOnError attribute of javac if (failOnError && exitCode != 0) {
if ((exitCode as Enum<*>).ordinal() != 0) {
throw BuildException("Compile failed; see the compiler error output for details.") throw BuildException("Compile failed; see the compiler error output for details.")
} }
} }
@@ -0,0 +1,3 @@
package hello;
public class IncorrectJavaCode {
@@ -0,0 +1,21 @@
OUT:
Buildfile: [TestData]/build.xml
build:
[javac] Compiling 2 source files to [Temp]
[javac] Compiling [[TestData]] => [[Temp]]
[javac] ERROR: [TestData]/incorrectKotlinCode.kt: (4, 1) Parameter name expected
[javac] ERROR: [TestData]/incorrectKotlinCode.kt: (4, 1) Expecting comma or ')'
[javac] ERROR: [TestData]/incorrectKotlinCode.kt: (4, 1) Expecting ')'
[kotlinc] Compiling [[TestData]] => [[Temp]]
[kotlinc] ERROR: [TestData]/incorrectKotlinCode.kt: (4, 1) Parameter name expected
[kotlinc] ERROR: [TestData]/incorrectKotlinCode.kt: (4, 1) Expecting comma or ')'
[kotlinc] ERROR: [TestData]/incorrectKotlinCode.kt: (4, 1) Expecting ')'
BUILD SUCCESSFUL
Total time: [time]
ERR:
[javac] Compile failed; see the compiler error output for details.
Return code: 0
@@ -0,0 +1,11 @@
<project name="Ant Task Test" default="build">
<taskdef resource="org/jetbrains/kotlin/ant/antlib.xml" classpath="${kotlin.lib}/kotlin-ant.jar"/>
<target name="build">
<javac srcdir="${test.data}" destdir="${temp}" includeantruntime="false" failonerror="false">
<withKotlin/>
</javac>
<kotlinc src="${test.data}" output="${temp}" failonerror="false"/>
</target>
</project>
@@ -0,0 +1,12 @@
ERR:
Exception in thread "main" java.lang.NoClassDefFoundError: hello/HelloPackage
Caused by: java.lang.ClassNotFoundException: hello.HelloPackage
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Return code: 1
@@ -0,0 +1,3 @@
package hello
fun main(
@@ -71,6 +71,11 @@ public class AntTaskJvmTest extends AntTaskBaseTest {
doJvmAntTest(); doJvmAntTest();
} }
@Test
public void doNotFailOnError() throws Exception {
doJvmAntTest();
}
@Test @Test
public void jvmClasspath() throws Exception { public void jvmClasspath() throws Exception {
doJvmAntTest(); doJvmAntTest();