diff --git a/ant/src/org/jetbrains/kotlin/ant/KotlinAntTaskUtil.kt b/ant/src/org/jetbrains/kotlin/ant/KotlinAntTaskUtil.kt index 51a33855f77..498f10e1afa 100644 --- a/ant/src/org/jetbrains/kotlin/ant/KotlinAntTaskUtil.kt +++ b/ant/src/org/jetbrains/kotlin/ant/KotlinAntTaskUtil.kt @@ -21,10 +21,36 @@ import org.jetbrains.kotlin.preloading.ClassPreloadingUtils import java.io.File import java.lang.ref.SoftReference import java.net.JarURLConnection +import kotlin.properties.Delegates object KotlinAntTaskUtil { private var classLoaderRef = SoftReference(null) + private val libPath: File by Delegates.lazy { + // Find path of kotlin-ant.jar in the filesystem and find kotlin-compiler.jar in the same directory + val resourcePath = "/" + javaClass.getName().replace('.', '/') + ".class" + val jarConnection = javaClass.getResource(resourcePath).openConnection() as? JarURLConnection + ?: throw UnsupportedOperationException("Kotlin compiler Ant task should be loaded from the JAR file") + val antTaskJarPath = File(jarConnection.getJarFileURL().toURI()) + + antTaskJarPath.getParentFile() + } + + val compilerJar: File by Delegates.lazy { + File(libPath, "kotlin-compiler.jar").assertExists() + } + + val runtimeJar: File by Delegates.lazy { + File(libPath, "kotlin-runtime.jar").assertExists() + } + + private fun File.assertExists(): File { + if (!this.exists()) { + throw IllegalStateException("${getName()} is not found in the directory of Kotlin Ant task") + } + return this + } + synchronized fun getOrCreateClassLoader(): ClassLoader { val cached = classLoaderRef.get() if (cached != null) return cached @@ -32,18 +58,7 @@ object KotlinAntTaskUtil { val myLoader = javaClass.getClassLoader() if (myLoader !is AntClassLoader) return myLoader - // Find path of kotlin-ant.jar in the filesystem and find kotlin-compiler.jar in the same directory - val resourcePath = "/" + javaClass.getName().replace('.', '/') + ".class" - val jarConnection = javaClass.getResource(resourcePath).openConnection() as? JarURLConnection - ?: throw UnsupportedOperationException("Kotlin compiler Ant task should be loaded from the JAR file") - val antTaskJarPath = File(jarConnection.getJarFileURL().toURI()) - - val compilerJarPath = File(antTaskJarPath.getParent(), "kotlin-compiler.jar") - if (!compilerJarPath.exists()) { - throw IllegalStateException("kotlin-compiler.jar is not found in the directory of Kotlin Ant task") - } - - val classLoader = ClassPreloadingUtils.preloadClasses(listOf(compilerJarPath), 4096, myLoader, null) + val classLoader = ClassPreloadingUtils.preloadClasses(listOf(compilerJar), 4096, myLoader, null) classLoaderRef = SoftReference(classLoader) return classLoader diff --git a/ant/src/org/jetbrains/kotlin/ant/KotlinCompilerAdapter.java b/ant/src/org/jetbrains/kotlin/ant/KotlinCompilerAdapter.java index 36e5b14dde9..cf98304708b 100644 --- a/ant/src/org/jetbrains/kotlin/ant/KotlinCompilerAdapter.java +++ b/ant/src/org/jetbrains/kotlin/ant/KotlinCompilerAdapter.java @@ -87,6 +87,11 @@ public class KotlinCompilerAdapter extends Javac13 { // Javac13#execute passes everything in compileList to javac, which doesn't recognize .kt files compileList = filterOutKotlinSources(compileList); + if (compileClasspath == null) { + compileClasspath = new Path(getProject()); + } + compileClasspath.add(new Path(getProject(), KotlinAntTaskUtil.INSTANCE$.getRuntimeJar().getAbsolutePath())); + return compileList.length == 0 || super.execute(); } diff --git a/compiler/testData/integration/ant/jvm/externalAnnotations/build.xml b/compiler/testData/integration/ant/jvm/externalAnnotations/build.xml index 59389201291..58c5c85af24 100644 --- a/compiler/testData/integration/ant/jvm/externalAnnotations/build.xml +++ b/compiler/testData/integration/ant/jvm/externalAnnotations/build.xml @@ -1,17 +1,10 @@ - - - - - - - + - diff --git a/compiler/testData/integration/ant/jvm/javacCompiler/build.log.expected b/compiler/testData/integration/ant/jvm/javacCompiler/build.log.expected deleted file mode 100644 index b0482150f40..00000000000 --- a/compiler/testData/integration/ant/jvm/javacCompiler/build.log.expected +++ /dev/null @@ -1,14 +0,0 @@ -OUT: -Buildfile: [TestData]/build.xml - -build: - [mkdir] Created dir: [Temp]/classes - [javac] Compiling 2 source files to [Temp]/classes - [javac] Compiling [[TestData]/root1] => [[Temp]/classes] - [javac] Running javac... - [jar] Building jar: [Temp]/hello.jar - -BUILD SUCCESSFUL -Total time: [time] - -Return code: 0 diff --git a/compiler/testData/integration/ant/jvm/javacCompiler/build.xml b/compiler/testData/integration/ant/jvm/javacCompiler/build.xml deleted file mode 100644 index 4881190d012..00000000000 --- a/compiler/testData/integration/ant/jvm/javacCompiler/build.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - - - - diff --git a/compiler/testData/integration/ant/jvm/javacCompiler/hello.run.expected b/compiler/testData/integration/ant/jvm/javacCompiler/hello.run.expected deleted file mode 100644 index 3f9a7b7bf67..00000000000 --- a/compiler/testData/integration/ant/jvm/javacCompiler/hello.run.expected +++ /dev/null @@ -1,5 +0,0 @@ -OUT: -Hello, a! -Java - -Return code: 0 diff --git a/compiler/testData/integration/ant/jvm/javacCompiler/root1/hello.kt b/compiler/testData/integration/ant/jvm/javacCompiler/root1/hello.kt deleted file mode 100644 index 2976267a1d5..00000000000 --- a/compiler/testData/integration/ant/jvm/javacCompiler/root1/hello.kt +++ /dev/null @@ -1,7 +0,0 @@ -package hello - -fun main(args : Array) { - for (s in arrayListOf("a")) - println("Hello, $s!") - j.Java().f(); -} diff --git a/compiler/testData/integration/ant/jvm/javacCompiler/root1/j/Java.java b/compiler/testData/integration/ant/jvm/javacCompiler/root1/j/Java.java deleted file mode 100644 index 1c2d8c763e7..00000000000 --- a/compiler/testData/integration/ant/jvm/javacCompiler/root1/j/Java.java +++ /dev/null @@ -1,14 +0,0 @@ -package j; - -import java.lang.String; -import java.lang.System; - -public class Java { - public void f() { - System.out.println("Java"); - } - - public static void main(String[] args) { - hello.HelloPackage.main(new String[] {}); - } -} \ No newline at end of file diff --git a/compiler/testData/integration/ant/jvm/kotlinCompiler/build.log.expected b/compiler/testData/integration/ant/jvm/kotlinCompiler/build.log.expected deleted file mode 100644 index ab5952486d8..00000000000 --- a/compiler/testData/integration/ant/jvm/kotlinCompiler/build.log.expected +++ /dev/null @@ -1,13 +0,0 @@ -OUT: -Buildfile: [TestData]/build.xml - -build: - [mkdir] Created dir: [Temp]/classes - [kotlinc] Compiling [[TestData]/root1] => [[Temp]/classes] - [javac] Compiling 1 source file to [Temp]/classes - [jar] Building jar: [Temp]/hello.jar - -BUILD SUCCESSFUL -Total time: [time] - -Return code: 0 diff --git a/compiler/testData/integration/ant/jvm/kotlinCompiler/build.xml b/compiler/testData/integration/ant/jvm/kotlinCompiler/build.xml deleted file mode 100644 index a0212759f37..00000000000 --- a/compiler/testData/integration/ant/jvm/kotlinCompiler/build.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/compiler/testData/integration/ant/jvm/kotlinCompiler/hello.run.expected b/compiler/testData/integration/ant/jvm/kotlinCompiler/hello.run.expected deleted file mode 100644 index f36d0378368..00000000000 --- a/compiler/testData/integration/ant/jvm/kotlinCompiler/hello.run.expected +++ /dev/null @@ -1,5 +0,0 @@ -OUT: -Hello, a! -Java Hello - -Return code: 0 diff --git a/compiler/testData/integration/ant/jvm/kotlinCompiler/root1/a/j/annotations.xml b/compiler/testData/integration/ant/jvm/kotlinCompiler/root1/a/j/annotations.xml deleted file mode 100644 index 81524f5e075..00000000000 --- a/compiler/testData/integration/ant/jvm/kotlinCompiler/root1/a/j/annotations.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/compiler/testData/integration/ant/jvm/kotlinCompiler/root1/b/j/annotations.xml b/compiler/testData/integration/ant/jvm/kotlinCompiler/root1/b/j/annotations.xml deleted file mode 100644 index b2d319ef8aa..00000000000 --- a/compiler/testData/integration/ant/jvm/kotlinCompiler/root1/b/j/annotations.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/compiler/testData/integration/ant/jvm/kotlinCompiler/root1/hello.kt b/compiler/testData/integration/ant/jvm/kotlinCompiler/root1/hello.kt deleted file mode 100644 index 97065421b25..00000000000 --- a/compiler/testData/integration/ant/jvm/kotlinCompiler/root1/hello.kt +++ /dev/null @@ -1,9 +0,0 @@ -package hello - -fun main(args : Array) { - for (s in arrayListOf("a")) - println("Hello, $s!") - val java: String = j.Java().f() - val hello: String = j.Java().f2() - println("$java $hello") -} diff --git a/compiler/testData/integration/ant/jvm/kotlinCompiler/root1/j/Java.java b/compiler/testData/integration/ant/jvm/kotlinCompiler/root1/j/Java.java deleted file mode 100644 index ce5f7282990..00000000000 --- a/compiler/testData/integration/ant/jvm/kotlinCompiler/root1/j/Java.java +++ /dev/null @@ -1,20 +0,0 @@ -package j; - -import org.jetbrains.annotations.NotNull; - -import java.lang.String; -import java.lang.System; - -public class Java { - public String f() { - return "Java"; - } - - public String f2() { - return "Hello"; - } - - public static void main(String[] args) { - hello.HelloPackage.main(new String[] {}); - } -} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/integration/AntTaskBaseTest.java b/compiler/tests/org/jetbrains/kotlin/integration/AntTaskBaseTest.java index 9b6e4b4692a..432ce7a90e5 100644 --- a/compiler/tests/org/jetbrains/kotlin/integration/AntTaskBaseTest.java +++ b/compiler/tests/org/jetbrains/kotlin/integration/AntTaskBaseTest.java @@ -16,14 +16,10 @@ package org.jetbrains.kotlin.integration; -import com.intellij.util.ArrayUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.cli.CliBaseTest; import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; import static org.junit.Assert.assertEquals; @@ -33,8 +29,8 @@ public abstract class AntTaskBaseTest extends KotlinIntegrationTestBase { protected static final int SUCCESSFUL = 0; protected static final int FAILED = 1; - protected void doAntTest(int expectedExitCode, String... extraArgs) throws Exception { - assertEquals("Compilation failed", expectedExitCode, runAnt("build.log", "build.xml", extraArgs)); + protected void doAntTest(int expectedExitCode) throws Exception { + assertEquals("Compilation failed", expectedExitCode, runAnt("build.log", "build.xml")); } @Override @@ -43,20 +39,18 @@ public abstract class AntTaskBaseTest extends KotlinIntegrationTestBase { .replaceAll("Total time: .+\n", "Total time: [time]\n"); } - private int runAnt(String logName, String scriptName, String... extraArgs) throws Exception { - String[] basicArgs = { + private int runAnt(@NotNull String logName, @NotNull String scriptName) throws Exception { + return runJava( + logName, "-jar", getAntHome() + File.separator + "lib" + File.separator + "ant-launcher.jar", "-Dkotlin.lib=" + getCompilerLib(), "-Dtest.data=" + getTestDataDir(), "-Dtemp=" + tmpdir.getTmpDir(), "-f", scriptName - }; - List strings = new ArrayList(); - strings.addAll(Arrays.asList(basicArgs)); - strings.addAll(Arrays.asList(extraArgs)); - return runJava(logName, ArrayUtil.toStringArray(strings)); + ); } + @NotNull private static String getAntHome() { return getKotlinProjectHome().getAbsolutePath() + File.separator + "dependencies" + File.separator + "ant-1.8"; } diff --git a/compiler/tests/org/jetbrains/kotlin/integration/AntTaskJvmTest.java b/compiler/tests/org/jetbrains/kotlin/integration/AntTaskJvmTest.java index 8ac3eb7daa0..b82d2b4d7c8 100644 --- a/compiler/tests/org/jetbrains/kotlin/integration/AntTaskJvmTest.java +++ b/compiler/tests/org/jetbrains/kotlin/integration/AntTaskJvmTest.java @@ -38,8 +38,8 @@ public class AntTaskJvmTest extends AntTaskBaseTest { return new File(new File(ANT_TASK_TEST_DATA_BASE_DIR, "jvm"), name.getMethodName()); } - private void doJvmAntTest(String... extraJavaArgs) throws Exception { - doAntTest(SUCCESSFUL, extraJavaArgs); + private void doJvmAntTest() throws Exception { + doAntTest(SUCCESSFUL); String classpath = UtilsPackage.join(Arrays.asList( getOutputFileByName(JVM_OUT_FILE).getAbsolutePath(), @@ -50,17 +50,6 @@ public class AntTaskJvmTest extends AntTaskBaseTest { runJava("hello.run", "-cp", classpath, "hello.HelloPackage"); } - private static String getClassPathForAnt() { - return UtilsPackage.join(Arrays.asList( - getCompilerLib() + File.separator + "kotlin-compiler.jar", - getCompilerLib() + File.separator + "kotlin-ant.jar" - ), File.pathSeparator); - } - - private static String getIdeaSdkHome() { - return getKotlinProjectHome().getAbsolutePath() + File.separator + "ideaSDK"; - } - @Test public void helloWorld() throws Exception { doJvmAntTest(); @@ -111,23 +100,8 @@ public class AntTaskJvmTest extends AntTaskBaseTest { doJvmAntTest(); } - @Test - public void javacCompiler() throws Exception { - doJvmAntTest("-cp", getClassPathForAnt(), - "-Dkotlin.home", getCompilerLib().getAbsolutePath()); - } - @Test public void externalAnnotations() throws Exception { - doJvmAntTest("-cp", getClassPathForAnt(), - "-Didea.sdk", getIdeaSdkHome(), - "-Dkotlin.home", getCompilerLib().getAbsolutePath()); - } - - @Test - public void kotlinCompiler() throws Exception { - doJvmAntTest("-cp", getClassPathForAnt(), - "-Didea.sdk", getIdeaSdkHome(), - "-Dkotlin.home", getCompilerLib().getAbsolutePath()); + doJvmAntTest(); } }