KT-20822 Maven: import multiplatform projects properly

This commit is contained in:
Sergey Mashkov
2017-10-18 15:07:45 +03:00
parent b781661279
commit fd12bd904b
2 changed files with 204 additions and 1 deletions
@@ -176,7 +176,7 @@ class KotlinMavenImporter : MavenImporter(KOTLIN_PLUGIN_GROUP_ID, KOTLIN_PLUGIN_
val mavenPlugin = mavenProject.findPlugin(KotlinMavenConfigurator.GROUP_ID, KotlinMavenConfigurator.MAVEN_PLUGIN_ID) ?: return
val compilerVersion = mavenPlugin.version ?: LanguageVersion.LATEST_STABLE.versionString
val kotlinFacet = module.getOrCreateFacet(modifiableModelsProvider, false)
val platform = detectPlatformByExecutions(mavenProject) ?: detectPlatformByLibraries(mavenProject)
val platform = detectPlatform(mavenProject)
kotlinFacet.configureFacet(compilerVersion, LanguageFeature.Coroutines.defaultState, platform, modifiableModelsProvider)
val configuredPlatform = kotlinFacet.configuration.settings.targetPlatformKind!!
@@ -190,8 +190,12 @@ class KotlinMavenImporter : MavenImporter(KOTLIN_PLUGIN_GROUP_ID, KOTLIN_PLUGIN_
parseCompilerArgumentsToFacet(executionArguments, emptyList(), kotlinFacet, modifiableModelsProvider)
}
MavenProjectImportHandler.getInstances(module.project).forEach { it(kotlinFacet, mavenProject) }
setImplementedModuleName(kotlinFacet, mavenProject, module)
}
private fun detectPlatform(mavenProject: MavenProject) = detectPlatformByExecutions(mavenProject) ?:
detectPlatformByLibraries(mavenProject)
private fun detectPlatformByExecutions(mavenProject: MavenProject): TargetPlatformKind<*>? {
return mavenProject.findPlugin(KOTLIN_PLUGIN_GROUP_ID, KOTLIN_PLUGIN_ARTIFACT_ID)?.executions?.flatMap { it.goals }?.mapNotNull { goal ->
when (goal) {
@@ -254,6 +258,18 @@ class KotlinMavenImporter : MavenImporter(KOTLIN_PLUGIN_GROUP_ID, KOTLIN_PLUGIN_
plugin.configurationElement.sourceDirectories().map { SourceType.PROD to it } +
plugin.executions.flatMap { execution -> execution.configurationElement.sourceDirectories().map { execution.sourceType() to it } }
}.distinct()
private fun setImplementedModuleName(kotlinFacet: KotlinFacet, mavenProject: MavenProject, module: Module) {
if (kotlinFacet.configuration.settings.targetPlatformKind == TargetPlatformKind.Common) {
kotlinFacet.configuration.settings.implementedModuleName = null
} else {
val manager = MavenProjectsManager.getInstance(module.project)
val mavenDependencies = mavenProject.dependencies.mapNotNull { manager?.findProject(it) }
val implemented = mavenDependencies.singleOrNull { detectPlatformByExecutions(it) == TargetPlatformKind.Common }
kotlinFacet.configuration.settings.implementedModuleName = implemented?.let { manager.findModule(it)?.name ?: it.displayName }
}
}
}
private fun MavenPlugin.isKotlinPlugin() = groupId == KotlinMavenImporter.KOTLIN_PLUGIN_GROUP_ID && artifactId == KotlinMavenImporter.KOTLIN_PLUGIN_ARTIFACT_ID
@@ -1726,6 +1726,193 @@ class KotlinMavenImporterTest : MavenImportingTestCase() {
}
}
fun testMultiModuleImport() {
createProjectSubDirs("src/main/kotlin", "my-common-module/src/main/kotlin", "my-jvm-module/src/main/kotlin", "my-js-module/src/main/kotlin")
val mainPom = createProjectPom("""
<groupId>test</groupId>
<artifactId>project</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>my-common-module</module>
<module>my-jvm-module</module>
<module>my-js-module</module>
</modules>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>$kotlinVersion</version>
</plugin>
</plugins>
</build>
""")
val commonModule = createModulePom(
"my-common-module",
"""
<parent>
<groupId>test</groupId>
<artifactId>project</artifactId>
<version>1.0.0</version>
</parent>
<groupId>test</groupId>
<artifactId>my-common-module</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-common</artifactId>
<version>$kotlinVersion</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<executions>
<execution>
<id>meta</id>
<phase>compile</phase>
<goals>
<goal>metadata</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
"""
)
val jvmModule = createModulePom(
"my-jvm-module",
"""
<parent>
<groupId>test</groupId>
<artifactId>project</artifactId>
<version>1.0.0</version>
</parent>
<groupId>test</groupId>
<artifactId>my-jvm-module</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>$kotlinVersion</version>
</dependency>
<dependency>
<groupId>test</groupId>
<artifactId>my-common-module</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<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>
</plugin>
</plugins>
</build>
"""
)
val jsModule = createModulePom(
"my-js-module",
"""
<parent>
<groupId>test</groupId>
<artifactId>project</artifactId>
<version>1.0.0</version>
</parent>
<groupId>test</groupId>
<artifactId>my-js-module</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-js</artifactId>
<version>$kotlinVersion</version>
</dependency>
<dependency>
<groupId>test</groupId>
<artifactId>my-common-module</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<executions>
<execution>
<id>js</id>
<phase>compile</phase>
<goals>
<goal>js</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
"""
)
importProjects(mainPom, commonModule, jvmModule, jsModule)
assertModules("project", "my-common-module", "my-jvm-module", "my-js-module")
assertImporterStatePresent()
with (facetSettings("my-common-module")) {
Assert.assertEquals(TargetPlatformKind.Common.description, targetPlatformKind!!.description)
}
with (facetSettings("my-jvm-module")) {
Assert.assertEquals(TargetPlatformKind.Jvm(JvmTarget.JVM_1_6).description, targetPlatformKind!!.description)
Assert.assertEquals("my-common-module", implementedModuleName)
}
with (facetSettings("my-js-module")) {
Assert.assertEquals(TargetPlatformKind.JavaScript.description, targetPlatformKind!!.description)
Assert.assertEquals("my-common-module", implementedModuleName)
}
}
fun testJDKImport() {
object : WriteAction<Unit>() {
override fun run(result: Result<Unit>) {