From c57b8c96e9c665b5dc277c05a984d2feea25712c Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Fri, 25 Mar 2016 10:03:34 +0400 Subject: [PATCH] Latest version. --- .../com/beust/kobalt/ResolveDependency.kt | 33 +++++------ .../com/beust/kobalt/maven/aether/Aether.kt | 58 ++++++++++++------- 2 files changed, 51 insertions(+), 40 deletions(-) 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 086751b8..f26740a3 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 @@ -4,7 +4,7 @@ import com.beust.kobalt.api.IClasspathDependency import com.beust.kobalt.maven.LocalRepo import com.beust.kobalt.maven.MavenId import com.beust.kobalt.maven.RepoFinder -import com.beust.kobalt.maven.dependency.MavenDependency +import com.beust.kobalt.maven.aether.KobaltAether import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.Node import com.beust.kobalt.misc.log @@ -16,8 +16,8 @@ import java.util.* */ class ResolveDependency @Inject constructor(val repoFinder: RepoFinder, val localRepo: LocalRepo, - val executors: KobaltExecutors, - val mdFactory: MavenDependency.IFactory) { + val aether: KobaltAether, + val executors: KobaltExecutors) { val increment = 8 val leftFirst = "\u2558" val leftMiddle = "\u255f" @@ -29,28 +29,21 @@ class ResolveDependency @Inject constructor(val repoFinder: RepoFinder, fun run(ids: List) = ids.forEach { displayDependenciesFor(it) } private fun displayDependenciesFor(id: String) { - val repoResult = repoFinder.findCorrectRepo(id) + val mavenId = MavenId.create(id) + val resolved = + if (mavenId.hasVersion) aether.resolve(id) + else aether.latestArtifact(mavenId.groupId, mavenId.artifactId) + displayDependencies(resolved.dependency, resolved.repoUrl) + } + + private fun displayDependencies(dep: IClasspathDependency, url: String) { val indent = -1 - - val originalId = MavenId.create(id) - val mavenId = MavenId.create(originalId.groupId, originalId.artifactId, originalId.packaging, - originalId.version) - - val originalDep = mdFactory.create(mavenId, executors.dependencyExecutor, - downloadSources = true, downloadJavadocs = true) - val packaging = if (mavenId.packaging != null) "@" + mavenId.packaging else "" - - // We want to display the dependencies of the id we found, not the one we queries - val dep = mdFactory.create(MavenId.create(originalDep.shortId + repoResult.version + packaging), - executors.dependencyExecutor, true, true) val root = Node(Dep(dep, indent)) - val seen = hashSetOf(id) + val seen = hashSetOf(dep.id) root.addChildren(findChildren(root, seen)) - val url = repoResult.hostConfig.url + repoResult.path - val localFile = localRepo.toFullPath(repoResult.path!!) - AsciiArt.logBox(listOf(id, url, localFile).map { " $it" }, {s -> println(s) }) + AsciiArt.logBox(listOf(dep.id, url, dep.jarFile.get()).map { " $it" }, {s -> println(s) }) display(root.children) println("") diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/Aether.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/Aether.kt index 57d16c75..084f67c1 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/Aether.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/Aether.kt @@ -27,6 +27,8 @@ import java.util.concurrent.Future val TEST_DIR = ".aether/repository" +class DependencyResult(val dependency: IClasspathDependency, val repoUrl: String) + class KobaltAether(val localRepo: File = File(homeDir(TEST_DIR))) { fun create(id: String): IClasspathDependency { val aether = Aether(localRepo) @@ -34,6 +36,20 @@ class KobaltAether(val localRepo: File = File(homeDir(TEST_DIR))) { return if (cr != null) AetherDependency(cr.root.artifact) else throw KobaltException("Couldn't resolve $id") } + + fun latestArtifact(group: String, artifactId: String, extension: String = "jar") : DependencyResult + = Aether(localRepo).latestArtifact(group, artifactId, extension).let { + DependencyResult(AetherDependency(it.artifact), it.repository.toString()) + } + + fun resolve(id: String): DependencyResult { + val results = Aether(localRepo).resolve(DefaultArtifact(id)) + if (results != null && results.size > 0) { + return DependencyResult(AetherDependency(results[0].artifact), results[0].repository.toString()) + } else { + throw KobaltException("Couldn't resolve $id") + } + } } class ExcludeOptionalDependencyFilter: DependencyFilter { @@ -68,6 +84,23 @@ class Aether(val localRepo: File = File(homeDir(TEST_DIR))) { } } + fun latestArtifact(group: String, artifactId: String, extension: String = "jar") : ArtifactResult { + 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(newArtifact) + if (artifactResult != null) { + return artifactResult[0] + } else { + throw KobaltException("Couldn't find latest artifact for $group:$artifactId") + } + } else { + throw KobaltException("Couldn't find latest artifact for $group:$artifactId") + } + } + fun resolveVersion(artifact: Artifact): VersionRangeResult? { val metadata = DefaultMetadata(artifact.groupId, artifact.artifactId, "maven-metadata.xml", org.eclipse.aether.metadata.Metadata.Nature.RELEASE) @@ -105,11 +138,8 @@ class Aether(val localRepo: File = File(homeDir(TEST_DIR))) { fun transitiveDependencies(artifact: Artifact) = directDependencies(artifact) - fun directDependencies(artifact: Artifact): CollectResult? { - val result = system.collectDependencies(session, collectRequest(artifact)) - val root = result.root - return result - } + fun directDependencies(artifact: Artifact): CollectResult? + = system.collectDependencies(session, collectRequest(artifact)) } class AetherDependency(val artifact: Artifact): IClasspathDependency, Comparable { @@ -183,19 +213,7 @@ class AetherDependency(val artifact: Artifact): IClasspathDependency, Comparable fun main(argv: Array) { KobaltLogger.LOG_LEVEL = 2 val aether = Aether() - val artifact = DefaultArtifact("org.testng:testng:(0,]") - aether.resolveVersion(artifact)?.let { versionResult -> - println("Latest version: " + versionResult + " " + versionResult.highestVersion) - println("") -// val newArtifact = DefaultArtifact(artifact.groupId, artifact.artifactId, artifact.extension, -// versionResult.highestVersion) -// val artifactResult = aether.resolve(newArtifact) -// println(" File: " + artifactResult) - } - val d2 = Aether().resolve(artifact) -// val dd = Aether().resolve("org.testng:testng:6.9.9") -// val artifact = d2?.root?.artifact - if (d2 != null && d2.size > 0) { - println("DD: " + d2) - } + val latestResult = aether.latestArtifact("org.testng", "testng") + val latest = latestResult.artifact + println("Latest: " + latest.version + " " + latest.file) }