From 9050a6bb2748ecfba4a1026d3ead71cfb9af93ac Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Sat, 14 Nov 2015 18:50:23 -0800 Subject: [PATCH] Make "run" use the transitive closure of dependencies. Necessary if fatJar=false. --- .../com/beust/kobalt/internal/JvmCompiler.kt | 28 ++---------------- .../kobalt/internal/JvmCompilerPlugin.kt | 2 +- .../beust/kobalt/kotlin/BuildFileCompiler.kt | 6 ++-- .../beust/kobalt/maven/DependencyManager.kt | 27 +++++++++++++++++ .../plugin/application/ApplicationPlugin.kt | 29 +++++++++++++++++-- .../kobalt/plugin/kotlin/KotlinPlugin.kt | 4 +-- .../plugin/packaging/PackagingPlugin.kt | 11 +++---- 7 files changed, 68 insertions(+), 39 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt b/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt index ecb075bd..d6a34760 100644 --- a/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt @@ -2,7 +2,6 @@ package com.beust.kobalt.internal import com.beust.kobalt.KobaltException import com.beust.kobalt.TaskResult -import com.beust.kobalt.api.IClasspathContributor import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.Project import com.beust.kobalt.maven.DependencyManager @@ -25,7 +24,8 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager) : TaskResult { // Dependencies - val allDependencies = info.dependencies + calculateDependencies(project, context!!, info.dependencies) + val allDependencies = info.dependencies + + dependencyManager.calculateDependencies(project, context!!, info.dependencies) // Plugins that add flags to the compiler val addedFlags = ArrayList(info.compilerArgs) + @@ -48,30 +48,6 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager) } } } - - /** - * @return the classpath for this project, including the IClasspathContributors. - */ - fun calculateDependencies(project: Project?, context: KobaltContext, - vararg allDependencies: List): List { - var result = arrayListOf() - allDependencies.forEach { dependencies -> - result.addAll(dependencyManager.transitiveClosure(dependencies)) - } - result.addAll(runClasspathContributors(project, context)) - - return result - } - - private fun runClasspathContributors(project: Project?, context: KobaltContext) : - Collection { - val result = hashSetOf() - context.pluginInfo.classpathContributors.forEach { it: IClasspathContributor -> - result.addAll(it.entriesFor(project)) - } - return result - } - } data class CompilerActionInfo(val directory: String?, val dependencies: List, diff --git a/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt b/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt index cf63eb21..40accc71 100644 --- a/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt @@ -59,7 +59,7 @@ abstract class JvmCompilerPlugin @Inject constructor( with(project) { arrayListOf(compileDependencies, compileProvidedDependencies, testDependencies, testProvidedDependencies).forEach { - result.addAll(jvmCompiler.calculateDependencies(project, context, it)) + result.addAll(dependencyManager.calculateDependencies(project, context, it)) } } return dependencyManager.reorderDependencies(result) diff --git a/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt b/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt index 84b6239e..a00f3a84 100644 --- a/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt @@ -5,8 +5,8 @@ import com.beust.kobalt.KobaltException import com.beust.kobalt.Plugins import com.beust.kobalt.api.* import com.beust.kobalt.api.annotation.Task -import com.beust.kobalt.internal.JvmCompiler import com.beust.kobalt.internal.PluginInfo +import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.maven.IClasspathDependency import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.Topological @@ -34,7 +34,7 @@ import javax.inject.Inject */ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val buildFiles: List, @Assisted val pluginInfo: PluginInfo, val files: KFiles, val plugins: Plugins, - val jvmCompiler: JvmCompiler, val pluginProperties: PluginProperties) { + val dependencyManager: DependencyManager, val pluginProperties: PluginProperties) { interface IFactory { fun create(@Assisted("buildFiles") buildFiles: List, pluginInfo: PluginInfo) : BuildFileCompiler @@ -183,7 +183,7 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b } private fun generateJarFile(context: KobaltContext, buildFile: BuildFile, buildScriptJarFile: File) { - val kotlintDeps = jvmCompiler.calculateDependencies(null, context, listOf()) + val kotlintDeps = dependencyManager.calculateDependencies(null, context, listOf()) val deps: List = kotlintDeps.map { it.jarFile.get().absolutePath } kotlinCompilePrivate { classpath(files.kobaltJar) diff --git a/src/main/kotlin/com/beust/kobalt/maven/DependencyManager.kt b/src/main/kotlin/com/beust/kobalt/maven/DependencyManager.kt index 2993c8d5..d45dffe7 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/DependencyManager.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/DependencyManager.kt @@ -1,5 +1,8 @@ package com.beust.kobalt.maven +import com.beust.kobalt.api.IClasspathContributor +import com.beust.kobalt.api.KobaltContext +import com.beust.kobalt.api.Project import com.beust.kobalt.misc.KobaltExecutors import com.google.common.collect.ArrayListMultimap import java.util.* @@ -9,6 +12,30 @@ import javax.inject.Singleton @Singleton public class DependencyManager @Inject constructor(val executors: KobaltExecutors, val depFactory: DepFactory){ + + /** + * @return the classpath for this project, including the IClasspathContributors. + */ + fun calculateDependencies(project: Project?, context: KobaltContext, + vararg allDependencies: List): List { + var result = arrayListOf() + allDependencies.forEach { dependencies -> + result.addAll(transitiveClosure(dependencies)) + } + result.addAll(runClasspathContributors(project, context)) + + return result + } + + private fun runClasspathContributors(project: Project?, context: KobaltContext) : + Collection { + val result = hashSetOf() + context.pluginInfo.classpathContributors.forEach { it: IClasspathContributor -> + result.addAll(it.entriesFor(project)) + } + return result + } + fun transitiveClosure(dependencies : List): List { var executor = executors.newExecutor("JvmCompiler}", 10) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt index ceccfae3..746f6376 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt @@ -5,8 +5,10 @@ import com.beust.kobalt.api.BasePlugin import com.beust.kobalt.api.Project import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Task +import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.RunCommand +import com.beust.kobalt.plugin.packaging.PackageConfig import com.beust.kobalt.plugin.packaging.PackagingPlugin import com.google.inject.Inject import com.google.inject.Singleton @@ -29,7 +31,8 @@ fun Project.application(init: ApplicationConfig.() -> Unit) { } @Singleton -class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors) : BasePlugin() { +class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors, + val dependencyManager: DependencyManager) : BasePlugin() { companion object { const val NAME = "application" @@ -49,7 +52,18 @@ class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors) : Ba val java = JavaInfo.create(File(SystemProperties.javaBase)).javaExecutable!! if (config.mainClass != null) { val jarName = context.pluginProperties.get("packaging", PackagingPlugin.JAR_NAME) as String - val args = listOf("-classpath", jarName) + config.jvmArgs + config.mainClass!! + val packages = context.pluginProperties.get("packaging", PackagingPlugin.PACKAGES) + as List + val allDeps = arrayListOf(jarName) + if (! isFatJar(packages, jarName)) { + // If the jar file is not fat, we need to add the transitive closure of all dependencies + // on the classpath + allDeps.addAll( + dependencyManager.calculateDependencies(project, context, project.compileDependencies) + .map { it.jarFile.get().path }) + } + val allDepsJoined = allDeps.joinToString(File.pathSeparator) + val args = listOf("-classpath", allDepsJoined) + config.jvmArgs + config.mainClass!! RunCommand(java.absolutePath).run(args, successCallback = { output: List -> println(output.joinToString("\n")) }) @@ -59,5 +73,16 @@ class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors) : Ba } return TaskResult() } + + private fun isFatJar(packages: List, jarName: String): Boolean { + packages.forEach { pc -> + pc.jars.forEach { jar -> + if ((jar.name == null || jar.name == jarName) && jar.fatJar) { + return true + } + } + } + return false + } } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt index bd4be44b..f3da2a03 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt @@ -1,11 +1,11 @@ package com.beust.kobalt.plugin.kotlin +import com.beust.kobalt.TaskResult import com.beust.kobalt.api.* import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.internal.JvmCompiler import com.beust.kobalt.internal.JvmCompilerPlugin -import com.beust.kobalt.TaskResult import com.beust.kobalt.maven.* import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KobaltExecutors @@ -36,7 +36,7 @@ class KotlinPlugin @Inject constructor( @Task(name = TASK_COMPILE, description = "Compile the project") fun taskCompile(project: Project): TaskResult { copyResources(project, JvmCompilerPlugin.SOURCE_SET_MAIN) - val classpath = jvmCompiler.calculateDependencies(project, context, project.compileDependencies, + val classpath = dependencyManager.calculateDependencies(project, context, project.compileDependencies, project.compileProvidedDependencies) val projectDirectory = java.io.File(project.directory) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt index 59138eb5..e1e76086 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt @@ -45,6 +45,9 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana @ExportedProperty const val JAR_NAME = "jarName" + @ExportedProperty + const val PACKAGES = "packages" + const val TASK_ASSEMBLE: String = "assemble" const val TASK_INSTALL: String = "install" } @@ -62,6 +65,7 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana @Task(name = TASK_ASSEMBLE, description = "Package the artifacts", runAfter = arrayOf(JavaPlugin.TASK_COMPILE)) fun taskAssemble(project: Project) : TaskResult { + context.pluginProperties.put(PLUGIN_NAME, PACKAGES, packages) packages.filter { it.project.name == project.name }.forEach { pkg -> pkg.jars.forEach { generateJar(pkg.project, it) } pkg.wars.forEach { generateWar(pkg.project, it) } @@ -277,11 +281,8 @@ fun Project.install(init: InstallConfig.() -> Unit) { class InstallConfig(var libDir : String = "libs") -@Directive -fun Project.assemble(init: PackageConfig.(p: Project) -> Unit): PackageConfig { - val pd = PackageConfig(this) - pd.init(this) - return pd +fun Project.assemble(init: PackageConfig.(p: Project) -> Unit) = let { + PackageConfig(this).apply { init(it) } } class PackageConfig(val project: Project) : AttributeHolder {