diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ResolveDependency.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ResolveDependency.kt index 2cb9c49c..d333298a 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ResolveDependency.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ResolveDependency.kt @@ -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) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/KobaltContext.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/KobaltContext.kt index ae4418b2..983c5961 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/KobaltContext.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/KobaltContext.kt @@ -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 { 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 e50317bc..5e45e7cb 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 @@ -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 { 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 4a77fe02..d609c905 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 @@ -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) } 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 d6826c78..7ec9bfe0 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 @@ -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) } } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/DependencyResult.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/DependencyResult.kt deleted file mode 100644 index bcf1e097..00000000 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/DependencyResult.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.beust.kobalt.maven.aether - -import com.beust.kobalt.api.IClasspathDependency - -class DependencyResult(val dependency: IClasspathDependency, val repoUrl: String) \ No newline at end of file diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/KobaltMavenResolver.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/KobaltMavenResolver.kt index 6ec960fa..69192c20 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/KobaltMavenResolver.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/KobaltMavenResolver.kt @@ -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 { - 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 { - 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. */ diff --git a/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt b/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt index 26523ce8..820bef21 100644 --- a/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt +++ b/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt @@ -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 diff --git a/src/test/kotlin/com/beust/kobalt/misc/MavenResolverTest.kt b/src/test/kotlin/com/beust/kobalt/misc/MavenResolverTest.kt index 74a9580b..45bccdcb 100644 --- a/src/test/kotlin/com/beust/kobalt/misc/MavenResolverTest.kt +++ b/src/test/kotlin/com/beust/kobalt/misc/MavenResolverTest.kt @@ -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))