From 6b3383ba727dc75102816168e3cdad467daf7b73 Mon Sep 17 00:00:00 2001 From: Dmitry Zhuravlev Date: Fri, 31 Mar 2017 16:47:47 +0300 Subject: [PATCH 1/3] * download sources property propagation. relates to #372 --- .../beust/kobalt/app/remote/GetDependencyGraphHandler.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/app/remote/GetDependencyGraphHandler.kt b/src/main/kotlin/com/beust/kobalt/app/remote/GetDependencyGraphHandler.kt index d6a47912..80c656e1 100644 --- a/src/main/kotlin/com/beust/kobalt/app/remote/GetDependencyGraphHandler.kt +++ b/src/main/kotlin/com/beust/kobalt/app/remote/GetDependencyGraphHandler.kt @@ -6,7 +6,6 @@ import com.beust.kobalt.app.ProjectFinder import com.beust.kobalt.internal.build.BuildSources import com.beust.kobalt.internal.eventbus.ArtifactDownloadedEvent import com.beust.kobalt.maven.aether.Exceptions -import com.beust.kobalt.misc.KFiles import com.google.common.eventbus.EventBus import com.google.common.eventbus.Subscribe import com.google.gson.Gson @@ -14,7 +13,6 @@ import org.eclipse.jetty.websocket.api.RemoteEndpoint import org.eclipse.jetty.websocket.api.Session import org.eclipse.jetty.websocket.api.WebSocketListener import java.io.File -import java.nio.file.Paths /** * Manage the websocket endpoint "/v1/getDependencyGraph". @@ -27,6 +25,7 @@ class GetDependencyGraphHandler : WebSocketListener { val PARAMETER_PROJECT_ROOT = "projectRoot" val PARAMETER_BUILD_FILE = "buildFile" val PARAMETER_PROFILES = "profiles" + val PARAMETER_DOWNLOAD_SOURCES = "downloadSources" var session: Session? = null @@ -46,6 +45,7 @@ class GetDependencyGraphHandler : WebSocketListener { } private fun findProfiles(map: Map>) = map[PARAMETER_PROFILES]?.getOrNull(0) + private fun findDownloadSources(map: Map>) = map[PARAMETER_DOWNLOAD_SOURCES]?.getOrNull(0)?.toBoolean() ?: false private fun findBuildFile(map: Map>) : BuildSources? { val projectRoot = map[PARAMETER_PROJECT_ROOT] @@ -65,6 +65,7 @@ class GetDependencyGraphHandler : WebSocketListener { session = s val buildSources = findBuildFile(s.upgradeRequest.parameterMap) val profiles = findProfiles(s.upgradeRequest.parameterMap) + val downloadSources = findDownloadSources(s.upgradeRequest.parameterMap) fun getInstance(cls: Class) : T = Kobalt.INJECTOR.getInstance(cls) @@ -89,6 +90,7 @@ class GetDependencyGraphHandler : WebSocketListener { val dependencyData = getInstance(RemoteDependencyData::class.java) val args = getInstance(Args::class.java) args.profiles = profiles + args.downloadSources = downloadSources val allProjects = projectFinder.initForBuildFile(buildSources, args) From b9a3a4f40b426544bddbee4715e9696a09ec0cab Mon Sep 17 00:00:00 2001 From: Dmitry Zhuravlev Date: Fri, 31 Mar 2017 18:31:20 +0300 Subject: [PATCH 2/3] * fix sources and javadoc resolution. relates to #372 --- .../maven/aether/KobaltMavenResolver.kt | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) 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 50731443..2b79c432 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 @@ -38,6 +38,14 @@ class KobaltMavenResolver @Inject constructor(val settings: KobaltSettings, fun resolve(id: String, scope: Scope? = null, filter: DependencyFilter? = null): DependencyResult { val dependencyRequest = DependencyRequest(createCollectRequest(id, scope), filter) val result = system.resolveDependencies(session, dependencyRequest) + if (args.downloadSources) { + listOf("sources", "javadoc").forEach { + val artifact = DefaultArtifact(id) + val sourceArtifact = DefaultArtifact(artifact.groupId, artifact.artifactId, it, artifact.extension, + artifact.version) + system.resolveDependencies(session, DependencyRequest(createCollectRequest(sourceArtifact, scope), filter)) + } + } // GraphUtil.displayGraph(listOf(result.root), { it -> it.children }, // { it: DependencyNode, indent: String -> println(indent + it.toString()) }) @@ -104,17 +112,16 @@ class KobaltMavenResolver @Inject constructor(val settings: KobaltSettings, private fun createCollectRequest(id: String, scope: Scope? = null) = CollectRequest().apply { val allIds = arrayListOf(MavenId.toMavenId(id)) - if (args.downloadSources) { - listOf("sources", "javadoc").forEach { - val artifact = DefaultArtifact(id) - val sourceArtifact = DefaultArtifact(artifact.groupId, artifact.artifactId, it, artifact.extension, - artifact.version) - allIds.add(sourceArtifact.toString()) - } - } + dependencies = allIds.map { Dependency(DefaultArtifact(it), scope?.scope) } root = Dependency(DefaultArtifact(MavenId.toMavenId(id)), scope?.scope) repositories = kobaltRepositories } + + private fun createCollectRequest( artifact: DefaultArtifact, scope: Scope? = null) = CollectRequest().apply { + dependencies = listOf(Dependency(artifact, scope?.scope)) + root = Dependency(artifact, scope?.scope) + repositories = kobaltRepositories + } } From c54d4c5b2b646fac56a183a9c952f91c0a566f22 Mon Sep 17 00:00:00 2001 From: Dmitry Zhuravlev Date: Mon, 3 Apr 2017 12:50:02 +0300 Subject: [PATCH 3/3] * fix sources and javadoc resolution. relates to #372 --- .../kobalt/maven/aether/AetherDependency.kt | 35 +++++++++++++++++-- .../maven/aether/KobaltMavenResolver.kt | 17 +-------- 2 files changed, 34 insertions(+), 18 deletions(-) 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 8fc960dc..cc9631da 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 @@ -1,5 +1,6 @@ package com.beust.kobalt.maven.aether +import com.beust.kobalt.Args import com.beust.kobalt.api.Dependencies import com.beust.kobalt.api.IClasspathDependency import com.beust.kobalt.api.Kobalt @@ -7,10 +8,12 @@ import com.beust.kobalt.maven.CompletedFuture import com.beust.kobalt.misc.StringVersion import com.beust.kobalt.misc.warn import org.eclipse.aether.artifact.Artifact +import org.eclipse.aether.artifact.DefaultArtifact +import org.eclipse.aether.resolution.DependencyResolutionException import java.io.File import java.util.concurrent.Future -class AetherDependency(val artifact: Artifact, override val optional: Boolean = false) +class AetherDependency(val artifact: Artifact, override val optional: Boolean = false, val args: Args? = null) : IClasspathDependency, Comparable { val aether: KobaltMavenResolver get() = Kobalt.INJECTOR.getInstance(KobaltMavenResolver::class.java) @@ -25,12 +28,37 @@ class AetherDependency(val artifact: Artifact, override val optional: Boolean = override val jarFile: Future get() = if (artifact.file != null) { + resolveSourcesIfNeeded() CompletedFuture(artifact.file) } else { - val td = aether.resolve(artifact, null) + resolveSourcesIfNeeded() + val td = aether.resolve(artifact) CompletedFuture(td.root.artifact.file) } + private fun resolveSourcesIfNeeded() { + if (args?.downloadSources ?: false) { + artifact.toSourcesArtifact().let { sourcesArtifact -> + if (sourcesArtifact.file == null) { + try { + aether.resolve(sourcesArtifact) + } catch(e: DependencyResolutionException) { + //do nothing + } + } + } + artifact.toJavaDocArtifact().let { javadocArtifact -> + if (javadocArtifact.file == null) { + try { + aether.resolve(javadocArtifact) + } catch(e: DependencyResolutionException) { + //do nothing + } + } + } + } + } + override fun toMavenDependencies(scope: String?) : org.apache.maven.model.Dependency { val passedScope = scope val op = this.optional @@ -69,4 +97,7 @@ class AetherDependency(val artifact: Artifact, override val optional: Boolean = override fun equals(other: Any?) = if (other is AetherDependency) other.id == id else false override fun toString() = id + + fun Artifact.toSourcesArtifact() = DefaultArtifact(groupId, artifactId, "sources", extension, version) + fun Artifact.toJavaDocArtifact() = DefaultArtifact(groupId, artifactId, "javadoc", extension, version) } 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 293ab8cf..3de4f868 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 @@ -40,15 +40,6 @@ class KobaltMavenResolver @Inject constructor(val settings: KobaltSettings, repos: List = emptyList()): DependencyResult { val dependencyRequest = DependencyRequest(createCollectRequest(id, scope, repos), filter) val result = system.resolveDependencies(session, dependencyRequest) - if (args.downloadSources) { - listOf("sources", "javadoc").forEach { - val artifact = DefaultArtifact(id) - val sourceArtifact = DefaultArtifact(artifact.groupId, artifact.artifactId, it, artifact.extension, - artifact.version) - system.resolveDependencies(session, DependencyRequest(createCollectRequest(sourceArtifact, scope), filter)) - } - } - // GraphUtil.displayGraph(listOf(result.root), { it -> it.children }, // { it: DependencyNode, indent: String -> println(indent + it.toString()) }) return result @@ -95,7 +86,7 @@ class KobaltMavenResolver @Inject constructor(val settings: KobaltSettings, /** * Create an IClasspathDependency from a Kobalt id. */ - fun create(id: String, optional: Boolean) = AetherDependency(DefaultArtifact(id), optional) + fun create(id: String, optional: Boolean) = AetherDependency(DefaultArtifact(id), optional, args) private val system = Booter.newRepositorySystem() private val session = Booter.newRepositorySystemSession(system, localRepo.localRepo, settings, eventBus) @@ -122,10 +113,4 @@ class KobaltMavenResolver @Inject constructor(val settings: KobaltSettings, root = Dependency(DefaultArtifact(MavenId.toMavenId(id)), scope?.scope) repositories = kobaltRepositories + repos.map { createRepo(it) } } - - private fun createCollectRequest( artifact: DefaultArtifact, scope: Scope? = null) = CollectRequest().apply { - dependencies = listOf(Dependency(artifact, scope?.scope)) - root = Dependency(artifact, scope?.scope) - repositories = kobaltRepositories - } }