From 8bfa3718110dcc5e3d531c35613d5d6649caa4a5 Mon Sep 17 00:00:00 2001 From: Nikita Skvortsov Date: Wed, 27 Mar 2013 13:28:27 +0400 Subject: [PATCH 1/4] add kotlinOptions to tasks with type kotlinCompile. Allows to set kotlin compiler options directly --- libraries/tools/kotlin-gradle-plugin/pom.xml | 5 +++ .../jetbrains/kotlin/gradle/tasks/Tasks.kt | 36 +++++++++++-------- .../resources/testProject/alfa/build.gradle | 6 ++++ .../com/google/common/base/annotations.xml | 18 ++++++++++ .../alfa/src/main/kotlin/helloWorld.kt | 4 +-- 5 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/alfa/externalAnnotations/com/google/common/base/annotations.xml diff --git a/libraries/tools/kotlin-gradle-plugin/pom.xml b/libraries/tools/kotlin-gradle-plugin/pom.xml index af6d7e7f215..f01aa3e1063 100644 --- a/libraries/tools/kotlin-gradle-plugin/pom.xml +++ b/libraries/tools/kotlin-gradle-plugin/pom.xml @@ -26,6 +26,11 @@ 4.11 test + + commons-lang + commons-lang + 2.3 + com.google.guava guava diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt index 5504585efe0..da3f962b9d4 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt @@ -30,6 +30,7 @@ import org.gradle.api.logging.Logger import org.gradle.api.logging.Logging import org.apache.commons.io.FileUtils import java.io.IOException +import org.apache.commons.lang.StringUtils public open class KotlinCompile(): AbstractCompile() { @@ -37,6 +38,9 @@ public open class KotlinCompile(): AbstractCompile() { val compiler = K2JVMCompiler() val logger = getLogger() + public val kotlinOptions: K2JVMCompilerArguments = K2JVMCompilerArguments() + + // override setSource to track source directory sets override fun setSource(source: Any?) { srcDirsRoots.clear() @@ -70,7 +74,7 @@ public open class KotlinCompile(): AbstractCompile() { override fun compile() { - val args = K2JVMCompilerArguments() + val args = kotlinOptions val javaSrcRoots = HashSet() val sources = ArrayList() @@ -92,19 +96,21 @@ public open class KotlinCompile(): AbstractCompile() { return } - args.setSourceDirs(sources.map { it.getAbsolutePath() }) + val customSources = args.getSourceDirs(); + if (customSources == null || customSources.isEmpty()) { + args.setSourceDirs(sources.map { it.getAbsolutePath() }) + } - val gradleClasspath = getClasspath() - val effectiveClassPath = (javaSrcRoots + gradleClasspath).makeString(File.pathSeparator) - args.setClasspath(effectiveClassPath) + if (StringUtils.isEmpty(kotlinOptions.classpath)) { + val gradleClasspath = getClasspath() + val effectiveClassPath = (javaSrcRoots + gradleClasspath).makeString(File.pathSeparator) + args.setClasspath(effectiveClassPath) + } val embeddedAnnotations = getAnnotations() - - args.outputDir = getDestinationDir()?.getPath() - args.noJdkAnnotations = true - args.annotations = embeddedAnnotations.getPath() - args.noStdlib = true + args.outputDir =if (StringUtils.isEmpty(kotlinOptions.outputDir)) { getDestinationDir()?.getPath() } else { kotlinOptions.outputDir } + args.annotations = if (StringUtils.isEmpty(kotlinOptions.annotations)) { embeddedAnnotations.getPath() } else { kotlinOptions.annotations } val logger = Logging.getLogger(getClass()) val messageCollector = GradleMessageCollector(logger) @@ -127,7 +133,7 @@ public open class KotlinCompile(): AbstractCompile() { fun getAnnotations(): File { val jdkAnnotations: String = "kotlin-jdk-annotations.jar" val jdkAnnotationsResource = Resources.getResource(jdkAnnotations) ?: - throw GradleException(jdkAnnotations + " not found in Kotlin gradle plugin classpath") + throw GradleException(jdkAnnotations + " not found in Kotlin gradle plugin classpath") val jdkAnnotationsTempDir = Files.createTempDir() val jdkAnnotationsFile = File(jdkAnnotationsTempDir, jdkAnnotations) @@ -231,10 +237,10 @@ class GradleMessageCollector(val logger : Logger): MessageCollector { val text = position + message when (severity) { - in CompilerMessageSeverity.VERBOSE -> logger.debug(text) - in CompilerMessageSeverity.ERRORS -> logger.error(text) - CompilerMessageSeverity.INFO -> logger.info(text) - else -> logger.warn(text) + in CompilerMessageSeverity.VERBOSE -> logger.debug(text) + in CompilerMessageSeverity.ERRORS -> logger.error(text) + CompilerMessageSeverity.INFO -> logger.info(text) + else -> logger.warn(text) } } } diff --git a/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/alfa/build.gradle b/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/alfa/build.gradle index 9c5b7f1b94e..df08432fede 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/alfa/build.gradle +++ b/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/alfa/build.gradle @@ -32,6 +32,12 @@ task show << { buildscript.configurations.classpath.each { println it } } + +compileKotlin { + kotlinOptions.annotations = "externalAnnotations" +} + + task wrapper(type: Wrapper) { gradleVersion="1.4" } \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/alfa/externalAnnotations/com/google/common/base/annotations.xml b/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/alfa/externalAnnotations/com/google/common/base/annotations.xml new file mode 100644 index 00000000000..be8ddab04a3 --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/alfa/externalAnnotations/com/google/common/base/annotations.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/alfa/src/main/kotlin/helloWorld.kt b/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/alfa/src/main/kotlin/helloWorld.kt index 710e6b06dc4..b3e99bbdd59 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/alfa/src/main/kotlin/helloWorld.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/alfa/src/main/kotlin/helloWorld.kt @@ -13,8 +13,8 @@ class KotlinGreetingJoiner(val greeter : Greeter) { } fun getJoinedGreeting() : String? { - val joiner = Joiner.on(" and ")?.skipNulls(); - return "${greeter.getGreeting()} ${joiner?.join(names)}" + val joiner = Joiner.on(" and ").skipNulls(); + return "${greeter.getGreeting()} ${joiner.join(names)}" } } From a876ea9fd016d0592e358d90ffad516827d93095 Mon Sep 17 00:00:00 2001 From: Nikita Skvortsov Date: Wed, 27 Mar 2013 13:35:02 +0400 Subject: [PATCH 2/4] added kotlin plugin name --- libraries/tools/kotlin-gradle-plugin/pom.xml | 3 +++ .../main/resources/META-INF/gradle-plugins/kotlin.properties | 1 + .../src/test/resources/testProject/alfa/build.gradle | 3 +-- 3 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 libraries/tools/kotlin-gradle-plugin/src/main/resources/META-INF/gradle-plugins/kotlin.properties diff --git a/libraries/tools/kotlin-gradle-plugin/pom.xml b/libraries/tools/kotlin-gradle-plugin/pom.xml index f01aa3e1063..ff7cb884e39 100644 --- a/libraries/tools/kotlin-gradle-plugin/pom.xml +++ b/libraries/tools/kotlin-gradle-plugin/pom.xml @@ -76,6 +76,9 @@ kotlin-jdk-annotations.jar false + + ${project.basedir}/src/main/resources + diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/resources/META-INF/gradle-plugins/kotlin.properties b/libraries/tools/kotlin-gradle-plugin/src/main/resources/META-INF/gradle-plugins/kotlin.properties new file mode 100644 index 00000000000..4f8cd030af6 --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin/src/main/resources/META-INF/gradle-plugins/kotlin.properties @@ -0,0 +1 @@ +implementation-class=org.jetbrains.kotlin.gradle.plugin.KotlinPlugin \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/alfa/build.gradle b/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/alfa/build.gradle index df08432fede..76b840fbf4e 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/alfa/build.gradle +++ b/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/alfa/build.gradle @@ -1,4 +1,3 @@ -import org.jetbrains.kotlin.gradle.plugin.* buildscript { repositories { mavenCentral() @@ -11,7 +10,7 @@ buildscript { } } -apply plugin: KotlinPlugin +apply plugin: "kotlin" sourceSets { deploy From e4b5165e8aa73bed1292211403564fc9d88351dc Mon Sep 17 00:00:00 2001 From: Nikita Skvortsov Date: Wed, 27 Mar 2013 14:40:02 +0400 Subject: [PATCH 3/4] use jdk annotations from maven repository --- .../org/gradle/api/annotations.xml | 5 +++ .../api/initialization/dsl/annotations.xml | 7 ++++ .../org/gradle/api/internal/annotations.xml | 5 +++ libraries/tools/kotlin-gradle-plugin/pom.xml | 5 +++ .../jetbrains/kotlin/gradle/tasks/Tasks.kt | 32 +++++++++---------- 5 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 libraries/tools/kotlin-gradle-plugin/kotlinAnnotation/org/gradle/api/initialization/dsl/annotations.xml diff --git a/libraries/tools/kotlin-gradle-plugin/kotlinAnnotation/org/gradle/api/annotations.xml b/libraries/tools/kotlin-gradle-plugin/kotlinAnnotation/org/gradle/api/annotations.xml index bef387a3e38..dc5cd1bbcbe 100644 --- a/libraries/tools/kotlin-gradle-plugin/kotlinAnnotation/org/gradle/api/annotations.xml +++ b/libraries/tools/kotlin-gradle-plugin/kotlinAnnotation/org/gradle/api/annotations.xml @@ -10,4 +10,9 @@ + + + + + \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/kotlinAnnotation/org/gradle/api/initialization/dsl/annotations.xml b/libraries/tools/kotlin-gradle-plugin/kotlinAnnotation/org/gradle/api/initialization/dsl/annotations.xml new file mode 100644 index 00000000000..a33a584b9ed --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin/kotlinAnnotation/org/gradle/api/initialization/dsl/annotations.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/kotlinAnnotation/org/gradle/api/internal/annotations.xml b/libraries/tools/kotlin-gradle-plugin/kotlinAnnotation/org/gradle/api/internal/annotations.xml index 763aaf5197f..c14ebed20ae 100644 --- a/libraries/tools/kotlin-gradle-plugin/kotlinAnnotation/org/gradle/api/internal/annotations.xml +++ b/libraries/tools/kotlin-gradle-plugin/kotlinAnnotation/org/gradle/api/internal/annotations.xml @@ -7,4 +7,9 @@ + + + + + \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/pom.xml b/libraries/tools/kotlin-gradle-plugin/pom.xml index ff7cb884e39..f3602e2ce62 100644 --- a/libraries/tools/kotlin-gradle-plugin/pom.xml +++ b/libraries/tools/kotlin-gradle-plugin/pom.xml @@ -52,6 +52,11 @@ kotlin-compiler ${project.version} + + org.jetbrains.kotlin + kotlin-jdk-annotations + ${project.version} + org.jetbrains.kotlin kdoc diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt index da3f962b9d4..720b177b824 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt @@ -31,12 +31,13 @@ import org.gradle.api.logging.Logging import org.apache.commons.io.FileUtils import java.io.IOException import org.apache.commons.lang.StringUtils +import org.gradle.api.initialization.dsl.ScriptHandler public open class KotlinCompile(): AbstractCompile() { val srcDirsRoots = HashSet() val compiler = K2JVMCompiler() - val logger = getLogger() + val logger = Logging.getLogger(getClass()) public val kotlinOptions: K2JVMCompilerArguments = K2JVMCompilerArguments() @@ -112,17 +113,9 @@ public open class KotlinCompile(): AbstractCompile() { args.outputDir =if (StringUtils.isEmpty(kotlinOptions.outputDir)) { getDestinationDir()?.getPath() } else { kotlinOptions.outputDir } args.annotations = if (StringUtils.isEmpty(kotlinOptions.annotations)) { embeddedAnnotations.getPath() } else { kotlinOptions.annotations } - val logger = Logging.getLogger(getClass()) val messageCollector = GradleMessageCollector(logger) val exitCode = compiler.exec(messageCollector, args) - val annotationsDir = embeddedAnnotations.getParentFile() - try { - FileUtils.deleteDirectory(annotationsDir); - } catch (e : IOException) { - logger.warn("Could not delete temporary annotations directory " + annotationsDir, e) - } - when (exitCode) { ExitCode.COMPILATION_ERROR -> throw GradleException("Compilation error. See log for more details") ExitCode.INTERNAL_ERROR -> throw GradleException("Internal compiler error. See log for more details") @@ -131,20 +124,26 @@ public open class KotlinCompile(): AbstractCompile() { } fun getAnnotations(): File { - val jdkAnnotations: String = "kotlin-jdk-annotations.jar" - val jdkAnnotationsResource = Resources.getResource(jdkAnnotations) ?: - throw GradleException(jdkAnnotations + " not found in Kotlin gradle plugin classpath") + val configuration = getProject() + .getBuildscript() + .getConfigurations() + .getByName(ScriptHandler.CLASSPATH_CONFIGURATION)!! - val jdkAnnotationsTempDir = Files.createTempDir() - val jdkAnnotationsFile = File(jdkAnnotationsTempDir, jdkAnnotations) + val jdkAnnotationsFromClasspath = configuration.find { it.name.startsWith("kotlin-jdk-annotations") } - Files.copy(Resources.newInputStreamSupplier(jdkAnnotationsResource), jdkAnnotationsFile) - return jdkAnnotationsFile + if (jdkAnnotationsFromClasspath != null) { + logger.info("using jdk annontations from [${jdkAnnotationsFromClasspath.getAbsolutePath()}]") + return jdkAnnotationsFromClasspath + } else { + throw GradleException("kotlin-jdk-annotations not found in Kotlin gradle plugin classpath") + } } } public open class KDoc(): SourceTask() { + val logger = Logging.getLogger(getClass()) + /** * Returns the directory to use to output the API docs */ @@ -216,7 +215,6 @@ public open class KDoc(): SourceTask() { val compiler = KDocCompiler() - val logger = Logging.getLogger(getClass()) val messageCollector = GradleMessageCollector(logger) val exitCode = compiler.exec(messageCollector, args); From b9fc66ec0b34904e72795eaabfb41eba79159d2e Mon Sep 17 00:00:00 2001 From: Nikita Skvortsov Date: Wed, 27 Mar 2013 15:07:13 +0400 Subject: [PATCH 4/4] filter out non-existent directories from class path --- .../kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt index 720b177b824..7422d5f491d 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt @@ -39,8 +39,12 @@ public open class KotlinCompile(): AbstractCompile() { val compiler = K2JVMCompiler() val logger = Logging.getLogger(getClass()) - public val kotlinOptions: K2JVMCompilerArguments = K2JVMCompilerArguments() + public val kotlinOptions: K2JVMCompilerArguments = K2JVMCompilerArguments(); + { + kotlinOptions.noStdlib = true + kotlinOptions.noJdkAnnotations = true + } // override setSource to track source directory sets override fun setSource(source: Any?) { @@ -104,8 +108,8 @@ public open class KotlinCompile(): AbstractCompile() { if (StringUtils.isEmpty(kotlinOptions.classpath)) { - val gradleClasspath = getClasspath() - val effectiveClassPath = (javaSrcRoots + gradleClasspath).makeString(File.pathSeparator) + val existingClasspathEntries = getClasspath().filter(KSpec({ it != null && it.exists() })) + val effectiveClassPath = (javaSrcRoots + existingClasspathEntries).makeString(File.pathSeparator) args.setClasspath(effectiveClassPath) }