1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27:12 -07:00

Fix hosts for ResolveDependency.

This commit is contained in:
Cedric Beust 2017-02-07 16:24:56 -08:00
parent 8e583c5a73
commit 75dda87fdd
9 changed files with 50 additions and 65 deletions

View file

@ -3,13 +3,15 @@ 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.DependencyResult
import com.beust.kobalt.maven.aether.AetherDependency
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
import com.google.inject.Inject
import org.eclipse.aether.artifact.DefaultArtifact
import org.eclipse.aether.graph.DependencyNode
import java.util.*
/**
@ -29,14 +31,34 @@ class ResolveDependency @Inject constructor(
fun run(id: String) = displayDependenciesFor(id)
private fun latestMavenArtifact(group: String, artifactId: String, extension: String = "jar"): DependencyNode {
val artifact = DefaultArtifact(group, artifactId, extension, "(0,]")
val resolved = aether.resolveVersion(artifact)
if (resolved != null) {
val newArtifact = DefaultArtifact(artifact.groupId, artifact.artifactId, artifact.extension,
resolved.highestVersion.toString())
val artifactResult = aether.resolve(KobaltMavenResolver.artifactToId(newArtifact), null)
return artifactResult.root
} else {
throw KobaltException("Couldn't find latest artifact for $group:$artifactId")
}
}
class PairResult(val dependency: IClasspathDependency, val repoUrl: String)
fun latestArtifact(group: String, artifactId: String, extension: String = "jar"): PairResult
= latestMavenArtifact(group, artifactId, extension).let {
PairResult(AetherDependency(it.artifact), "(TBD repo)")
}
private fun displayDependenciesFor(id: String) {
val mavenId = MavenId.create(id)
val resolved : DependencyResult =
val resolved : PairResult =
if (mavenId.hasVersion) {
val dep = aether.resolveToDependencies(id, filter = Filters.EXCLUDE_OPTIONAL_FILTER)[0]
DependencyResult(dep, "")
val node = aether.resolve(id, filter = Filters.EXCLUDE_OPTIONAL_FILTER)
PairResult(AetherDependency(node.root.artifact), node.artifactResults[0].repository.id)
} else {
aether.latestArtifact(mavenId.groupId, mavenId.artifactId)
latestArtifact(mavenId.groupId, mavenId.artifactId)
}
displayDependencies(resolved.dependency, resolved.repoUrl)

View file

@ -53,7 +53,7 @@ class KobaltContext(val args: Args) {
FileType.JAVADOC -> toQualifier(dep, "", "javadoc")
FileType.OTHER -> id
}
val resolved = resolver.resolve(fullId).artifact
val resolved = resolver.resolveToArtifact(fullId)
if (resolved != null) {
return resolved.file
} else {

View file

@ -150,8 +150,8 @@ class Dependencies(val project: Project,
val resolved =
if (KobaltMavenResolver.isRangeVersion(it)) {
// Range id
val node = Kobalt.INJECTOR.getInstance(KobaltMavenResolver::class.java).resolve(it)
val result = KobaltMavenResolver.artifactToId(node.artifact)
val node = Kobalt.INJECTOR.getInstance(KobaltMavenResolver::class.java).resolveToArtifact(it)
val result = KobaltMavenResolver.artifactToId(node)
kobaltLog(2, "Resolved range id $it to $result")
result
} else {

View file

@ -62,7 +62,7 @@ class DependencyManager @Inject constructor(val executors: KobaltExecutors,
*/
override fun createMaven(id: String, optional: Boolean) : IClasspathDependency=
if (KobaltMavenResolver.isRangeVersion(id)) {
Kobalt.INJECTOR.getInstance(KobaltMavenResolver::class.java).resolveToDependencies(id)[0]
Kobalt.INJECTOR.getInstance(DependencyManager::class.java).create(id, optional)
} else {
resolver.create(id, optional)
}

View file

@ -34,7 +34,7 @@ class AetherDependency(val artifact: Artifact, override val optional: Boolean =
CompletedFuture(file)
} else {
val td = aether.resolve(artifact, null)
CompletedFuture(td.artifact.file)
CompletedFuture(td.root.artifact.file)
}
}

View file

@ -1,5 +0,0 @@
package com.beust.kobalt.maven.aether
import com.beust.kobalt.api.IClasspathDependency
class DependencyResult(val dependency: IClasspathDependency, val repoUrl: String)

View file

@ -1,7 +1,5 @@
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
@ -16,9 +14,9 @@ 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.DependencyResult
import org.eclipse.aether.resolution.VersionRangeRequest
import org.eclipse.aether.resolution.VersionRangeResult
@ -32,45 +30,38 @@ class KobaltMavenResolver @Inject constructor(val settings: KobaltSettings,
fun isRangeVersion(id: String) = id.contains(",")
}
fun resolve(id: String, scope: Scope? = null, filter: DependencyFilter? = null): DependencyNode {
fun resolveToArtifact(id: String, scope: Scope? = null, filter: DependencyFilter? = null) : Artifact
= resolve(id, scope, filter).root.artifact
fun resolve(id: String, scope: Scope? = null, filter: DependencyFilter? = null): DependencyResult {
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
return result
}
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 rr = resolve(id, scope, filter)
val children =
root.children.filter {
rr.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 result = listOf(artifactToId(rr.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(id: String, scope: Scope? = null): CollectResult?
= system.collectDependencies(session, createCollectRequest(id, scope))
fun directDependencies(artifact: Artifact, scope: Scope? = null): CollectResult?
= artifactToId(artifact).let { id ->
@ -83,29 +74,6 @@ class KobaltMavenResolver @Inject constructor(val settings: KobaltSettings,
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.
*/

View file

@ -5,6 +5,7 @@ import com.beust.kobalt.KobaltTest
import com.beust.kobalt.maven.aether.KobaltMavenResolver
import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.warn
import org.assertj.core.api.Assertions.assertThat
import org.testng.Assert
import org.testng.annotations.BeforeClass
import org.testng.annotations.Test
@ -150,10 +151,9 @@ class DownloadTest @Inject constructor(
// This id has a parent pom which defines moshi version to be 1.1.0. Make sure that this
// version is being fetched instead of moshi:1.2.0-SNAPSHOT (which gets discarded anyway
// since snapshots are not allowed to be returned when looking up a versionless id)
val host = HostConfig("http://repository.jetbrains.com/all/")
val id = "com.squareup.moshi:moshi:1.1.0"
val dr = resolver.resolve(id)
Assert.assertEquals(dr.dependency.artifact.version, "1.1.0")
val artifact = resolver.resolveToArtifact(id)
assertThat(artifact.version).isEqualTo("1.1.0")
}
@Test

View file

@ -47,11 +47,11 @@ class MavenResolverTest {
@Test(dataProvider = "rangeProvider")
fun kobaltRangeVersion(id: String, expectedVersion: String) {
val result = resolver.resolve(id)
assertThat(result.dependency.artifact.version).isEqualTo(expectedVersion)
val artifact = resolver.resolveToArtifact(id)
assertThat(artifact.version).isEqualTo(expectedVersion)
}
// @Test
@Test
fun aetherShouldNotIncludeOptionalDependencies() {
val artifactResults = resolve("com.squareup.retrofit2:converter-jackson:jar:2.1.0")
@ -59,7 +59,7 @@ class MavenResolverTest {
assertThat(artifactResults.none { it.toString().contains("android") })
}
// @Test
@Test
fun kobaltAetherShouldNotIncludeOptionalDependencies() {
val dep = resolver.create("com.squareup.retrofit2:converter-jackson:jar:2.1.0", optional = false)
val closure = dependencyManager.transitiveClosure(listOf(dep))