From 33f01cdb271721f9cfc6d9b4d5b74317ae3fdac2 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Mon, 25 Jul 2016 00:41:42 -0800 Subject: [PATCH] Introduce KobaltContext#filefor(id). --- .../com/beust/kobalt/api/KobaltContext.kt | 38 ++++++++ .../com/beust/kobalt/maven/aether/Aether.kt | 87 ++++++++----------- .../com/beust/kobalt/app/BuildFileCompiler.kt | 6 +- 3 files changed, 80 insertions(+), 51 deletions(-) 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 1b4ee15c..a503e9de 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 @@ -1,12 +1,16 @@ package com.beust.kobalt.api import com.beust.kobalt.Args +import com.beust.kobalt.KobaltException import com.beust.kobalt.Plugins import com.beust.kobalt.Variant import com.beust.kobalt.internal.IncrementalManager import com.beust.kobalt.internal.KobaltSettings import com.beust.kobalt.internal.PluginInfo import com.beust.kobalt.maven.DependencyManager +import com.beust.kobalt.maven.MavenId +import com.beust.kobalt.maven.SimpleDep +import com.beust.kobalt.maven.aether.KobaltAether import com.beust.kobalt.misc.KobaltExecutors import java.io.File @@ -22,6 +26,39 @@ class KobaltContext(val args: Args) { fun findPlugin(name: String) = Plugins.findPlugin(name) + /** + * Files that can be resolved in the local cache. + */ + enum class FileType { JAR, POM, SOURCES, JAVADOC, OTHER } + + /** + * @param{id} is the Maven coordinate (e.g. "org.testng:testng:6.9.11"). If you are looking for a file + * that is not described by the enum (e.g. "aar"), use OTHER and make sure your @param{id} contains + * the fully qualified id (e.g. "com.example:example::aar:1.0"). + */ + fun fileFor(id: String, fileType: FileType) : File { + val dep = SimpleDep(MavenId.create(id)) + fun toQualifier(dep: SimpleDep, ext: String, qualifier: String?) = + dep.groupId + ":" + dep.artifactId + + ":$ext" + + (if (qualifier != null) ":$qualifier" else "") + + ":" + dep.version + val fullId = + when (fileType) { + FileType.JAR -> toQualifier(dep, "jar", null) + FileType.POM -> toQualifier(dep, "pom", null) + FileType.SOURCES -> toQualifier(dep, "", "sources") + FileType.JAVADOC -> toQualifier(dep, "", "javadoc") + FileType.OTHER -> id + } + val resolved = aether.resolveToArtifact(fullId) + if (resolved != null) { + return resolved.artifact.file + } else { + throw KobaltException("Couldn't resolve $id") + } + } + /** All the projects that are being built during this run */ val allProjects = arrayListOf() @@ -37,6 +74,7 @@ class KobaltContext(val args: Args) { lateinit var executors: KobaltExecutors lateinit var settings: KobaltSettings lateinit var incrementalManager: IncrementalManager + lateinit var aether: KobaltAether } class InternalContext { 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 51999be7..cb9f6ced 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 @@ -45,10 +45,10 @@ class KobaltAether @Inject constructor (val settings: KobaltSettings, val aether /** * @return the latest artifact for the given group and artifactId. */ - fun latestArtifact(group: String, artifactId: String, extension: String = "jar") : DependencyResult - = aether.latestArtifact(group, artifactId, extension).let { - DependencyResult(AetherDependency(it.artifact), it.repository.toString()) - } + fun latestArtifact(group: String, artifactId: String, extension: String = "jar"): DependencyResult + = aether.latestArtifact(group, artifactId, extension).let { + DependencyResult(AetherDependency(it.artifact), it.repository.toString()) + } fun resolveAll(id: String, isTest: Boolean): List { val results = aether.resolve(DefaultArtifact(id), isTest) @@ -57,23 +57,32 @@ class KobaltAether @Inject constructor (val settings: KobaltSettings, val aether fun resolve(id: String, isTest: Boolean = false): DependencyResult { log(ConsoleRepositoryListener.LOG_LEVEL, "Resolving $id") - val results = aether.resolve(DefaultArtifact(MavenId.toKobaltId(id)), isTest) - if (results.size > 0) { - return DependencyResult(AetherDependency(results[0].artifact), results[0].repository.toString()) + val result = resolveToArtifact(id, isTest) + if (result != null) { + return DependencyResult(AetherDependency(result.artifact), result.repository.toString()) } else { throw KobaltException("Couldn't resolve $id") } } -} + fun resolveToArtifact(id: String, isTest: Boolean = false): ArtifactResult? { + log(ConsoleRepositoryListener.LOG_LEVEL, "Resolving $id") + val results = aether.resolve(DefaultArtifact(MavenId.toKobaltId(id)), isTest) + if (results.size > 0) { + return results[0] + } else { + return null + } + } -class ExcludeOptionalDependencyFilter: DependencyFilter { - override fun accept(node: DependencyNode?, p1: MutableList?): Boolean { + class ExcludeOptionalDependencyFilter : DependencyFilter { + override fun accept(node: DependencyNode?, p1: MutableList?): Boolean { // val result = node != null && ! node.dependency.isOptional - val accept1 = node == null || node.artifact.artifactId != "srczip" - val accept2 = node != null && ! node.dependency.isOptional - val result = accept1 && accept2 - return result + val accept1 = node == null || node.artifact.artifactId != "srczip" + val accept2 = node != null && !node.dependency.isOptional + val result = accept1 && accept2 + return result + } } } @@ -82,15 +91,15 @@ class Aether(val localRepo: File, val settings: KobaltSettings, val eventBus: Ev private val system = Booter.newRepositorySystem() private val session = Booter.newRepositorySystemSession(system, localRepo, settings, eventBus) private val classpathFilter = AndDependencyFilter( - ExcludeOptionalDependencyFilter(), + KobaltAether.ExcludeOptionalDependencyFilter(), DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE), DependencyFilterUtils.classpathFilter(JavaScopes.TEST)) private val testClasspathFilter = AndDependencyFilter( - ExcludeOptionalDependencyFilter(), + KobaltAether.ExcludeOptionalDependencyFilter(), DependencyFilterUtils.classpathFilter(JavaScopes.TEST)) - private val kobaltRepositories : List + private val kobaltRepositories: List get() = Kobalt.repos.map { RemoteRepository.Builder("maven", "default", it.url) // .setSnapshotPolicy(RepositoryPolicy(false, null, null)) @@ -102,7 +111,7 @@ class Aether(val localRepo: File, val settings: KobaltSettings, val eventBus: Ev } } - private fun collectRequest(artifact: Artifact, isTest: Boolean) : CollectRequest { + private fun collectRequest(artifact: Artifact, isTest: Boolean): CollectRequest { with(CollectRequest()) { root = Dependency(artifact, if (isTest) JavaScopes.TEST else JavaScopes.COMPILE) repositories = kobaltRepositories @@ -111,7 +120,7 @@ class Aether(val localRepo: File, val settings: KobaltSettings, val eventBus: Ev } } - fun latestArtifact(group: String, artifactId: String, extension: String = "jar") : ArtifactResult { + fun latestArtifact(group: String, artifactId: String, extension: String = "jar"): ArtifactResult { val artifact = DefaultArtifact(group, artifactId, extension, "(0,]") val resolved = resolveVersion(artifact) if (resolved != null) { @@ -119,7 +128,7 @@ class Aether(val localRepo: File, val settings: KobaltSettings, val eventBus: Ev resolved.highestVersion.toString()) val artifactResult = resolve(newArtifact) if (artifactResult != null && artifactResult.size > 0) { - return artifactResult[0] + return artifactResult[0] } else { throw KobaltException("Couldn't find latest artifact for $group:$artifactId") } @@ -134,32 +143,8 @@ class Aether(val localRepo: File, val settings: KobaltSettings, val eventBus: Ev return result } - private fun oldResolveVErsion() { -// val artifact = DefaultArtifact(a.groupId, a.artifactId, null, "[0,)") -// val r = system.resolveMetadata(session, kobaltRepositories.map { -// MetadataRequest(metadata, it, null).apply { -// isFavorLocalRepository = false -// } -// }) - -// val metadata = DefaultMetadata(artifact.groupId, artifact.artifactId, "maven-metadata.xml", -// org.eclipse.aether.metadata.Metadata.Nature.RELEASE) -// -// kobaltRepositories.forEach { -// val request = MetadataRequest(metadata, it, null).apply { -// isFavorLocalRepository = false -// } -// val md = system.resolveMetadata(session, listOf(request)) -// if (artifact.groupId.contains("org.testng")) { -// println("DONOTCOMMIT") -// } -// println("Repo: $it " + md) -// } - - } - fun resolve(artifact: Artifact, isTest: Boolean = false): List { - fun manageException(ex: Exception, artifact: Artifact) : List { + fun manageException(ex: Exception, artifact: Artifact): List { if (artifact.extension == "pom") { // Only display a warning for .pom files. Not resolving a .jar or other artifact // is not necessarily an error as long as there is a pom file. @@ -186,10 +171,11 @@ class Aether(val localRepo: File, val settings: KobaltSettings, val eventBus: Ev = system.collectDependencies(session, collectRequest(artifact, isTest)) } -class AetherDependency(val artifact: Artifact): IClasspathDependency, Comparable { +class AetherDependency(val artifact: Artifact) : IClasspathDependency, Comparable { val aether: Aether get() = Kobalt.INJECTOR.getInstance(Aether::class.java) - constructor(node: DependencyNode) : this(node.artifact) {} + constructor(node: DependencyNode) : this(node.artifact) { + } override val id: String = toId(artifact) @@ -228,13 +214,13 @@ class AetherDependency(val artifact: Artifact): IClasspathDependency, Comparable } } - override fun directDependencies() : List { + override fun directDependencies(): List { val result = arrayListOf() val deps = aether.directDependencies(artifact) if (deps != null) { - if (! deps.root.dependency.optional) { + if (!deps.root.dependency.optional) { deps.root.children.forEach { - if (! it.dependency.isOptional) { + if (!it.dependency.isOptional) { result.add(AetherDependency(it.artifact)) } else { log(ConsoleRepositoryListener.LOG_LEVEL, "Skipping optional dependency " + deps.root.artifact) @@ -290,3 +276,4 @@ fun main(argv: Array) { // // println("Artifact: " + d) } + diff --git a/src/main/kotlin/com/beust/kobalt/app/BuildFileCompiler.kt b/src/main/kotlin/com/beust/kobalt/app/BuildFileCompiler.kt index aac58c59..659ff847 100644 --- a/src/main/kotlin/com/beust/kobalt/app/BuildFileCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/app/BuildFileCompiler.kt @@ -14,6 +14,8 @@ import com.beust.kobalt.internal.PluginInfo import com.beust.kobalt.internal.build.BuildFile import com.beust.kobalt.internal.build.VersionFile import com.beust.kobalt.maven.DependencyManager +import com.beust.kobalt.maven.LocalRepo +import com.beust.kobalt.maven.aether.KobaltAether import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.log @@ -33,7 +35,8 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b @Assisted val pluginInfo: PluginInfo, val files: KFiles, val plugins: Plugins, val dependencyManager: DependencyManager, val pluginProperties: PluginProperties, val executors: KobaltExecutors, val buildScriptUtil: BuildScriptUtil, val settings: KobaltSettings, - val incrementalManagerFactory: IncrementalManager.IFactory, val args: Args) { + val incrementalManagerFactory: IncrementalManager.IFactory, val args: Args, + val aether: KobaltAether) { interface IFactory { fun create(@Assisted("buildFiles") buildFiles: List, pluginInfo: PluginInfo) : BuildFileCompiler @@ -53,6 +56,7 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b context.executors = executors context.settings = settings context.incrementalManager = incrementalManagerFactory.create() + context.aether = aether Kobalt.context = context //