1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27:12 -07:00

Make "run" use the transitive closure of dependencies.

Necessary if fatJar=false.
This commit is contained in:
Cedric Beust 2015-11-14 18:50:23 -08:00
parent 91194b2b2a
commit 9050a6bb27
7 changed files with 68 additions and 39 deletions

View file

@ -2,7 +2,6 @@ package com.beust.kobalt.internal
import com.beust.kobalt.KobaltException import com.beust.kobalt.KobaltException
import com.beust.kobalt.TaskResult import com.beust.kobalt.TaskResult
import com.beust.kobalt.api.IClasspathContributor
import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project import com.beust.kobalt.api.Project
import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.maven.DependencyManager
@ -25,7 +24,8 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager)
: TaskResult { : TaskResult {
// Dependencies // 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 // Plugins that add flags to the compiler
val addedFlags = ArrayList(info.compilerArgs) + 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<IClasspathDependency>): List<IClasspathDependency> {
var result = arrayListOf<IClasspathDependency>()
allDependencies.forEach { dependencies ->
result.addAll(dependencyManager.transitiveClosure(dependencies))
}
result.addAll(runClasspathContributors(project, context))
return result
}
private fun runClasspathContributors(project: Project?, context: KobaltContext) :
Collection<IClasspathDependency> {
val result = hashSetOf<IClasspathDependency>()
context.pluginInfo.classpathContributors.forEach { it: IClasspathContributor ->
result.addAll(it.entriesFor(project))
}
return result
}
} }
data class CompilerActionInfo(val directory: String?, val dependencies: List<IClasspathDependency>, data class CompilerActionInfo(val directory: String?, val dependencies: List<IClasspathDependency>,

View file

@ -59,7 +59,7 @@ abstract class JvmCompilerPlugin @Inject constructor(
with(project) { with(project) {
arrayListOf(compileDependencies, compileProvidedDependencies, testDependencies, arrayListOf(compileDependencies, compileProvidedDependencies, testDependencies,
testProvidedDependencies).forEach { testProvidedDependencies).forEach {
result.addAll(jvmCompiler.calculateDependencies(project, context, it)) result.addAll(dependencyManager.calculateDependencies(project, context, it))
} }
} }
return dependencyManager.reorderDependencies(result) return dependencyManager.reorderDependencies(result)

View file

@ -5,8 +5,8 @@ import com.beust.kobalt.KobaltException
import com.beust.kobalt.Plugins import com.beust.kobalt.Plugins
import com.beust.kobalt.api.* import com.beust.kobalt.api.*
import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.api.annotation.Task
import com.beust.kobalt.internal.JvmCompiler
import com.beust.kobalt.internal.PluginInfo import com.beust.kobalt.internal.PluginInfo
import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.IClasspathDependency import com.beust.kobalt.maven.IClasspathDependency
import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.Topological import com.beust.kobalt.misc.Topological
@ -34,7 +34,7 @@ import javax.inject.Inject
*/ */
public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val buildFiles: List<BuildFile>, public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val buildFiles: List<BuildFile>,
@Assisted val pluginInfo: PluginInfo, val files: KFiles, val plugins: Plugins, @Assisted val pluginInfo: PluginInfo, val files: KFiles, val plugins: Plugins,
val jvmCompiler: JvmCompiler, val pluginProperties: PluginProperties) { val dependencyManager: DependencyManager, val pluginProperties: PluginProperties) {
interface IFactory { interface IFactory {
fun create(@Assisted("buildFiles") buildFiles: List<BuildFile>, pluginInfo: PluginInfo) : BuildFileCompiler fun create(@Assisted("buildFiles") buildFiles: List<BuildFile>, 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) { private fun generateJarFile(context: KobaltContext, buildFile: BuildFile, buildScriptJarFile: File) {
val kotlintDeps = jvmCompiler.calculateDependencies(null, context, listOf<IClasspathDependency>()) val kotlintDeps = dependencyManager.calculateDependencies(null, context, listOf<IClasspathDependency>())
val deps: List<String> = kotlintDeps.map { it.jarFile.get().absolutePath } val deps: List<String> = kotlintDeps.map { it.jarFile.get().absolutePath }
kotlinCompilePrivate { kotlinCompilePrivate {
classpath(files.kobaltJar) classpath(files.kobaltJar)

View file

@ -1,5 +1,8 @@
package com.beust.kobalt.maven 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.beust.kobalt.misc.KobaltExecutors
import com.google.common.collect.ArrayListMultimap import com.google.common.collect.ArrayListMultimap
import java.util.* import java.util.*
@ -9,6 +12,30 @@ import javax.inject.Singleton
@Singleton @Singleton
public class DependencyManager @Inject constructor(val executors: KobaltExecutors, public class DependencyManager @Inject constructor(val executors: KobaltExecutors,
val depFactory: DepFactory){ val depFactory: DepFactory){
/**
* @return the classpath for this project, including the IClasspathContributors.
*/
fun calculateDependencies(project: Project?, context: KobaltContext,
vararg allDependencies: List<IClasspathDependency>): List<IClasspathDependency> {
var result = arrayListOf<IClasspathDependency>()
allDependencies.forEach { dependencies ->
result.addAll(transitiveClosure(dependencies))
}
result.addAll(runClasspathContributors(project, context))
return result
}
private fun runClasspathContributors(project: Project?, context: KobaltContext) :
Collection<IClasspathDependency> {
val result = hashSetOf<IClasspathDependency>()
context.pluginInfo.classpathContributors.forEach { it: IClasspathContributor ->
result.addAll(it.entriesFor(project))
}
return result
}
fun transitiveClosure(dependencies : List<IClasspathDependency>): List<IClasspathDependency> { fun transitiveClosure(dependencies : List<IClasspathDependency>): List<IClasspathDependency> {
var executor = executors.newExecutor("JvmCompiler}", 10) var executor = executors.newExecutor("JvmCompiler}", 10)

View file

@ -5,8 +5,10 @@ import com.beust.kobalt.api.BasePlugin
import com.beust.kobalt.api.Project import com.beust.kobalt.api.Project
import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.api.annotation.Task
import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.RunCommand import com.beust.kobalt.misc.RunCommand
import com.beust.kobalt.plugin.packaging.PackageConfig
import com.beust.kobalt.plugin.packaging.PackagingPlugin import com.beust.kobalt.plugin.packaging.PackagingPlugin
import com.google.inject.Inject import com.google.inject.Inject
import com.google.inject.Singleton import com.google.inject.Singleton
@ -29,7 +31,8 @@ fun Project.application(init: ApplicationConfig.() -> Unit) {
} }
@Singleton @Singleton
class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors) : BasePlugin() { class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors,
val dependencyManager: DependencyManager) : BasePlugin() {
companion object { companion object {
const val NAME = "application" const val NAME = "application"
@ -49,7 +52,18 @@ class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors) : Ba
val java = JavaInfo.create(File(SystemProperties.javaBase)).javaExecutable!! val java = JavaInfo.create(File(SystemProperties.javaBase)).javaExecutable!!
if (config.mainClass != null) { if (config.mainClass != null) {
val jarName = context.pluginProperties.get("packaging", PackagingPlugin.JAR_NAME) as String 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<PackageConfig>
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<String> -> RunCommand(java.absolutePath).run(args, successCallback = { output: List<String> ->
println(output.joinToString("\n")) println(output.joinToString("\n"))
}) })
@ -59,5 +73,16 @@ class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors) : Ba
} }
return TaskResult() return TaskResult()
} }
private fun isFatJar(packages: List<PackageConfig>, jarName: String): Boolean {
packages.forEach { pc ->
pc.jars.forEach { jar ->
if ((jar.name == null || jar.name == jarName) && jar.fatJar) {
return true
}
}
}
return false
}
} }

View file

@ -1,11 +1,11 @@
package com.beust.kobalt.plugin.kotlin package com.beust.kobalt.plugin.kotlin
import com.beust.kobalt.TaskResult
import com.beust.kobalt.api.* import com.beust.kobalt.api.*
import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.api.annotation.Task
import com.beust.kobalt.internal.JvmCompiler import com.beust.kobalt.internal.JvmCompiler
import com.beust.kobalt.internal.JvmCompilerPlugin import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.TaskResult
import com.beust.kobalt.maven.* import com.beust.kobalt.maven.*
import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.KobaltExecutors
@ -36,7 +36,7 @@ class KotlinPlugin @Inject constructor(
@Task(name = TASK_COMPILE, description = "Compile the project") @Task(name = TASK_COMPILE, description = "Compile the project")
fun taskCompile(project: Project): TaskResult { fun taskCompile(project: Project): TaskResult {
copyResources(project, JvmCompilerPlugin.SOURCE_SET_MAIN) copyResources(project, JvmCompilerPlugin.SOURCE_SET_MAIN)
val classpath = jvmCompiler.calculateDependencies(project, context, project.compileDependencies, val classpath = dependencyManager.calculateDependencies(project, context, project.compileDependencies,
project.compileProvidedDependencies) project.compileProvidedDependencies)
val projectDirectory = java.io.File(project.directory) val projectDirectory = java.io.File(project.directory)

View file

@ -45,6 +45,9 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
@ExportedProperty @ExportedProperty
const val JAR_NAME = "jarName" const val JAR_NAME = "jarName"
@ExportedProperty
const val PACKAGES = "packages"
const val TASK_ASSEMBLE: String = "assemble" const val TASK_ASSEMBLE: String = "assemble"
const val TASK_INSTALL: String = "install" 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)) @Task(name = TASK_ASSEMBLE, description = "Package the artifacts", runAfter = arrayOf(JavaPlugin.TASK_COMPILE))
fun taskAssemble(project: Project) : TaskResult { fun taskAssemble(project: Project) : TaskResult {
context.pluginProperties.put(PLUGIN_NAME, PACKAGES, packages)
packages.filter { it.project.name == project.name }.forEach { pkg -> packages.filter { it.project.name == project.name }.forEach { pkg ->
pkg.jars.forEach { generateJar(pkg.project, it) } pkg.jars.forEach { generateJar(pkg.project, it) }
pkg.wars.forEach { generateWar(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") class InstallConfig(var libDir : String = "libs")
@Directive fun Project.assemble(init: PackageConfig.(p: Project) -> Unit) = let {
fun Project.assemble(init: PackageConfig.(p: Project) -> Unit): PackageConfig { PackageConfig(this).apply { init(it) }
val pd = PackageConfig(this)
pd.init(this)
return pd
} }
class PackageConfig(val project: Project) : AttributeHolder { class PackageConfig(val project: Project) : AttributeHolder {