mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 00:17:11 -07:00
Better dependency exclusion.
This commit is contained in:
parent
4911677bc1
commit
a9bba0d83b
9 changed files with 84 additions and 12 deletions
|
@ -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<Dependencies.ExcludeConfig>
|
||||
}
|
|
@ -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<Scope> = listOf(Scope.COMPILE),
|
||||
vararg passedDependencies: List<IClasspathDependency>): List<IClasspathDependency>
|
||||
|
||||
/**
|
||||
* Create an Aether dependency filter that uses the dependency configuration included in each
|
||||
* IClasspathDependency.
|
||||
*/
|
||||
fun createDependencyFilter(dependencies: List<IClasspathDependency>) : 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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<IClasspathDependency>,
|
||||
dep: Array<out String>, optional: Boolean = false): List<Future<File>>
|
||||
dep: Array<out String>, optional: Boolean = false, excludeConfig: ExcludeConfig? = null): List<Future<File>>
|
||||
= 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<String>()
|
||||
|
||||
@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)
|
||||
|
|
|
@ -23,8 +23,8 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager)
|
|||
flags: List<String>): 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
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -178,11 +178,11 @@ class DependencyManager @Inject constructor(val executors: KobaltExecutors,
|
|||
dependencyFilter: DependencyFilter? = null,
|
||||
requiredBy: String? = null): List<IClasspathDependency> {
|
||||
val result = arrayListOf<IClasspathDependency>()
|
||||
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)
|
||||
|
|
|
@ -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<Dependencies.ExcludeConfig>()
|
||||
|
||||
override fun compareTo(other: AetherDependency): Int {
|
||||
return Versions.toLongVersion(artifact.version).compareTo(Versions.toLongVersion(
|
||||
other.artifact.version))
|
||||
|
|
|
@ -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<IClasspathDependency>()
|
||||
|
||||
override val excluded = arrayListOf<Dependencies.ExcludeConfig>()
|
||||
|
||||
override fun compareTo(other: FileDependency) = fileName.compareTo(other.fileName)
|
||||
|
||||
override fun toString() = fileName
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue