mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 00:17:11 -07:00
New KobaltMavenResolver.
This commit is contained in:
parent
7d9d2c00ed
commit
36d2953c8c
7 changed files with 222 additions and 77 deletions
|
@ -3,7 +3,9 @@ package com.beust.kobalt
|
|||
import com.beust.kobalt.api.IClasspathDependency
|
||||
import com.beust.kobalt.maven.LocalRepo
|
||||
import com.beust.kobalt.maven.MavenId
|
||||
import com.beust.kobalt.maven.aether.KobaltAether
|
||||
import com.beust.kobalt.maven.aether.DependencyResult
|
||||
import com.beust.kobalt.maven.aether.Filters
|
||||
import com.beust.kobalt.maven.aether.KobaltMavenResolver
|
||||
import com.beust.kobalt.misc.KobaltExecutors
|
||||
import com.beust.kobalt.misc.Node
|
||||
import com.beust.kobalt.misc.kobaltLog
|
||||
|
@ -15,7 +17,7 @@ import java.util.*
|
|||
*/
|
||||
class ResolveDependency @Inject constructor(
|
||||
val localRepo: LocalRepo,
|
||||
val aether: KobaltAether,
|
||||
val aether: KobaltMavenResolver,
|
||||
val executors: KobaltExecutors) {
|
||||
val increment = 8
|
||||
val leftFirst = "\u2558"
|
||||
|
@ -29,9 +31,13 @@ class ResolveDependency @Inject constructor(
|
|||
|
||||
private fun displayDependenciesFor(id: String) {
|
||||
val mavenId = MavenId.create(id)
|
||||
val resolved =
|
||||
if (mavenId.hasVersion) aether.resolve(id)
|
||||
else aether.latestArtifact(mavenId.groupId, mavenId.artifactId)
|
||||
val resolved : DependencyResult =
|
||||
if (mavenId.hasVersion) {
|
||||
val dep = aether.resolveToDependencies(id, filter = Filters.EXCLUDE_OPTIONAL_FILTER)[0]
|
||||
DependencyResult(dep, "")
|
||||
} else {
|
||||
aether.latestArtifact(mavenId.groupId, mavenId.artifactId)
|
||||
}
|
||||
|
||||
displayDependencies(resolved.dependency, resolved.repoUrl)
|
||||
}
|
||||
|
@ -60,7 +66,7 @@ class ResolveDependency @Inject constructor(
|
|||
if (i % increment == 0) print(vertical)
|
||||
else print(" ")
|
||||
}
|
||||
println(left + " " + dep.id)
|
||||
println(left + " " + dep.id + (if (dep.optional) " (optional)" else ""))
|
||||
display(node.children)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import com.beust.kobalt.KobaltException
|
|||
import com.beust.kobalt.api.*
|
||||
import com.beust.kobalt.maven.aether.Filters
|
||||
import com.beust.kobalt.maven.aether.KobaltAether
|
||||
import com.beust.kobalt.maven.aether.KobaltMavenResolver
|
||||
import com.beust.kobalt.maven.aether.Scope
|
||||
import com.beust.kobalt.maven.dependency.FileDependency
|
||||
import com.beust.kobalt.misc.KFiles
|
||||
|
@ -17,7 +18,7 @@ import javax.inject.Inject
|
|||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class DependencyManager @Inject constructor(val executors: KobaltExecutors, val aether: KobaltAether)
|
||||
class DependencyManager @Inject constructor(val executors: KobaltExecutors, val aether: KobaltMavenResolver)
|
||||
: IDependencyManager {
|
||||
|
||||
companion object {
|
||||
|
@ -162,7 +163,7 @@ class DependencyManager @Inject constructor(val executors: KobaltExecutors, val
|
|||
dependencies.forEach {
|
||||
result.add(it)
|
||||
if (it.isMaven) {
|
||||
val resolved = aether.resolveAll(it.id, null, dependencyFilter)
|
||||
val resolved = aether.resolveToIds(it.id, null, dependencyFilter)
|
||||
result.addAll(resolved.map { create(it) })
|
||||
}
|
||||
}
|
||||
|
|
|
@ -220,7 +220,7 @@ class Aether(localRepo: File, val settings: KobaltSettings, eventBus: EventBus)
|
|||
|
||||
class AetherDependency(val artifact: Artifact, override val optional: Boolean = false)
|
||||
: IClasspathDependency, Comparable<AetherDependency> {
|
||||
val aether: Aether get() = Kobalt.INJECTOR.getInstance(Aether::class.java)
|
||||
val aether: KobaltMavenResolver get() = Kobalt.INJECTOR.getInstance(KobaltMavenResolver::class.java)
|
||||
|
||||
override val id: String = toId(artifact)
|
||||
|
||||
|
@ -240,16 +240,7 @@ class AetherDependency(val artifact: Artifact, override val optional: Boolean =
|
|||
CompletedFuture(file)
|
||||
} else {
|
||||
val td = aether.resolve(artifact, null)
|
||||
if (td.any()) {
|
||||
val newFile = td[0].artifact.file
|
||||
if (newFile != null) {
|
||||
CompletedFuture(newFile)
|
||||
} else {
|
||||
CompletedFuture(File("DOESNOTEXIST $id")) // will be filtered out
|
||||
}
|
||||
} else {
|
||||
CompletedFuture(File("DOESNOTEXIST $id"))
|
||||
}
|
||||
CompletedFuture(td.artifact.file)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -269,17 +260,8 @@ class AetherDependency(val artifact: Artifact, override val optional: Boolean =
|
|||
val result = arrayListOf<IClasspathDependency>()
|
||||
val deps = aether.directDependencies(artifact)
|
||||
if (deps != null) {
|
||||
if (!deps.root.dependency.optional) {
|
||||
deps.root.children.forEach {
|
||||
if (!it.dependency.isOptional) {
|
||||
result.add(AetherDependency(it.artifact))
|
||||
} else {
|
||||
kobaltLog(ConsoleRepositoryListener.LOG_LEVEL,
|
||||
"Skipping optional dependency " + deps.root.artifact)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
kobaltLog(ConsoleRepositoryListener.LOG_LEVEL, "Skipping optional dependency " + deps.root.artifact)
|
||||
deps.root.children.forEach {
|
||||
result.add(AetherDependency(it.artifact, it.dependency.optional))
|
||||
}
|
||||
} else {
|
||||
warn("Couldn't resolve $artifact")
|
||||
|
|
|
@ -10,6 +10,6 @@ object Filters {
|
|||
val TEST_FILTER = DependencyFilter { p0, p1 -> p0.dependency.scope == JavaScopes.TEST }
|
||||
|
||||
val EXCLUDE_OPTIONAL_FILTER = DependencyFilter { p0, p1 ->
|
||||
! p0.dependency.optional
|
||||
p0.dependency != null && ! p0.dependency.optional
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,129 @@
|
|||
package com.beust.kobalt.maven.aether
|
||||
|
||||
import com.beust.kobalt.KobaltException
|
||||
import com.beust.kobalt.api.IClasspathDependency
|
||||
import com.beust.kobalt.api.Kobalt
|
||||
import com.beust.kobalt.internal.KobaltSettings
|
||||
import com.beust.kobalt.internal.getProxy
|
||||
import com.beust.kobalt.maven.LocalRepo
|
||||
import com.beust.kobalt.maven.MavenId
|
||||
import com.google.common.eventbus.EventBus
|
||||
import com.google.inject.Inject
|
||||
import org.eclipse.aether.artifact.Artifact
|
||||
import org.eclipse.aether.artifact.DefaultArtifact
|
||||
import org.eclipse.aether.collection.CollectRequest
|
||||
import org.eclipse.aether.collection.CollectResult
|
||||
import org.eclipse.aether.graph.DefaultDependencyNode
|
||||
import org.eclipse.aether.graph.Dependency
|
||||
import org.eclipse.aether.graph.DependencyFilter
|
||||
import org.eclipse.aether.graph.DependencyNode
|
||||
import org.eclipse.aether.repository.RemoteRepository
|
||||
import org.eclipse.aether.resolution.DependencyRequest
|
||||
import org.eclipse.aether.resolution.VersionRangeRequest
|
||||
import org.eclipse.aether.resolution.VersionRangeResult
|
||||
|
||||
class KobaltMavenResolver @Inject constructor(val settings: KobaltSettings,
|
||||
localRepo: LocalRepo, eventBus: EventBus) {
|
||||
fun resolve(id: String, scope: Scope? = null, filter: DependencyFilter? = null): DependencyNode {
|
||||
val dependencyRequest = DependencyRequest(createCollectRequest(id, scope), filter)
|
||||
val result = system.resolveDependencies(session, dependencyRequest)
|
||||
|
||||
// GraphUtil.displayGraph(listOf(result.root), { it -> it.children },
|
||||
// { it: DependencyNode, indent: String -> println(indent + it.toString()) })
|
||||
return result.root
|
||||
}
|
||||
|
||||
fun resolve(artifact: Artifact, scope: Scope? = null, filter: DependencyFilter? = null)
|
||||
= resolve(artifactToId(artifact), scope, filter)
|
||||
|
||||
fun resolveToIds(id: String, scope: Scope? = null, filter: DependencyFilter? = null) : List<String> {
|
||||
val root = resolve(id, scope, filter)
|
||||
val children =
|
||||
root.children.filter {
|
||||
filter == null || filter.accept(DefaultDependencyNode(it.dependency), emptyList())
|
||||
}.filter {
|
||||
it.dependency.scope != Scope.SYSTEM.scope
|
||||
}
|
||||
val result = listOf(artifactToId(root.artifact)) + children.flatMap {
|
||||
val thisId = artifactToId(it.artifact)
|
||||
resolveToIds(thisId, scope, filter)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun resolveToDependencies(id: String, scope: Scope? = null, filter: DependencyFilter? = null)
|
||||
: List<IClasspathDependency> {
|
||||
val result = resolveToIds(id, scope, filter).map {
|
||||
create(it, false)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun directDependencies(id: String, scope: Scope? = null): CollectResult? {
|
||||
val result = system.collectDependencies(session, createCollectRequest(id, scope))
|
||||
return result
|
||||
}
|
||||
|
||||
fun directDependencies(artifact: Artifact, scope: Scope? = null): CollectResult?
|
||||
= artifactToId(artifact).let { id ->
|
||||
directDependencies(id, scope)
|
||||
}
|
||||
|
||||
fun artifactToId(artifact: Artifact) = artifact.let {
|
||||
MavenId.toId(it.groupId, it.artifactId, it.extension, it.classifier, it.version)
|
||||
}
|
||||
|
||||
private fun resolveVersion(artifact: Artifact): VersionRangeResult? {
|
||||
val request = VersionRangeRequest(artifact, kobaltRepositories, null)
|
||||
val result = system.resolveVersionRange(session, request)
|
||||
return result
|
||||
}
|
||||
|
||||
private fun latestMavenArtifact(group: String, artifactId: String, extension: String = "jar"): DependencyNode {
|
||||
val artifact = DefaultArtifact(group, artifactId, extension, "(0,]")
|
||||
val resolved = resolveVersion(artifact)
|
||||
if (resolved != null) {
|
||||
val newArtifact = DefaultArtifact(artifact.groupId, artifact.artifactId, artifact.extension,
|
||||
resolved.highestVersion.toString())
|
||||
val artifactResult = resolve(artifactToId(newArtifact), null)
|
||||
return artifactResult
|
||||
// if (artifactResult != null) {
|
||||
// return artifactResult
|
||||
// } else {
|
||||
// throw KobaltException("Couldn't find latest artifact for $group:$artifactId")
|
||||
// }
|
||||
} else {
|
||||
throw KobaltException("Couldn't find latest artifact for $group:$artifactId")
|
||||
}
|
||||
}
|
||||
|
||||
fun latestArtifact(group: String, artifactId: String, extension: String = "jar"): DependencyResult
|
||||
= latestMavenArtifact(group, artifactId, extension).let {
|
||||
DependencyResult(AetherDependency(it.artifact), "(TBD repo)")
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an IClasspathDependency from a Kobalt id.
|
||||
*/
|
||||
fun create(id: String, optional: Boolean) = AetherDependency(DefaultArtifact(id), optional)
|
||||
|
||||
private val system = Booter.newRepositorySystem()
|
||||
private val session = Booter.newRepositorySystemSession(system, localRepo.localRepo, settings, eventBus)
|
||||
|
||||
private val kobaltRepositories: List<RemoteRepository>
|
||||
get() = Kobalt.repos.map {
|
||||
RemoteRepository.Builder(null, "default", it.url)
|
||||
// .setSnapshotPolicy(RepositoryPolicy(false, null, null))
|
||||
.build().let { repository ->
|
||||
val proxyConfigs = settings.proxyConfigs ?: return@map repository
|
||||
RemoteRepository.Builder(repository).apply {
|
||||
setProxy(proxyConfigs.getProxy(repository.protocol)?.toAetherProxy())
|
||||
}.build()
|
||||
}
|
||||
}
|
||||
|
||||
private fun createCollectRequest(id: String, scope: Scope? = null) = CollectRequest().apply {
|
||||
root = Dependency(DefaultArtifact(MavenId.toKobaltId(id)), scope?.scope)
|
||||
repositories = kobaltRepositories
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue