Kotlin Facet: Change module JDK according to jdkHome
It's possible when JDK with the same home path is already configured
This commit is contained in:
@@ -125,6 +125,7 @@ class KotlinMavenImporter : MavenImporter(KOTLIN_PLUGIN_GROUP_ID, KOTLIN_PLUGIN_
|
|||||||
when (arguments) {
|
when (arguments) {
|
||||||
is K2JVMCompilerArguments -> {
|
is K2JVMCompilerArguments -> {
|
||||||
arguments.classpath = configuration?.getChild("classpath")?.text
|
arguments.classpath = configuration?.getChild("classpath")?.text
|
||||||
|
arguments.jdkHome = configuration?.getChild("jdkHome")?.text
|
||||||
arguments.jvmTarget = configuration?.getChild("jvmTarget")?.text ?: mavenProject.properties["kotlin.compiler.jvmTarget"]?.toString()
|
arguments.jvmTarget = configuration?.getChild("jvmTarget")?.text ?: mavenProject.properties["kotlin.compiler.jvmTarget"]?.toString()
|
||||||
}
|
}
|
||||||
is K2JSCompilerArguments -> {
|
is K2JSCompilerArguments -> {
|
||||||
@@ -163,9 +164,9 @@ class KotlinMavenImporter : MavenImporter(KOTLIN_PLUGIN_GROUP_ID, KOTLIN_PLUGIN_
|
|||||||
val executionArguments = mavenPlugin.executions?.filter { it.goals.any { it in compilationGoals } }
|
val executionArguments = mavenPlugin.executions?.filter { it.goals.any { it in compilationGoals } }
|
||||||
?.firstOrNull()
|
?.firstOrNull()
|
||||||
?.configurationElement?.let { getCompilerArgumentsByConfigurationElement(mavenProject, it, configuredPlatform) }
|
?.configurationElement?.let { getCompilerArgumentsByConfigurationElement(mavenProject, it, configuredPlatform) }
|
||||||
parseCompilerArgumentsToFacet(sharedArguments, emptyList(), kotlinFacet)
|
parseCompilerArgumentsToFacet(sharedArguments, emptyList(), kotlinFacet, modifiableModelsProvider)
|
||||||
if (executionArguments != null) {
|
if (executionArguments != null) {
|
||||||
parseCompilerArgumentsToFacet(executionArguments, emptyList(), kotlinFacet)
|
parseCompilerArgumentsToFacet(executionArguments, emptyList(), kotlinFacet, modifiableModelsProvider)
|
||||||
}
|
}
|
||||||
MavenProjectImportHandler.getInstances(module.project).forEach { it(kotlinFacet, mavenProject) }
|
MavenProjectImportHandler.getInstances(module.project).forEach { it(kotlinFacet, mavenProject) }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,11 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.maven
|
package org.jetbrains.kotlin.idea.maven
|
||||||
|
|
||||||
|
import com.intellij.openapi.application.Result
|
||||||
|
import com.intellij.openapi.application.WriteAction
|
||||||
|
import com.intellij.openapi.projectRoots.JavaSdk
|
||||||
|
import com.intellij.openapi.projectRoots.ProjectJdkTable
|
||||||
|
import com.intellij.openapi.roots.ModuleRootManager
|
||||||
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
|
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
|
||||||
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
|
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
|
||||||
import org.jetbrains.kotlin.config.*
|
import org.jetbrains.kotlin.config.*
|
||||||
@@ -1645,6 +1650,73 @@ class KotlinMavenImporterTest : MavenImportingTestCase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun testJDKImport() {
|
||||||
|
object : WriteAction<Unit>() {
|
||||||
|
override fun run(result: Result<Unit>) {
|
||||||
|
val jdk = JavaSdk.getInstance().createJdk("myJDK", "my/path/to/jdk")
|
||||||
|
ProjectJdkTable.getInstance().addJdk(jdk)
|
||||||
|
}
|
||||||
|
}.execute()
|
||||||
|
|
||||||
|
try {
|
||||||
|
createProjectSubDirs("src/main/kotlin", "src/main/kotlin.jvm", "src/test/kotlin", "src/test/kotlin.jvm")
|
||||||
|
|
||||||
|
importProject("""
|
||||||
|
<groupId>test</groupId>
|
||||||
|
<artifactId>project</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-stdlib</artifactId>
|
||||||
|
<version>$kotlinVersion</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<sourceDirectory>src/main/kotlin</sourceDirectory>
|
||||||
|
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-maven-plugin</artifactId>
|
||||||
|
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>compile</id>
|
||||||
|
<phase>compile</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>compile</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<jdkHome>my/path/to/jdk</jdkHome>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
""")
|
||||||
|
|
||||||
|
assertModules("project")
|
||||||
|
assertImporterStatePresent()
|
||||||
|
|
||||||
|
val moduleSDK = ModuleRootManager.getInstance(getModule("project")).sdk!!
|
||||||
|
Assert.assertTrue(moduleSDK.sdkType is JavaSdk)
|
||||||
|
Assert.assertEquals("myJDK", moduleSDK.name)
|
||||||
|
Assert.assertEquals("my/path/to/jdk", moduleSDK.homePath)
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
object : WriteAction<Unit>() {
|
||||||
|
override fun run(result: Result<Unit>) {
|
||||||
|
val jdkTable = ProjectJdkTable.getInstance()
|
||||||
|
jdkTable.removeJdk(jdkTable.findJdk("myJDK"))
|
||||||
|
}
|
||||||
|
}.execute()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun assertImporterStatePresent() {
|
private fun assertImporterStatePresent() {
|
||||||
assertNotNull("Kotlin importer component is not present", myTestFixture.module.getComponent(KotlinImporterComponent::class.java))
|
assertNotNull("Kotlin importer component is not present", myTestFixture.module.getComponent(KotlinImporterComponent::class.java))
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -139,7 +139,7 @@ private fun configureFacetByGradleModule(
|
|||||||
val defaultCompilerArguments = argsInfo.defaultArguments
|
val defaultCompilerArguments = argsInfo.defaultArguments
|
||||||
val dependencyClasspath = argsInfo.dependencyClasspath.map { PathUtil.toSystemIndependentName(it) }
|
val dependencyClasspath = argsInfo.dependencyClasspath.map { PathUtil.toSystemIndependentName(it) }
|
||||||
if (currentCompilerArguments.isNotEmpty()) {
|
if (currentCompilerArguments.isNotEmpty()) {
|
||||||
parseCompilerArgumentsToFacet(currentCompilerArguments, defaultCompilerArguments, kotlinFacet)
|
parseCompilerArgumentsToFacet(currentCompilerArguments, defaultCompilerArguments, kotlinFacet, modelsProvider)
|
||||||
}
|
}
|
||||||
adjustClasspath(kotlinFacet, dependencyClasspath)
|
adjustClasspath(kotlinFacet, dependencyClasspath)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,8 +21,10 @@ import com.intellij.openapi.module.Module
|
|||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.projectRoots.JavaSdk
|
import com.intellij.openapi.projectRoots.JavaSdk
|
||||||
import com.intellij.openapi.projectRoots.JavaSdkVersion
|
import com.intellij.openapi.projectRoots.JavaSdkVersion
|
||||||
|
import com.intellij.openapi.projectRoots.ProjectJdkTable
|
||||||
import com.intellij.openapi.roots.ModuleRootManager
|
import com.intellij.openapi.roots.ModuleRootManager
|
||||||
import com.intellij.openapi.roots.ModuleRootModel
|
import com.intellij.openapi.roots.ModuleRootModel
|
||||||
|
import com.intellij.openapi.util.io.FileUtil
|
||||||
import com.intellij.openapi.util.text.StringUtil
|
import com.intellij.openapi.util.text.StringUtil
|
||||||
import org.jetbrains.kotlin.cli.common.arguments.*
|
import org.jetbrains.kotlin.cli.common.arguments.*
|
||||||
import org.jetbrains.kotlin.compilerRunner.ArgumentUtils
|
import org.jetbrains.kotlin.compilerRunner.ArgumentUtils
|
||||||
@@ -184,7 +186,20 @@ private val CommonCompilerArguments.ignoredFields: List<String>
|
|||||||
else -> emptyList()
|
else -> emptyList()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun parseCompilerArgumentsToFacet(arguments: List<String>, defaultArguments: List<String>, kotlinFacet: KotlinFacet) {
|
private fun Module.configureJdkIfPossible(compilerArguments: K2JVMCompilerArguments, modelsProvider: IdeModifiableModelsProvider) {
|
||||||
|
val jdkHome = compilerArguments.jdkHome ?: return
|
||||||
|
val jdk = ProjectJdkTable.getInstance().allJdks.firstOrNull {
|
||||||
|
it.sdkType is JavaSdk && FileUtil.comparePaths(it.homePath, jdkHome) == 0
|
||||||
|
} ?: return
|
||||||
|
modelsProvider.getModifiableRootModel(this).sdk = jdk
|
||||||
|
}
|
||||||
|
|
||||||
|
fun parseCompilerArgumentsToFacet(
|
||||||
|
arguments: List<String>,
|
||||||
|
defaultArguments: List<String>,
|
||||||
|
kotlinFacet: KotlinFacet,
|
||||||
|
modelsProvider: IdeModifiableModelsProvider
|
||||||
|
) {
|
||||||
val argumentArray = arguments.toTypedArray()
|
val argumentArray = arguments.toTypedArray()
|
||||||
|
|
||||||
with(kotlinFacet.configuration.settings) {
|
with(kotlinFacet.configuration.settings) {
|
||||||
@@ -201,6 +216,10 @@ fun parseCompilerArgumentsToFacet(arguments: List<String>, defaultArguments: Lis
|
|||||||
// Retain only fields exposed (and not explicitly ignored) in facet configuration editor.
|
// Retain only fields exposed (and not explicitly ignored) in facet configuration editor.
|
||||||
// The rest is combined into string and stored in CompilerSettings.additionalArguments
|
// The rest is combined into string and stored in CompilerSettings.additionalArguments
|
||||||
|
|
||||||
|
if (compilerArguments is K2JVMCompilerArguments) {
|
||||||
|
kotlinFacet.module.configureJdkIfPossible(compilerArguments, modelsProvider)
|
||||||
|
}
|
||||||
|
|
||||||
val primaryFields = compilerArguments.primaryFields
|
val primaryFields = compilerArguments.primaryFields
|
||||||
val ignoredFields = compilerArguments.ignoredFields
|
val ignoredFields = compilerArguments.ignoredFields
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,11 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.codeInsight.gradle
|
package org.jetbrains.kotlin.idea.codeInsight.gradle
|
||||||
|
|
||||||
|
import com.intellij.openapi.application.Result
|
||||||
|
import com.intellij.openapi.application.WriteAction
|
||||||
|
import com.intellij.openapi.projectRoots.JavaSdk
|
||||||
|
import com.intellij.openapi.projectRoots.ProjectJdkTable
|
||||||
|
import com.intellij.openapi.roots.ModuleRootManager
|
||||||
import com.intellij.openapi.util.text.StringUtil
|
import com.intellij.openapi.util.text.StringUtil
|
||||||
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
|
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
|
||||||
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
|
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
|
||||||
@@ -1079,4 +1084,59 @@ class GradleFacetImportTest : GradleImportingTestCase() {
|
|||||||
Assert.assertEquals(null, (compilerArguments as K2JVMCompilerArguments).classpath)
|
Assert.assertEquals(null, (compilerArguments as K2JVMCompilerArguments).classpath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testJDKImport() {
|
||||||
|
object : WriteAction<Unit>() {
|
||||||
|
override fun run(result: Result<Unit>) {
|
||||||
|
val jdk = JavaSdk.getInstance().createJdk("myJDK", "my/path/to/jdk")
|
||||||
|
ProjectJdkTable.getInstance().addJdk(jdk)
|
||||||
|
}
|
||||||
|
}.execute()
|
||||||
|
|
||||||
|
try {
|
||||||
|
createProjectSubFile("build.gradle", """
|
||||||
|
group 'Again'
|
||||||
|
version '1.0-SNAPSHOT'
|
||||||
|
|
||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
maven {
|
||||||
|
url 'http://dl.bintray.com/kotlin/kotlin-eap-1.1'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.0")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
apply plugin: 'kotlin'
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.0"
|
||||||
|
compile "org.apache.logging.log4j:log4j-core:2.7"
|
||||||
|
}
|
||||||
|
|
||||||
|
compileKotlin {
|
||||||
|
kotlinOptions.jdkHome = "my/path/to/jdk"
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
importProject()
|
||||||
|
|
||||||
|
val moduleSDK = ModuleRootManager.getInstance(getModule("project_main")).sdk!!
|
||||||
|
Assert.assertTrue(moduleSDK.sdkType is JavaSdk)
|
||||||
|
Assert.assertEquals("myJDK", moduleSDK.name)
|
||||||
|
Assert.assertEquals("my/path/to/jdk", moduleSDK.homePath)
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
object : WriteAction<Unit>() {
|
||||||
|
override fun run(result: Result<Unit>) {
|
||||||
|
val jdkTable = ProjectJdkTable.getInstance()
|
||||||
|
jdkTable.removeJdk(jdkTable.findJdk("myJDK"))
|
||||||
|
}
|
||||||
|
}.execute()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user