From c7ed7cc6e4cf2222a0f33b3d62eafd29eb9190c0 Mon Sep 17 00:00:00 2001 From: Sergey Mashkov Date: Mon, 9 Jan 2017 19:12:26 +0300 Subject: [PATCH] Maven: add source roots automatically from dependency multiplatform modules (with metadata goal) --- .../kotlin/maven/KotlinCompileMojoBase.java | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java index 61091633d89..4058f400e4c 100644 --- a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java +++ b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java @@ -24,6 +24,8 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.resolver.ArtifactResolutionRequest; import org.apache.maven.artifact.resolver.ArtifactResolutionResult; import org.apache.maven.model.Dependency; +import org.apache.maven.model.Plugin; +import org.apache.maven.model.PluginExecution; import org.apache.maven.plugin.*; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Parameter; @@ -87,20 +89,45 @@ public abstract class KotlinCompileMojoBase e public List getSourceDirs() { List sources = getSourceFilePaths(); List result = new ArrayList(sources.size()); - File baseDir = project.getBasedir(); for (String source : sources) { - File f = new File(source); - if (f.isAbsolute()) { - result.add(f); - } else { - result.add(new File(baseDir, source)); + addSourceRoots(result, source); + } + + Map projectReferences = project.getProjectReferences(); + if (projectReferences != null) { + for (Dependency dependency : project.getDependencies()) { + MavenProject sibling = projectReferences.get(dependency.getGroupId() + ":" + dependency.getArtifactId() + ":" + dependency.getVersion()); + if (sibling != null) { + Plugin plugin = sibling.getPlugin("org.jetbrains.kotlin:kotlin-maven-plugin"); + if (plugin != null) { + for (PluginExecution pluginExecution : plugin.getExecutions()) { + if (pluginExecution.getGoals() != null && pluginExecution.getGoals().contains("metadata")) { + List sourceRoots = sibling.getCompileSourceRoots(); + if (sourceRoots != null) { + for (String sourceRoot : sourceRoots) { + addSourceRoots(result, sourceRoot); + } + } + } + } + } + } } } return result; } + private void addSourceRoots(List result, String source) { + File f = new File(source); + if (f.isAbsolute()) { + result.add(f); + } else { + result.add(new File(project.getBasedir(), source)); + } + } + /** * Suppress all warnings. */