1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 00:17:11 -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.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<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>,

View file

@ -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)

View file

@ -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<BuildFile>,
@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<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) {
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 }
kotlinCompilePrivate {
classpath(files.kobaltJar)

View file

@ -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<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> {
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.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<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> ->
println(output.joinToString("\n"))
})
@ -59,5 +73,16 @@ class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors) : Ba
}
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
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)

View file

@ -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 {