diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/IClasspathDependency.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/IClasspathDependency.kt index a24344ad..527e6f13 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/IClasspathDependency.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/IClasspathDependency.kt @@ -36,4 +36,6 @@ interface IClasspathDependency { /** Used to only keep the most recent version for an artifact if no version was specified */ val shortId: String + + val excluded: ArrayList } \ No newline at end of file diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/IDependencyManager.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/IDependencyManager.kt index c2e7fdb9..a1e9f6ba 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/IDependencyManager.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/IDependencyManager.kt @@ -1,8 +1,10 @@ package com.beust.kobalt.api -import com.beust.kobalt.maven.aether.Filters +import com.beust.kobalt.maven.aether.Filters.EXCLUDE_OPTIONAL_FILTER +import com.beust.kobalt.maven.aether.KobaltMavenResolver import com.beust.kobalt.maven.aether.Scope import org.eclipse.aether.graph.DependencyFilter +import org.eclipse.aether.graph.DependencyNode /** * Manage the creation of dependencies and also provide dependencies for projects. @@ -38,7 +40,43 @@ interface IDependencyManager { * allDependencies is typically either compileDependencies or testDependencies */ fun calculateDependencies(project: Project?, context: KobaltContext, - dependencyFilter: DependencyFilter = Filters.EXCLUDE_OPTIONAL_FILTER, + dependencyFilter: DependencyFilter = createDependencyFilter(project?.compileDependencies ?: emptyList()), scopes: List = listOf(Scope.COMPILE), vararg passedDependencies: List): List + + /** + * Create an Aether dependency filter that uses the dependency configuration included in each + * IClasspathDependency. + */ + fun createDependencyFilter(dependencies: List) : DependencyFilter { + return DependencyFilter { p0, p1 -> + fun isNodeExcluded(passedDep: IClasspathDependency, node: DependencyNode) : Boolean { + val dep = create(KobaltMavenResolver.artifactToId(node.artifact)) + return passedDep.excluded.any { ex -> ex.isExcluded(dep)} + } + + val accept = dependencies.any { + // Is this dependency excluded? + val isExcluded = isNodeExcluded(it, p0) + + // Is the parent dependency excluded? + val isParentExcluded = + if (p1.any()) { + isNodeExcluded(it, p1[0]) + } else { + false + } + + // Only accept if no exclusions were found + ! isExcluded && ! isParentExcluded + } + + if (! accept) { + println(" FOUND EXCLUDED DEP: " + p0) + } + + if (accept) EXCLUDE_OPTIONAL_FILTER.accept(p0, p1) + else accept + } + } } \ No newline at end of file diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Project.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Project.kt index 5e45e7cb..60a29fed 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Project.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Project.kt @@ -145,7 +145,7 @@ class Dependencies(val project: Project, * future tasks receive a get(), the repos will be correct. */ private fun addToDependencies(project: Project, dependencies: ArrayList, - dep: Array, optional: Boolean = false): List> + dep: Array, optional: Boolean = false, excludeConfig: ExcludeConfig? = null): List> = with(dep.map { val resolved = if (KobaltMavenResolver.isRangeVersion(it)) { @@ -160,12 +160,36 @@ class Dependencies(val project: Project, DependencyManager.create(resolved, optional, project.directory) }) { dependencies.addAll(this) + if (excludeConfig != null) { + this.forEach { it.excluded.add(excludeConfig) } + } + this.map { FutureTask { it.jarFile.get() } } } @Directive fun compile(vararg dep: String) = addToDependencies(project, dependencies, dep) + class ExcludeConfig { + val ids = arrayListOf() + + @Directive + fun excludeIds(vararg passedIds: String) = ids.addAll(passedIds) + + fun isExcluded(dep: IClasspathDependency) : Boolean { + val result = ids.contains(dep.id) + return result + } + } + + @Directive + fun compile(dep: String, init: ExcludeConfig.() -> Unit) { + val excludeConfig = ExcludeConfig().apply { + init() + } + addToDependencies(project, dependencies, arrayOf(dep), excludeConfig = excludeConfig) + } + @Directive fun compileOptional(vararg dep: String) { addToDependencies(project, optionalDependencies, dep, optional = true) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt index 25294934..58f65776 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt @@ -23,8 +23,8 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager) flags: List): TaskResult { // Dependencies - val allDependencies = (info.dependencies - + dependencyManager.calculateDependencies(project, context!!, passedDependencies = info.dependencies)) + val allDependencies = (info.dependencies + dependencyManager.calculateDependencies(project, context!!, + passedDependencies = info.dependencies)) .distinct() // Plugins that add flags to the compiler diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt index c27e26bb..fa774ad3 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt @@ -88,6 +88,7 @@ open class JvmCompilerPlugin @Inject constructor( if (testContributor != null && testContributor.affinity(project, context) > 0) { // val td1 = dependencyManager.testDependencies(project, context) val testDependencies = dependencyManager.calculateDependencies(project, context, + dependencyFilter = dependencyManager.createDependencyFilter(project.testDependencies), scopes = listOf(Scope.TEST)) val compileDependencies = dependencyManager.calculateDependencies(project, context, scopes = listOf(Scope.COMPILE)) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/DependencyManager.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/DependencyManager.kt index 9ff45407..f93d92c2 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/DependencyManager.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/DependencyManager.kt @@ -178,11 +178,11 @@ class DependencyManager @Inject constructor(val executors: KobaltExecutors, dependencyFilter: DependencyFilter? = null, requiredBy: String? = null): List { val result = arrayListOf() - dependencies.forEach { - result.add(it) - if (it.isMaven) { - val resolved = resolver.resolveToIds(it.id, null, dependencyFilter) - result.addAll(resolved.map { create(it) }) + dependencies.forEach { dependency -> + result.add(dependency) + if (dependency.isMaven) { + val resolved = resolver.resolveToIds(dependency.id, null, dependencyFilter).map { create(it) } + result.addAll(resolved) } } val reordered = reorderDependencies(result) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/AetherDependency.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/AetherDependency.kt index 7ec9bfe0..5cd532fe 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/AetherDependency.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/AetherDependency.kt @@ -1,5 +1,6 @@ package com.beust.kobalt.maven.aether +import com.beust.kobalt.api.Dependencies import com.beust.kobalt.api.IClasspathDependency import com.beust.kobalt.api.Kobalt import com.beust.kobalt.maven.CompletedFuture @@ -65,6 +66,8 @@ class AetherDependency(val artifact: Artifact, override val optional: Boolean = override val shortId = artifact.groupId + ":" + artifact.artifactId + ":" + artifact.classifier + override val excluded = arrayListOf() + override fun compareTo(other: AetherDependency): Int { return Versions.toLongVersion(artifact.version).compareTo(Versions.toLongVersion( other.artifact.version)) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/dependency/FileDependency.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/dependency/FileDependency.kt index a271cf9a..047754e2 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/dependency/FileDependency.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/dependency/FileDependency.kt @@ -1,5 +1,6 @@ package com.beust.kobalt.maven.dependency +import com.beust.kobalt.api.Dependencies import com.beust.kobalt.api.IClasspathDependency import com.beust.kobalt.maven.CompletedFuture import org.apache.maven.model.Dependency @@ -31,6 +32,8 @@ open class FileDependency(open val fileName: String, override val optional: Bool override fun directDependencies() = arrayListOf() + override val excluded = arrayListOf() + override fun compareTo(other: FileDependency) = fileName.compareTo(other.fileName) override fun toString() = fileName diff --git a/src/main/kotlin/com/beust/kobalt/app/ParsedBuildFile.kt b/src/main/kotlin/com/beust/kobalt/app/ParsedBuildFile.kt index 8f7d3fe3..cf9afc94 100644 --- a/src/main/kotlin/com/beust/kobalt/app/ParsedBuildFile.kt +++ b/src/main/kotlin/com/beust/kobalt/app/ParsedBuildFile.kt @@ -8,6 +8,7 @@ import com.beust.kobalt.api.Project import com.beust.kobalt.internal.build.BuildFile import com.beust.kobalt.internal.build.VersionFile import com.beust.kobalt.maven.DependencyManager +import com.beust.kobalt.maven.aether.Filters.EXCLUDE_OPTIONAL_FILTER import com.beust.kobalt.misc.BlockExtractor import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.kobaltLog @@ -129,8 +130,8 @@ class ParsedBuildFile(val buildFile: BuildFile, val context: KobaltContext, val } } - private fun generateJarFile(context: KobaltContext, buildFile: BuildFile, buildScriptJarFile: File, - originalFile: BuildFile) { + private fun generateJarFile(context: KobaltContext, buildFile: BuildFile, + buildScriptJarFile: File, originalFile: BuildFile) { // // Compile the jar file