diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/JarGenerator.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/JarGenerator.kt index c3399a68..8a2f5733 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/JarGenerator.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/JarGenerator.kt @@ -102,7 +102,8 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) context.variant.buildType.compileRuntimeDependencies + context.variant.productFlavor.compileDependencies + context.variant.productFlavor.compileRuntimeDependencies - val transitiveDependencies = dependencyManager.calculateDependencies(project, context, allDependencies) + val transitiveDependencies = dependencyManager.calculateDependencies(project, context, false, + allDependencies) transitiveDependencies.map { it.jarFile.get() }.forEach { file : File -> diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/IDependencyManager.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/IDependencyManager.kt index 9e47c0dc..3b514cf3 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/IDependencyManager.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/IDependencyManager.kt @@ -33,6 +33,6 @@ interface IDependencyManager { * @return the classpath for this project, including the IClasspathContributors. * allDependencies is typically either compileDependencies or testDependencies */ - fun calculateDependencies(project: Project?, context: KobaltContext, + fun calculateDependencies(project: Project?, context: KobaltContext, isTest: Boolean = false, vararg allDependencies: List): List } \ No newline at end of file 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 37d0a126..d12bfdfa 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 @@ -84,11 +84,11 @@ class DependencyManager @Inject constructor(val executors: KobaltExecutors, val * @return the classpath for this project, including the IClasspathContributors. * allDependencies is typically either compileDependencies or testDependencies */ - override fun calculateDependencies(project: Project?, context: KobaltContext, + override fun calculateDependencies(project: Project?, context: KobaltContext, isTest: Boolean, vararg allDependencies: List): List { val result = arrayListOf() allDependencies.forEach { dependencies -> - result.addAll(transitiveClosure(dependencies, project?.name)) + result.addAll(transitiveClosure(dependencies, isTest, project?.name)) } result.addAll(runClasspathContributors(project, context)) result.addAll(dependentProjectDependencies(project, context)) @@ -113,13 +113,13 @@ class DependencyManager @Inject constructor(val executors: KobaltExecutors, val * Return the transitive closure of the dependencies *without* running the classpath contributors. * TODO: This should be private, everyone should be calling calculateDependencies(). */ - fun transitiveClosure(dependencies : List, requiredBy: String? = null): - List { + fun transitiveClosure(dependencies : List, isTest: Boolean = false, + requiredBy: String? = null): List { val result = arrayListOf() dependencies.forEach { result.add(it) if (it.isMaven) { - val resolved = aether.resolveAll(it.id).map { it.toString() } + val resolved = aether.resolveAll(it.id, isTest).map { it.toString() } result.addAll(resolved.map { create(it) }) } } @@ -165,7 +165,7 @@ class DependencyManager @Inject constructor(val executors: KobaltExecutors, val result.add(FileDependency(KFiles.joinDir(p.directory, p.classesDir(context)))) } } - val otherDependencies = calculateDependencies(p, context, p.compileDependencies) + val otherDependencies = calculateDependencies(p, context, false, p.compileDependencies) result.addAll(otherDependencies) } @@ -189,7 +189,7 @@ class DependencyManager @Inject constructor(val executors: KobaltExecutors, val deps.add(testProvidedDependencies) } deps.filter { it.any() }.forEach { - transitive.addAll(calculateDependencies(project, context, it)) + transitive.addAll(calculateDependencies(project, context, isTest, it)) } } } 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 192f1908..51999be7 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 @@ -3,7 +3,6 @@ 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.homeDir import com.beust.kobalt.internal.KobaltSettings import com.beust.kobalt.internal.KobaltSettingsXml import com.beust.kobalt.internal.getProxy @@ -11,7 +10,6 @@ import com.beust.kobalt.maven.CompletedFuture import com.beust.kobalt.maven.LocalDep import com.beust.kobalt.maven.LocalRepo import com.beust.kobalt.maven.MavenId -import com.beust.kobalt.misc.KobaltLogger import com.beust.kobalt.misc.Versions import com.beust.kobalt.misc.log import com.beust.kobalt.misc.warn @@ -52,14 +50,14 @@ class KobaltAether @Inject constructor (val settings: KobaltSettings, val aether DependencyResult(AetherDependency(it.artifact), it.repository.toString()) } - fun resolveAll(id: String): List { - val results = aether.resolve(DefaultArtifact(id)) + fun resolveAll(id: String, isTest: Boolean): List { + val results = aether.resolve(DefaultArtifact(id), isTest) return results.map { it.artifact.toString() } } - fun resolve(id: String): DependencyResult { + fun resolve(id: String, isTest: Boolean = false): DependencyResult { log(ConsoleRepositoryListener.LOG_LEVEL, "Resolving $id") - val results = aether.resolve(DefaultArtifact(MavenId.toKobaltId(id))) + val results = aether.resolve(DefaultArtifact(MavenId.toKobaltId(id)), isTest) if (results.size > 0) { return DependencyResult(AetherDependency(results[0].artifact), results[0].repository.toString()) } else { @@ -85,7 +83,13 @@ class Aether(val localRepo: File, val settings: KobaltSettings, val eventBus: Ev private val session = Booter.newRepositorySystemSession(system, localRepo, settings, eventBus) private val classpathFilter = AndDependencyFilter( ExcludeOptionalDependencyFilter(), - DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE)) + DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE), + DependencyFilterUtils.classpathFilter(JavaScopes.TEST)) + + private val testClasspathFilter = AndDependencyFilter( + ExcludeOptionalDependencyFilter(), + DependencyFilterUtils.classpathFilter(JavaScopes.TEST)) + private val kobaltRepositories : List get() = Kobalt.repos.map { RemoteRepository.Builder("maven", "default", it.url) @@ -98,9 +102,9 @@ class Aether(val localRepo: File, val settings: KobaltSettings, val eventBus: Ev } } - private fun collectRequest(artifact: Artifact) : CollectRequest { + private fun collectRequest(artifact: Artifact, isTest: Boolean) : CollectRequest { with(CollectRequest()) { - root = Dependency(artifact, JavaScopes.COMPILE) + root = Dependency(artifact, if (isTest) JavaScopes.TEST else JavaScopes.COMPILE) repositories = kobaltRepositories return this @@ -154,7 +158,7 @@ class Aether(val localRepo: File, val settings: KobaltSettings, val eventBus: Ev } - fun resolve(artifact: Artifact): List { + fun resolve(artifact: Artifact, isTest: Boolean = false): 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 @@ -165,7 +169,8 @@ class Aether(val localRepo: File, val settings: KobaltSettings, val eventBus: Ev } try { - val dependencyRequest = DependencyRequest(collectRequest(artifact), classpathFilter) + val dependencyRequest = DependencyRequest(collectRequest(artifact, isTest), + if (isTest) testClasspathFilter else classpathFilter) val result = system.resolveDependencies(session, dependencyRequest).artifactResults return result } catch(ex: ArtifactNotFoundException) { @@ -177,8 +182,8 @@ class Aether(val localRepo: File, val settings: KobaltSettings, val eventBus: Ev fun transitiveDependencies(artifact: Artifact) = directDependencies(artifact) - fun directDependencies(artifact: Artifact): CollectResult? - = system.collectDependencies(session, collectRequest(artifact)) + fun directDependencies(artifact: Artifact, isTest: Boolean = false): CollectResult? + = system.collectDependencies(session, collectRequest(artifact, isTest)) } class AetherDependency(val artifact: Artifact): IClasspathDependency, Comparable { @@ -259,13 +264,29 @@ class AetherDependency(val artifact: Artifact): IClasspathDependency, Comparable } fun main(argv: Array) { - KobaltLogger.LOG_LEVEL = 1 - val id = "org.testng:testng:6.9.11" - val aether = KobaltAether(KobaltSettings(KobaltSettingsXml()), Aether(File(homeDir(".aether")), - KobaltSettings(KobaltSettingsXml()), EventBus())) - val r = aether.resolve(id) - val r2 = aether.resolve(id) - val d = org.eclipse.aether.artifact.DefaultArtifact("org.testng:testng:6.9") + val request = CollectRequest().apply { + root = Dependency(DefaultArtifact("org.testng:testng:6.9.11"), JavaScopes.COMPILE) + repositories = listOf( + RemoteRepository.Builder("Maven", "default", "http://repo1.maven.org/maven2/").build(), + RemoteRepository.Builder("JCenter", "default", "https://jcenter.bintray.com").build()) + } + val dependencyRequest = DependencyRequest().apply { + collectRequest = request + } + val system = Booter.newRepositorySystem() + val session = Booter.newRepositorySystemSession(system, File("/tmp"), KobaltSettings(KobaltSettingsXml()), + EventBus()) +// val session = MavenRepositorySystemUtils.newSession(KobaltSettings(KobaltSettingsXml())) + val result = system.resolveDependencies(session, dependencyRequest).artifactResults + println("RESULT: " + result) - println("Artifact: " + d) +// KobaltLogger.LOG_LEVEL = 1 +// val id = "org.testng:testng:6.9.11" +// val aether = KobaltAether(KobaltSettings(KobaltSettingsXml()), Aether(File(homeDir(".aether")), +// KobaltSettings(KobaltSettingsXml()), EventBus())) +// val r = aether.resolve(id) +// val r2 = aether.resolve(id) +// val d = org.eclipse.aether.artifact.DefaultArtifact("org.testng:testng:6.9") +// +// println("Artifact: " + d) } diff --git a/src/main/kotlin/com/beust/kobalt/app/remote/DependencyData.kt b/src/main/kotlin/com/beust/kobalt/app/remote/DependencyData.kt index 67e1e438..ce51fe51 100644 --- a/src/main/kotlin/com/beust/kobalt/app/remote/DependencyData.kt +++ b/src/main/kotlin/com/beust/kobalt/app/remote/DependencyData.kt @@ -33,7 +33,8 @@ class DependencyData @Inject constructor(val executors: KobaltExecutors, val dep return DependencyData(d.id, scope, dep.jarFile.get().absolutePath) } - fun allDeps(l: List, name: String) = dependencyManager.transitiveClosure(l, name) + fun allDeps(l: List, name: String) = dependencyManager.transitiveClosure(l, + requiredBy = name) val buildFile = BuildFile(Paths.get(buildFilePath), "GetDependenciesCommand") val buildFileCompiler = buildFileCompilerFactory.create(listOf(buildFile), pluginInfo) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt index d13c4a9a..2d27a849 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt @@ -183,7 +183,7 @@ class KotlinCompiler @Inject constructor( val executor = executors.newExecutor("KotlinCompiler", 10) val compilerVersion = settings.kobaltCompilerVersion val compilerDep = dependencyManager.create("org.jetbrains.kotlin:kotlin-compiler-embeddable:$compilerVersion") - val deps = dependencyManager.transitiveClosure(listOf(compilerDep), project?.name ?: "") + val deps = dependencyManager.transitiveClosure(listOf(compilerDep), requiredBy = project?.name ?: "") // Force a download of the compiler dependencies deps.forEach { it.jarFile.get() } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt index bccdf918..9d33bb2e 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt @@ -121,7 +121,7 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana val analyzer = Analyzer().apply { jar = aQute.bnd.osgi.Jar(project.projectProperties.get(Archives.JAR_NAME) as String) val dependencies = project.compileDependencies + project.compileRuntimeDependencies - dependencyManager.calculateDependencies(project, context, dependencies).forEach { + dependencyManager.calculateDependencies(project, context, allDependencies = dependencies).forEach { addClasspath(it.jarFile.get()) } setProperty(Analyzer.BUNDLE_VERSION, project.version) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt index a4826813..e574a9e6 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt @@ -39,7 +39,8 @@ class WarGenerator @Inject constructor(val dependencyManager: DependencyManager) // The transitive closure of libraries goes into WEB-INF/libs. // Copy them all in kobaltBuild/war/WEB-INF/libs and create one IncludedFile out of that directory // - val allDependencies = dependencyManager.calculateDependencies(project, context, project.compileDependencies) + val allDependencies = dependencyManager.calculateDependencies(project, context, + allDependencies = project.compileDependencies) val outDir = project.buildDirectory + "/war" val fullDir = outDir + "/" + LIB diff --git a/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt b/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt index a3f73325..c5fa9b31 100644 --- a/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt +++ b/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt @@ -160,7 +160,7 @@ class DownloadTest @Inject constructor( @Test fun variablesShouldBeExpanded() { val dep = dependencyManager.createMaven("org.mapdb:mapdb:3.0.0-M3") - val closure = dependencyManager.transitiveClosure(listOf(dep), "") + val closure = dependencyManager.transitiveClosure(listOf(dep), false, "") val d = closure.filter { it.id.contains("eclipse-collections-api")} Assert.assertEquals(d.size, 1) }