diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/reflectionUtil.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/reflectionUtil.kt new file mode 100644 index 00000000000..1d650157b72 --- /dev/null +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/reflectionUtil.kt @@ -0,0 +1,94 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.cli.common + +import kotlin.reflect.KClass +import kotlin.reflect.KParameter +import kotlin.reflect.KType + +fun tryConstructScriptClass(scriptClass: Class<*>, scriptArgs: List): Any? { + + fun convertPrimitive(type: KType?, arg: String): Any? = + when (type?.classifier) { + String::class -> arg + Int::class -> arg.toInt() + Long::class -> arg.toLong() + Short::class -> arg.toShort() + Byte::class -> arg.toByte() + Char::class -> arg[0] + Float::class -> arg.toFloat() + Double::class -> arg.toDouble() + Boolean::class -> arg.toBoolean() + else -> null + } + + fun convertArray(type: KType?, args: List): Any? = + when (type?.classifier) { + String::class -> args.toTypedArray() + Int::class -> args.map(String::toInt).toTypedArray() + Long::class -> args.map(String::toLong).toTypedArray() + Short::class -> args.map(String::toShort).toTypedArray() + Byte::class -> args.map(String::toByte).toTypedArray() + Char::class -> args.map { it[0] }.toTypedArray() + Float::class -> args.map(String::toFloat).toTypedArray() + Double::class -> args.map(String::toDouble).toTypedArray() + Boolean::class -> args.map(String::toBoolean).toTypedArray() + else -> null + } + + fun foldingFunc(state: Pair, List?>, par: KParameter): Pair, List?> { + state.second?.let { scriptArgsLeft -> + try { + if (scriptArgsLeft.isNotEmpty()) { + val primArgCandidate = convertPrimitive(par.type, scriptArgsLeft.first()) + if (primArgCandidate != null) + return@foldingFunc Pair(state.first + primArgCandidate, scriptArgsLeft.drop(1)) + } + + if ((par.type.classifier as? KClass<*>)?.qualifiedName == Array::class.qualifiedName) { + val arrCompType = par.type.arguments.getOrNull(0)?.type + val arrayArgCandidate = convertArray(arrCompType, scriptArgsLeft) + if (arrayArgCandidate != null) + return@foldingFunc Pair(state.first + arrayArgCandidate, null) + } + } + catch (e: NumberFormatException) { + } // just skips to return below + } + return state + } + + try { + return scriptClass.getConstructor(Array::class.java).newInstance(*arrayOf(scriptArgs.toTypedArray())) + } + catch (e: java.lang.NoSuchMethodException) { + for (ctor in scriptClass.kotlin.constructors) { + val (ctorArgs, scriptArgsLeft) = ctor.parameters.fold(Pair(emptyList(), scriptArgs), ::foldingFunc) + if (ctorArgs.size <= ctor.parameters.size && (scriptArgsLeft == null || scriptArgsLeft.isEmpty())) { + val argsMap = ctorArgs.zip(ctor.parameters) { a, p -> Pair(p, a) }.toMap() + try { + return ctor.callBy(argsMap) + } + catch (e: Exception) { // TODO: find the exact exception type thrown then callBy fails + } + } + } + } + return null +} + + diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt index bd88bc3cb23..f561cb8958c 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt @@ -237,7 +237,7 @@ class K2JVMCompiler : CLICompiler() { ): KotlinCoreEnvironment? { val scriptResolverEnv = hashMapOf() - configureScriptDefinitions(arguments, configuration, messageCollector, scriptResolverEnv) + configureScriptDefinitions(arguments.scriptTemplates, configuration, messageCollector, scriptResolverEnv) if (!messageCollector.hasErrors()) { val environment = createCoreEnvironment(rootDisposable, configuration) if (!messageCollector.hasErrors()) { @@ -248,50 +248,6 @@ class K2JVMCompiler : CLICompiler() { return null } - private fun configureScriptDefinitions(arguments: K2JVMCompilerArguments, - configuration: CompilerConfiguration, - messageCollector: MessageCollector, - scriptResolverEnv: HashMap) { - val classpath = configuration.getList(JVMConfigurationKeys.CONTENT_ROOTS).filterIsInstance(JvmClasspathRoot::class.java).mapNotNull { it.file } - // TODO: consider using escaping to allow kotlin escaped names in class names - if (arguments.scriptTemplates != null && arguments.scriptTemplates.isNotEmpty()) { - val classloader = URLClassLoader(classpath.map { it.toURI().toURL() }.toTypedArray(), this.javaClass.classLoader) - var hasErrors = false - for (template in arguments.scriptTemplates) { - try { - val cls = classloader.loadClass(template) - val def = KotlinScriptDefinitionFromTemplate(cls.kotlin, null, null, scriptResolverEnv) - configuration.add(JVMConfigurationKeys.SCRIPT_DEFINITIONS, def) - messageCollector.report( - CompilerMessageSeverity.INFO, - "Added script definition $template to configuration: files pattern = \"${def.scriptFilePattern}\", resolver = ${def.resolver?.javaClass?.name}", - CompilerMessageLocation.NO_LOCATION - ) - } - catch (ex: ClassNotFoundException) { - messageCollector.report( - CompilerMessageSeverity.ERROR, "Cannot find script definition template class $template", CompilerMessageLocation.NO_LOCATION - ) - hasErrors = true - } - catch (ex: Exception) { - messageCollector.report( - CompilerMessageSeverity.ERROR, "Error processing script definition template $template: ${ex.message}", CompilerMessageLocation.NO_LOCATION - ) - hasErrors = true - break - } - } - if (hasErrors) { - messageCollector.report( - CompilerMessageSeverity.LOGGING, "(Classpath used for templates loading: $classpath)", CompilerMessageLocation.NO_LOCATION - ) - return - } - } - configuration.add(JVMConfigurationKeys.SCRIPT_DEFINITIONS, StandardScriptDefinition) - } - private fun createCoreEnvironment(rootDisposable: Disposable, configuration: CompilerConfiguration): KotlinCoreEnvironment { val result = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES) @@ -431,6 +387,50 @@ class K2JVMCompiler : CLICompiler() { } return OK } + + fun configureScriptDefinitions(scriptTemplates: Array?, + configuration: CompilerConfiguration, + messageCollector: MessageCollector, + scriptResolverEnv: HashMap) { + val classpath = configuration.getList(JVMConfigurationKeys.CONTENT_ROOTS).filterIsInstance(JvmClasspathRoot::class.java).mapNotNull { it.file } + // TODO: consider using escaping to allow kotlin escaped names in class names + if (scriptTemplates != null && scriptTemplates.isNotEmpty()) { + val classloader = URLClassLoader(classpath.map { it.toURI().toURL() }.toTypedArray(), Thread.currentThread().contextClassLoader) + var hasErrors = false + for (template in scriptTemplates) { + try { + val cls = classloader.loadClass(template) + val def = KotlinScriptDefinitionFromTemplate(cls.kotlin, null, null, scriptResolverEnv) + configuration.add(JVMConfigurationKeys.SCRIPT_DEFINITIONS, def) + messageCollector.report( + CompilerMessageSeverity.INFO, + "Added script definition $template to configuration: files pattern = \"${def.scriptFilePattern}\", resolver = ${def.resolver?.javaClass?.name}", + CompilerMessageLocation.NO_LOCATION + ) + } + catch (ex: ClassNotFoundException) { + messageCollector.report( + CompilerMessageSeverity.ERROR, "Cannot find script definition template class $template", CompilerMessageLocation.NO_LOCATION + ) + hasErrors = true + } + catch (ex: Exception) { + messageCollector.report( + CompilerMessageSeverity.ERROR, "Error processing script definition template $template: ${ex.message}", CompilerMessageLocation.NO_LOCATION + ) + hasErrors = true + break + } + } + if (hasErrors) { + messageCollector.report( + CompilerMessageSeverity.LOGGING, "(Classpath used for templates loading: $classpath)", CompilerMessageLocation.NO_LOCATION + ) + return + } + } + configuration.add(JVMConfigurationKeys.SCRIPT_DEFINITIONS, StandardScriptDefinition) + } } } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt index 03e34916ebe..d835d0a0af8 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt @@ -34,6 +34,7 @@ import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys import org.jetbrains.kotlin.cli.common.ExitCode import org.jetbrains.kotlin.cli.common.messages.* import org.jetbrains.kotlin.cli.common.output.outputUtils.writeAll +import org.jetbrains.kotlin.cli.common.tryConstructScriptClass import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler import org.jetbrains.kotlin.cli.jvm.config.* import org.jetbrains.kotlin.codegen.ClassBuilderFactories @@ -66,10 +67,6 @@ import java.lang.reflect.InvocationTargetException import java.net.URLClassLoader import java.util.concurrent.TimeUnit import java.util.jar.Attributes -import kotlin.reflect.KParameter -import kotlin.reflect.KType -import kotlin.reflect.defaultType -import kotlin.reflect.jvm.javaType object KotlinToJVMBytecodeCompiler { @@ -230,7 +227,7 @@ object KotlinToJVMBytecodeCompiler { try { try { - tryConstructClass(scriptClass, scriptArgs) + tryConstructScriptClass(scriptClass, scriptArgs) ?: throw RuntimeException("unable to find appropriate constructor for class ${scriptClass.name} accepting arguments $scriptArgs\n") } finally { @@ -295,78 +292,6 @@ object KotlinToJVMBytecodeCompiler { } } - @TestOnly - fun tryConstructClassPub(scriptClass: Class<*>, scriptArgs: List): Any? = tryConstructClass(scriptClass, scriptArgs) - - private fun tryConstructClass(scriptClass: Class<*>, scriptArgs: List): Any? { - - fun convertPrimitive(type: KType?, arg: String): Any? = - when (type) { - String::class.defaultType -> arg - Int::class.defaultType -> arg.toInt() - Long::class.defaultType -> arg.toLong() - Short::class.defaultType -> arg.toShort() - Byte::class.defaultType -> arg.toByte() - Char::class.defaultType -> arg[0] - Float::class.defaultType -> arg.toFloat() - Double::class.defaultType -> arg.toDouble() - Boolean::class.defaultType -> arg.toBoolean() - else -> null - } - - fun convertArray(type: KType?, args: List): Any? = - when (type) { - String::class.defaultType -> args.toTypedArray() - Int::class.defaultType -> args.map { it.toInt() }.toTypedArray() - Long::class.defaultType -> args.map { it.toLong() }.toTypedArray() - Short::class.defaultType -> args.map { it.toShort() }.toTypedArray() - Byte::class.defaultType -> args.map { it.toByte() }.toTypedArray() - Char::class.defaultType -> args.map { it[0] }.toTypedArray() - Float::class.defaultType -> args.map { it.toFloat() }.toTypedArray() - Double::class.defaultType -> args.map { it.toDouble() }.toTypedArray() - Boolean::class.defaultType -> args.map { it.toBoolean() }.toTypedArray() - else -> null - } - - fun foldingFunc(state: Pair, List?>, par: KParameter): Pair, List?> { - state.second?.let { scriptArgsLeft -> - try { - if (scriptArgsLeft.isNotEmpty()) { - val primArgCandidate = convertPrimitive(par.type, scriptArgsLeft.first()) - if (primArgCandidate != null) - return@foldingFunc Pair(state.first + primArgCandidate, scriptArgsLeft.drop(1)) - } - - val arrCompType = (par.type.javaType as? Class<*>)?.componentType?.kotlin?.defaultType - val arrayArgCandidate = convertArray(arrCompType, scriptArgsLeft) - if (arrayArgCandidate != null) - return@foldingFunc Pair(state.first + arrayArgCandidate, null) - } - catch (e: NumberFormatException) { - } // just skips to return below - } - return state - } - - try { - return scriptClass.getConstructor(Array::class.java).newInstance(*arrayOf(scriptArgs.toTypedArray())) - } - catch (e: java.lang.NoSuchMethodException) { - for (ctor in scriptClass.kotlin.constructors) { - val (ctorArgs, scriptArgsLeft) = ctor.parameters.fold(Pair(emptyList(), scriptArgs), ::foldingFunc) - if (ctorArgs.size <= ctor.parameters.size && (scriptArgsLeft == null || scriptArgsLeft.isEmpty())) { - val argsMap = ctorArgs.zip(ctor.parameters) { a, p -> Pair(p, a) }.toMap() - try { - return ctor.callBy(argsMap) - } - catch (e: Exception) { // TODO: find the exact exception type thrown then callBy fails - } - } - } - } - return null - } - fun compileScript(environment: KotlinCoreEnvironment, paths: KotlinPaths): Class<*>? = compileScript(environment, { @@ -481,7 +406,7 @@ object KotlinToJVMBytecodeCompiler { configuration, GenerationState.GenerateClassFilter.GENERATE_ALL, module?.let(::TargetId), - module?.let { it.getModuleName() }, + module?.let(Module::getModuleName), module?.let { File(it.getOutputDirectory()) }, createOutputFilesFlushingCallbackIfPossible(configuration) ) diff --git a/compiler/tests/org/jetbrains/kotlin/scripts/ScriptTest.kt b/compiler/tests/org/jetbrains/kotlin/scripts/ScriptTest.kt index 7c378aab058..3ec39797002 100644 --- a/compiler/tests/org/jetbrains/kotlin/scripts/ScriptTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/scripts/ScriptTest.kt @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.scripts import com.intellij.openapi.util.Disposer import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys import org.jetbrains.kotlin.cli.common.messages.* +import org.jetbrains.kotlin.cli.common.tryConstructScriptClass import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler @@ -43,7 +44,7 @@ class ScriptTest : KtUsefulTestCase() { val aClass = compileScript("fib_std.kts", StandardScriptDefinition) Assert.assertNotNull(aClass) val out = captureOut { - val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, listOf("4", "comment")) + val anObj = tryConstructScriptClass(aClass!!, listOf("4", "comment")) Assert.assertNotNull(anObj) } Assert.assertEquals(NUM_4_LINE + " (comment)" + FIB_SCRIPT_OUTPUT_TAIL, out) @@ -54,7 +55,7 @@ class ScriptTest : KtUsefulTestCase() { val aClass = compileScript("fib_std.kts", StandardScriptDefinition) Assert.assertNotNull(aClass) val out = captureOut { - val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, emptyList()) + val anObj = tryConstructScriptClass(aClass!!, emptyList()) Assert.assertNotNull(anObj) } Assert.assertEquals(NUM_4_LINE + " (none)" + FIB_SCRIPT_OUTPUT_TAIL, out) @@ -67,7 +68,7 @@ class ScriptTest : KtUsefulTestCase() { val aClass = compileScript("fib_std.kts", StandardScriptDefinition, saveClassesDir = tmpdir) Assert.assertNotNull(aClass) val out1 = captureOut { - val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, emptyList()) + val anObj = tryConstructScriptClass(aClass!!, emptyList()) Assert.assertNotNull(anObj) } Assert.assertEquals(NUM_4_LINE + " (none)" + FIB_SCRIPT_OUTPUT_TAIL, out1) @@ -75,7 +76,7 @@ class ScriptTest : KtUsefulTestCase() { val aClassSaved = savedClassLoader.loadClass(aClass.name) Assert.assertNotNull(aClassSaved) val out2 = captureOut { - val anObjSaved = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClassSaved!!, emptyList()) + val anObjSaved = tryConstructScriptClass(aClassSaved!!, emptyList()) Assert.assertNotNull(anObjSaved) } Assert.assertEquals(NUM_4_LINE + " (none)" + FIB_SCRIPT_OUTPUT_TAIL, out2) @@ -134,4 +135,4 @@ class ScriptTest : KtUsefulTestCase() { } } -class TestParamClass(val memberNum: Int) \ No newline at end of file +class TestParamClass(@Suppress("unused") val memberNum: Int) \ No newline at end of file diff --git a/libraries/tools/kotlin-maven-plugin-test/pom.xml b/libraries/tools/kotlin-maven-plugin-test/pom.xml index a5fbbe650d4..28cd8b7a7b7 100644 --- a/libraries/tools/kotlin-maven-plugin-test/pom.xml +++ b/libraries/tools/kotlin-maven-plugin-test/pom.xml @@ -23,6 +23,16 @@ kotlin-reflect ${project.version} + + org.jetbrains.kotlin + kotlin-stdlib + ${project.version} + + + org.jetbrains.kotlin + kotlin-script-util + ${project.version} + org.jetbrains.kotlin kotlin-js-library @@ -75,6 +85,7 @@ local-repo + true verify false invoker.properties diff --git a/libraries/tools/kotlin-maven-plugin-test/src/it/test-executeKotlinScriptWithTemplate/pom.xml b/libraries/tools/kotlin-maven-plugin-test/src/it/test-executeKotlinScriptWithTemplate/pom.xml new file mode 100644 index 00000000000..73ca79427d8 --- /dev/null +++ b/libraries/tools/kotlin-maven-plugin-test/src/it/test-executeKotlinScriptWithTemplate/pom.xml @@ -0,0 +1,76 @@ + + + 4.0.0 + + org.jetbrains.kotlin + test-executeKotlinScriptFile + 1.0-SNAPSHOT + + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-script-util + ${kotlin.version} + + + + + org.jetbrains.kotlin.script.util.StandardScript + ${org.jetbrains.kotlin:kotlin-script-util:jar} + + + + ${project.basedir}/src/main/kotlin + + + org.apache.maven.plugins + maven-dependency-plugin + 2.3 + + + + properties + + + + + + kotlin-maven-plugin + org.jetbrains.kotlin + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-script-util + ${kotlin.version} + + + + + execute-kotlin-script + compile + + script + + + script.kts + + + + + + + + \ No newline at end of file diff --git a/libraries/tools/kotlin-maven-plugin-test/src/it/test-executeKotlinScriptWithTemplate/script.kts b/libraries/tools/kotlin-maven-plugin-test/src/it/test-executeKotlinScriptWithTemplate/script.kts new file mode 100644 index 00000000000..a43c85f2173 --- /dev/null +++ b/libraries/tools/kotlin-maven-plugin-test/src/it/test-executeKotlinScriptWithTemplate/script.kts @@ -0,0 +1,5 @@ + +if (!args.isEmpty()) + println("some args passed") + +println("Hello from Kotlin script file!") \ No newline at end of file diff --git a/libraries/tools/kotlin-maven-plugin-test/src/it/test-executeKotlinScriptWithTemplate/verify.bsh b/libraries/tools/kotlin-maven-plugin-test/src/it/test-executeKotlinScriptWithTemplate/verify.bsh new file mode 100644 index 00000000000..36249025565 --- /dev/null +++ b/libraries/tools/kotlin-maven-plugin-test/src/it/test-executeKotlinScriptWithTemplate/verify.bsh @@ -0,0 +1,3 @@ +source(new File(basedir, "../../../verify-common.bsh").getAbsolutePath()); + +assertBuildLogHasLine("Hello from Kotlin script file!") \ No newline at end of file diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/ExecuteKotlinScriptMojo.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/ExecuteKotlinScriptMojo.java index 6c17b065f7d..9ebd1d9b694 100644 --- a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/ExecuteKotlinScriptMojo.java +++ b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/ExecuteKotlinScriptMojo.java @@ -35,6 +35,7 @@ import org.apache.maven.plugins.annotations.ResolutionScope; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.component.repository.ComponentDependency; import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys; +import org.jetbrains.kotlin.cli.common.ReflectionUtilKt; import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler; import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles; import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment; @@ -49,16 +50,14 @@ import org.jetbrains.kotlin.config.KotlinSourceRoot; import org.jetbrains.kotlin.load.java.JvmAbi; import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.psi.KtScript; -import org.jetbrains.kotlin.script.StandardScriptDefinition; import org.jetbrains.kotlin.utils.PathUtil; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; /** @@ -108,6 +107,15 @@ public class ExecuteKotlinScriptMojo extends AbstractMojo { @Parameter(defaultValue = "${localRepository}", required = true, readonly = true) private ArtifactRepository localRepository; + @Parameter(property = "kotlin.compiler.scriptTemplates", required = false, readonly = false) + protected List scriptTemplates; + + @Parameter(property = "kotlin.compiler.scriptArguments", required = false, readonly = false) + protected List scriptArguments; + + @Parameter(property = "kotlin.compiler.scriptClasspath", required = false, readonly = false) + protected List scriptClasspath; + @Component private ArtifactHandlerManager artifactHandlerManager; @@ -176,7 +184,8 @@ public class ExecuteKotlinScriptMojo extends AbstractMojo { configuration.add(JVMConfigurationKeys.CONTENT_ROOTS, new KotlinSourceRoot(scriptFile.getAbsolutePath())); configuration.put(CommonConfigurationKeys.MODULE_NAME, JvmAbi.DEFAULT_MODULE_NAME); - configuration.add(JVMConfigurationKeys.SCRIPT_DEFINITIONS, StandardScriptDefinition.INSTANCE); + + K2JVMCompiler.Companion.configureScriptDefinitions(scriptTemplates.toArray(new String[scriptTemplates.size()]), configuration, messageCollector, new HashMap()); KotlinCoreEnvironment environment = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES); @@ -192,17 +201,9 @@ public class ExecuteKotlinScriptMojo extends AbstractMojo { try { Class klass = classLoader.loadClass(nameForScript.asString()); - Constructor constructor = klass.getConstructor(String[].class); ExecuteKotlinScriptMojo.INSTANCE = this; - constructor.newInstance(new Object[]{new String[]{}}); - } catch (InstantiationException e) { - throw new ScriptExecutionException(scriptFile, "internal error", e); - } catch (InvocationTargetException e) { - throw new ScriptExecutionException(scriptFile, "script threw an exception", e.getCause()); - } catch (NoSuchMethodException e) { - throw new ScriptExecutionException(scriptFile, "internal error", e); - } catch (IllegalAccessException e) { - throw new ScriptExecutionException(scriptFile, "internal error", e); + if (ReflectionUtilKt.tryConstructScriptClass(klass, scriptArguments) == null) + throw new ScriptExecutionException(scriptFile, "unable to construct script"); } catch (ClassNotFoundException e) { throw new ScriptExecutionException(scriptFile, "internal error", e); } @@ -221,6 +222,10 @@ public class ExecuteKotlinScriptMojo extends AbstractMojo { deps.addAll(getThisPluginDependencies()); + for (String cp: scriptClasspath) { + deps.add(new File(cp)); + } + return deps; } diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JVMCompileMojo.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JVMCompileMojo.java index 094d65ab144..84b8f1350f8 100644 --- a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JVMCompileMojo.java +++ b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JVMCompileMojo.java @@ -65,6 +65,9 @@ public class K2JVMCompileMojo extends KotlinCompileMojoBase scriptTemplates; + @NotNull @Override protected K2JVMCompiler createCompiler() { @@ -114,5 +117,9 @@ public class K2JVMCompileMojo extends KotlinCompileMojoBase