mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 16:28:12 -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 */
|
/** Used to only keep the most recent version for an artifact if no version was specified */
|
||||||
val shortId: String
|
val shortId: String
|
||||||
|
|
||||||
|
val excluded: ArrayList<Dependencies.ExcludeConfig>
|
||||||
}
|
}
|
|
@ -1,8 +1,10 @@
|
||||||
package com.beust.kobalt.api
|
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 com.beust.kobalt.maven.aether.Scope
|
||||||
import org.eclipse.aether.graph.DependencyFilter
|
import org.eclipse.aether.graph.DependencyFilter
|
||||||
|
import org.eclipse.aether.graph.DependencyNode
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manage the creation of dependencies and also provide dependencies for projects.
|
* Manage the creation of dependencies and also provide dependencies for projects.
|
||||||
|
@ -38,7 +40,43 @@ interface IDependencyManager {
|
||||||
* allDependencies is typically either compileDependencies or testDependencies
|
* allDependencies is typically either compileDependencies or testDependencies
|
||||||
*/
|
*/
|
||||||
fun calculateDependencies(project: Project?, context: KobaltContext,
|
fun calculateDependencies(project: Project?, context: KobaltContext,
|
||||||
dependencyFilter: DependencyFilter = Filters.EXCLUDE_OPTIONAL_FILTER,
|
dependencyFilter: DependencyFilter = createDependencyFilter(project?.compileDependencies ?: emptyList()),
|
||||||
scopes: List<Scope> = listOf(Scope.COMPILE),
|
scopes: List<Scope> = listOf(Scope.COMPILE),
|
||||||
vararg passedDependencies: List<IClasspathDependency>): List<IClasspathDependency>
|
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.
|
* future tasks receive a get(), the repos will be correct.
|
||||||
*/
|
*/
|
||||||
private fun addToDependencies(project: Project, dependencies: ArrayList<IClasspathDependency>,
|
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 {
|
= with(dep.map {
|
||||||
val resolved =
|
val resolved =
|
||||||
if (KobaltMavenResolver.isRangeVersion(it)) {
|
if (KobaltMavenResolver.isRangeVersion(it)) {
|
||||||
|
@ -160,12 +160,36 @@ class Dependencies(val project: Project,
|
||||||
DependencyManager.create(resolved, optional, project.directory)
|
DependencyManager.create(resolved, optional, project.directory)
|
||||||
}) {
|
}) {
|
||||||
dependencies.addAll(this)
|
dependencies.addAll(this)
|
||||||
|
if (excludeConfig != null) {
|
||||||
|
this.forEach { it.excluded.add(excludeConfig) }
|
||||||
|
}
|
||||||
|
|
||||||
this.map { FutureTask { it.jarFile.get() } }
|
this.map { FutureTask { it.jarFile.get() } }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Directive
|
@Directive
|
||||||
fun compile(vararg dep: String) = addToDependencies(project, dependencies, dep)
|
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
|
@Directive
|
||||||
fun compileOptional(vararg dep: String) {
|
fun compileOptional(vararg dep: String) {
|
||||||
addToDependencies(project, optionalDependencies, dep, optional = true)
|
addToDependencies(project, optionalDependencies, dep, optional = true)
|
||||||
|
|
|
@ -23,8 +23,8 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager)
|
||||||
flags: List<String>): TaskResult {
|
flags: List<String>): TaskResult {
|
||||||
|
|
||||||
// Dependencies
|
// Dependencies
|
||||||
val allDependencies = (info.dependencies
|
val allDependencies = (info.dependencies + dependencyManager.calculateDependencies(project, context!!,
|
||||||
+ dependencyManager.calculateDependencies(project, context!!, passedDependencies = info.dependencies))
|
passedDependencies = info.dependencies))
|
||||||
.distinct()
|
.distinct()
|
||||||
|
|
||||||
// Plugins that add flags to the compiler
|
// Plugins that add flags to the compiler
|
||||||
|
|
|
@ -88,6 +88,7 @@ open class JvmCompilerPlugin @Inject constructor(
|
||||||
if (testContributor != null && testContributor.affinity(project, context) > 0) {
|
if (testContributor != null && testContributor.affinity(project, context) > 0) {
|
||||||
// val td1 = dependencyManager.testDependencies(project, context)
|
// val td1 = dependencyManager.testDependencies(project, context)
|
||||||
val testDependencies = dependencyManager.calculateDependencies(project, context,
|
val testDependencies = dependencyManager.calculateDependencies(project, context,
|
||||||
|
dependencyFilter = dependencyManager.createDependencyFilter(project.testDependencies),
|
||||||
scopes = listOf(Scope.TEST))
|
scopes = listOf(Scope.TEST))
|
||||||
val compileDependencies = dependencyManager.calculateDependencies(project, context,
|
val compileDependencies = dependencyManager.calculateDependencies(project, context,
|
||||||
scopes = listOf(Scope.COMPILE))
|
scopes = listOf(Scope.COMPILE))
|
||||||
|
|
|
@ -178,11 +178,11 @@ class DependencyManager @Inject constructor(val executors: KobaltExecutors,
|
||||||
dependencyFilter: DependencyFilter? = null,
|
dependencyFilter: DependencyFilter? = null,
|
||||||
requiredBy: String? = null): List<IClasspathDependency> {
|
requiredBy: String? = null): List<IClasspathDependency> {
|
||||||
val result = arrayListOf<IClasspathDependency>()
|
val result = arrayListOf<IClasspathDependency>()
|
||||||
dependencies.forEach {
|
dependencies.forEach { dependency ->
|
||||||
result.add(it)
|
result.add(dependency)
|
||||||
if (it.isMaven) {
|
if (dependency.isMaven) {
|
||||||
val resolved = resolver.resolveToIds(it.id, null, dependencyFilter)
|
val resolved = resolver.resolveToIds(dependency.id, null, dependencyFilter).map { create(it) }
|
||||||
result.addAll(resolved.map { create(it) })
|
result.addAll(resolved)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val reordered = reorderDependencies(result)
|
val reordered = reorderDependencies(result)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.beust.kobalt.maven.aether
|
package com.beust.kobalt.maven.aether
|
||||||
|
|
||||||
|
import com.beust.kobalt.api.Dependencies
|
||||||
import com.beust.kobalt.api.IClasspathDependency
|
import com.beust.kobalt.api.IClasspathDependency
|
||||||
import com.beust.kobalt.api.Kobalt
|
import com.beust.kobalt.api.Kobalt
|
||||||
import com.beust.kobalt.maven.CompletedFuture
|
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 shortId = artifact.groupId + ":" + artifact.artifactId + ":" + artifact.classifier
|
||||||
|
|
||||||
|
override val excluded = arrayListOf<Dependencies.ExcludeConfig>()
|
||||||
|
|
||||||
override fun compareTo(other: AetherDependency): Int {
|
override fun compareTo(other: AetherDependency): Int {
|
||||||
return Versions.toLongVersion(artifact.version).compareTo(Versions.toLongVersion(
|
return Versions.toLongVersion(artifact.version).compareTo(Versions.toLongVersion(
|
||||||
other.artifact.version))
|
other.artifact.version))
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.beust.kobalt.maven.dependency
|
package com.beust.kobalt.maven.dependency
|
||||||
|
|
||||||
|
import com.beust.kobalt.api.Dependencies
|
||||||
import com.beust.kobalt.api.IClasspathDependency
|
import com.beust.kobalt.api.IClasspathDependency
|
||||||
import com.beust.kobalt.maven.CompletedFuture
|
import com.beust.kobalt.maven.CompletedFuture
|
||||||
import org.apache.maven.model.Dependency
|
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 fun directDependencies() = arrayListOf<IClasspathDependency>()
|
||||||
|
|
||||||
|
override val excluded = arrayListOf<Dependencies.ExcludeConfig>()
|
||||||
|
|
||||||
override fun compareTo(other: FileDependency) = fileName.compareTo(other.fileName)
|
override fun compareTo(other: FileDependency) = fileName.compareTo(other.fileName)
|
||||||
|
|
||||||
override fun toString() = fileName
|
override fun toString() = fileName
|
||||||
|
|
|
@ -8,6 +8,7 @@ import com.beust.kobalt.api.Project
|
||||||
import com.beust.kobalt.internal.build.BuildFile
|
import com.beust.kobalt.internal.build.BuildFile
|
||||||
import com.beust.kobalt.internal.build.VersionFile
|
import com.beust.kobalt.internal.build.VersionFile
|
||||||
import com.beust.kobalt.maven.DependencyManager
|
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.BlockExtractor
|
||||||
import com.beust.kobalt.misc.KFiles
|
import com.beust.kobalt.misc.KFiles
|
||||||
import com.beust.kobalt.misc.kobaltLog
|
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,
|
private fun generateJarFile(context: KobaltContext, buildFile: BuildFile,
|
||||||
originalFile: BuildFile) {
|
buildScriptJarFile: File, originalFile: BuildFile) {
|
||||||
|
|
||||||
//
|
//
|
||||||
// Compile the jar file
|
// Compile the jar file
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue