CLI: Allow to specify JDK for classpath building with '-jdk' compiler argument.

Maven: support jdk parameter linked to kotlin.compiler.jdk property.
Gradle: support jdk compiler option.
This commit is contained in:
Ilya Gorbunov
2016-06-16 22:00:07 +03:00
parent 1157e052ee
commit 644df89dc9
7 changed files with 35 additions and 8 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* 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.
@@ -31,6 +31,10 @@ public class K2JVMCompilerArguments extends CommonCompilerArguments {
@Argument(value = "include-runtime", description = "Include Kotlin runtime in to resulting .jar")
public boolean includeRuntime;
@Argument(value = "jdk", description = "Path to JDK home to include into classpath, if differs from default %JAVA_HOME%")
@ValueDescription("<path>")
public String jdk;
@Argument(value = "no-jdk", description = "Don't include Java runtime into classpath")
public boolean noJdk;
@@ -62,7 +62,19 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
try {
if (!arguments.noJdk) {
configuration.addJvmClasspathRoots(PathUtil.getJdkClassesRoots())
if (arguments.jdk != null) {
configuration.addJvmClasspathRoots(PathUtil.getJdkClassesRoots(File(arguments.jdk)))
}
else {
configuration.addJvmClasspathRoots(PathUtil.getJdkClassesRoots())
}
}
else {
if (arguments.jdk != null) {
messageCollector.report(CompilerMessageSeverity.WARNING,
"The '-jdk' option with a path to JDK is ignored because '-no-jdk' is specified",
CompilerMessageLocation.NO_LOCATION)
}
}
}
catch (t: Throwable) {
+1
View File
@@ -3,6 +3,7 @@ where possible options include:
-d <directory|jar> Destination for generated class files
-classpath (-cp) <path> Paths where to find user class files
-include-runtime Include Kotlin runtime in to resulting .jar
-jdk <path> Path to JDK home to include into classpath, if differs from default %JAVA_HOME%
-no-jdk Don't include Java runtime into classpath
-no-stdlib Don't include Kotlin runtime into classpath
-module <path> Path to the module file to compile
+1
View File
@@ -4,6 +4,7 @@ where possible options include:
-d <directory|jar> Destination for generated class files
-classpath (-cp) <path> Paths where to find user class files
-include-runtime Include Kotlin runtime in to resulting .jar
-jdk <path> Path to JDK home to include into classpath, if differs from default %JAVA_HOME%
-no-jdk Don't include Java runtime into classpath
-no-stdlib Don't include Kotlin runtime into classpath
-module <path> Path to the module file to compile
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* 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.
@@ -144,4 +144,9 @@ public class PathUtil {
public static List<File> getJdkClassesRoots() {
return JavaSdkUtil.getJdkClassesRoots(new File(System.getProperty("java.home")), true);
}
@NotNull
public static List<File> getJdkClassesRoots(@NotNull File jdkHome) {
return JavaSdkUtil.getJdkClassesRoots(jdkHome, false);
}
}
@@ -195,6 +195,7 @@ open class KotlinCompile() : AbstractKotlinCompile<K2JVMCompilerArguments>() {
logger.kotlinDebug("args.pluginOptions = ${args.pluginOptions.joinToString(File.pathSeparator)}")
args.noStdlib = true
args.jdk = kotlinOptions.jdk
args.noJdk = kotlinOptions.noJdk
args.noInline = kotlinOptions.noInline
args.noOptimize = kotlinOptions.noOptimize
@@ -16,11 +16,7 @@
package org.jetbrains.kotlin.maven;
import kotlin.collections.CollectionsKt;
import kotlin.jvm.functions.Function1;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
@@ -30,7 +26,6 @@ import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments;
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import static com.intellij.openapi.util.text.StringUtil.join;
@@ -67,6 +62,9 @@ public class K2JVMCompileMojo extends KotlinCompileMojoBase<K2JVMCompilerArgumen
@Parameter(property = "kotlin.compiler.jvmTarget", required = false, readonly = false)
protected String jvmTarget;
@Parameter(property = "kotlin.compiler.jdk", required = false, readonly = false)
protected String jdk;
@NotNull
@Override
protected K2JVMCompiler createCompiler() {
@@ -111,5 +109,10 @@ public class K2JVMCompileMojo extends KotlinCompileMojoBase<K2JVMCompilerArgumen
arguments.languageVersion = languageVersion;
arguments.jvmTarget = jvmTarget;
if (jdk != null) {
getLog().info("Overriding JDK path with: " + jdk);
arguments.jdk = jdk;
}
}
}