Skip to content

Commit 90c2b14

Browse files
committed
Kotlin Facet: Import Maven option specified in <jvmTarget> element
#KT-16329 In Progress
1 parent b9b4cce commit 90c2b14

File tree

3 files changed

+121
-6
lines changed

3 files changed

+121
-6
lines changed

idea/idea-maven/idea-maven.iml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</content>
1010
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
1111
<orderEntry type="sourceFolder" forTests="false" />
12-
<orderEntry type="library" name="idea-full" level="project" />
12+
<orderEntry type="library" name="cli-parser" level="project" />
1313
<orderEntry type="module" module-name="idea" />
1414
<orderEntry type="library" scope="PROVIDED" name="maven" level="project" />
1515
<orderEntry type="module" module-name="frontend" />
@@ -22,5 +22,7 @@
2222
<orderEntry type="module" module-name="tests-common" scope="TEST" />
2323
<orderEntry type="module" module-name="idea-jps-common" />
2424
<orderEntry type="module" module-name="util" />
25+
<orderEntry type="module" module-name="cli-common" />
26+
<orderEntry type="library" name="idea-full" level="project" />
2527
</component>
26-
</module>
28+
</module>

idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/KotlinMavenImporter.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,22 @@ class KotlinMavenImporter : MavenImporter(KOTLIN_PLUGIN_GROUP_ID, KOTLIN_PLUGIN_
112112
PomFile.KotlinGoals.MetaData)
113113

114114
private fun configureFacet(mavenProject: MavenProject, modifiableModelsProvider: IdeModifiableModelsProvider, module: Module) {
115-
val mavenPlugin = mavenProject.findPlugin(KotlinMavenConfigurator.GROUP_ID, KotlinMavenConfigurator.MAVEN_PLUGIN_ID)
116-
val compilerVersion = mavenPlugin?.version ?: return
115+
val mavenPlugin = mavenProject.findPlugin(KotlinMavenConfigurator.GROUP_ID, KotlinMavenConfigurator.MAVEN_PLUGIN_ID) ?: return
116+
val compilerVersion = mavenPlugin.version ?: LanguageVersion.LATEST.versionString
117117
val kotlinFacet = module.getOrCreateFacet(modifiableModelsProvider, false)
118118
val platform = detectPlatformByExecutions(mavenProject) ?: detectPlatformByLibraries(mavenProject)
119119

120120
kotlinFacet.configureFacet(compilerVersion, CoroutineSupport.DEFAULT, platform, modifiableModelsProvider)
121-
val apiVersion = mavenPlugin.configurationElement?.getChild("apiVersion")?.text?.let { LanguageVersion.fromFullVersionString(it) }
122-
val sharedArguments = mavenPlugin.configurationElement?.let { getCompilerArgumentsByConfigurationElement(it) } ?: emptyList()
121+
val configuration = mavenPlugin.configurationElement
122+
val apiVersion = configuration?.getChild("apiVersion")?.text?.let { LanguageVersion.fromFullVersionString(it) }
123+
val sharedArguments = configuration?.let { getCompilerArgumentsByConfigurationElement(it) } ?: emptyList()
123124
val executionArguments = mavenPlugin.executions?.filter { it.goals.any { it in compilationGoals } }
124125
?.firstOrNull()
125126
?.configurationElement?.let { getCompilerArgumentsByConfigurationElement(it) }
126127
?: emptyList()
127128
with(kotlinFacet.configuration.settings) {
128129
versionInfo.apiLevel = apiVersion
130+
compilerInfo.k2jvmCompilerArguments?.jvmTarget = configuration?.getChild("jvmTarget")?.text
129131
}
130132
parseCompilerArgumentsToFacet(sharedArguments, emptyList(), kotlinFacet)
131133
parseCompilerArgumentsToFacet(executionArguments, emptyList(), kotlinFacet)

idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenImporterTest.kt

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.jetbrains.kotlin.idea.maven
1818

19+
import org.jetbrains.kotlin.config.KotlinFacetSettings
20+
import org.jetbrains.kotlin.idea.facet.KotlinFacet
21+
import org.junit.Assert
1922
import java.io.File
2023

2124
class KotlinMavenImporterTest : MavenImportingTestCase() {
@@ -384,7 +387,115 @@ class KotlinMavenImporterTest : MavenImportingTestCase() {
384387
assertTestSources("project", "src/test/java", "src/test/kotlin", "src/test/kotlin.jvm")
385388
}
386389

390+
fun testJvmTarget() {
391+
createProjectSubDirs("src/main/kotlin", "src/main/kotlin.jvm", "src/test/kotlin", "src/test/kotlin.jvm")
392+
393+
importProject("""
394+
<groupId>test</groupId>
395+
<artifactId>project</artifactId>
396+
<version>1.0.0</version>
397+
398+
<dependencies>
399+
<dependency>
400+
<groupId>org.jetbrains.kotlin</groupId>
401+
<artifactId>kotlin-stdlib</artifactId>
402+
<version>$kotlinVersion</version>
403+
</dependency>
404+
</dependencies>
405+
406+
<build>
407+
<sourceDirectory>src/main/kotlin</sourceDirectory>
408+
409+
<plugins>
410+
<plugin>
411+
<groupId>org.jetbrains.kotlin</groupId>
412+
<artifactId>kotlin-maven-plugin</artifactId>
413+
414+
<executions>
415+
<execution>
416+
<id>compile</id>
417+
<phase>compile</phase>
418+
<goals>
419+
<goal>compile</goal>
420+
</goals>
421+
</execution>
422+
</executions>
423+
<configuration>
424+
<jvmTarget>1.8</jvmTarget>
425+
</configuration>
426+
</plugin>
427+
</plugins>
428+
</build>
429+
""")
430+
431+
assertModules("project")
432+
assertImporterStatePresent()
433+
434+
with (facetSettings) {
435+
Assert.assertEquals("JVM 1.8", versionInfo.targetPlatformKind!!.description)
436+
Assert.assertEquals("1.8", compilerInfo.k2jvmCompilerArguments!!.jvmTarget)
437+
}
438+
}
439+
440+
fun testArgsInFacet() {
441+
createProjectSubDirs("src/main/kotlin", "src/main/kotlin.jvm", "src/test/kotlin", "src/test/kotlin.jvm")
442+
443+
importProject("""
444+
<groupId>test</groupId>
445+
<artifactId>project</artifactId>
446+
<version>1.0.0</version>
447+
448+
<dependencies>
449+
<dependency>
450+
<groupId>org.jetbrains.kotlin</groupId>
451+
<artifactId>kotlin-stdlib</artifactId>
452+
<version>$kotlinVersion</version>
453+
</dependency>
454+
</dependencies>
455+
456+
<build>
457+
<sourceDirectory>src/main/kotlin</sourceDirectory>
458+
459+
<plugins>
460+
<plugin>
461+
<groupId>org.jetbrains.kotlin</groupId>
462+
<artifactId>kotlin-maven-plugin</artifactId>
463+
464+
<executions>
465+
<execution>
466+
<id>compile</id>
467+
<phase>compile</phase>
468+
<goals>
469+
<goal>compile</goal>
470+
</goals>
471+
</execution>
472+
</executions>
473+
<configuration>
474+
<args>
475+
<arg>-jvm-target</arg>
476+
<arg>1.8</arg>
477+
<arg>-Xcoroutines=enable</arg>
478+
</args>
479+
</configuration>
480+
</plugin>
481+
</plugins>
482+
</build>
483+
""")
484+
485+
assertModules("project")
486+
assertImporterStatePresent()
487+
488+
with (facetSettings) {
489+
Assert.assertEquals("JVM 1.8", versionInfo.targetPlatformKind!!.description)
490+
Assert.assertEquals("1.8", compilerInfo.k2jvmCompilerArguments!!.jvmTarget)
491+
Assert.assertEquals("enable", compilerInfo.coroutineSupport.compilerArgument)
492+
}
493+
}
494+
387495
private fun assertImporterStatePresent() {
388496
assertNotNull("Kotlin importer component is not present", myTestFixture.module.getComponent(KotlinImporterComponent::class.java))
389497
}
498+
499+
private val facetSettings: KotlinFacetSettings
500+
get() = KotlinFacet.get(getModule("project"))!!.configuration.settings
390501
}

0 commit comments

Comments
 (0)