1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-27 00:38:11 -07:00

Transitive dependencies for tests were not correct.

This commit is contained in:
Cedric Beust 2016-07-21 04:02:22 -08:00
parent 4ba5f5bee1
commit 45e40a1397
9 changed files with 59 additions and 35 deletions

View file

@ -102,7 +102,8 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager)
context.variant.buildType.compileRuntimeDependencies + context.variant.buildType.compileRuntimeDependencies +
context.variant.productFlavor.compileDependencies + context.variant.productFlavor.compileDependencies +
context.variant.productFlavor.compileRuntimeDependencies context.variant.productFlavor.compileRuntimeDependencies
val transitiveDependencies = dependencyManager.calculateDependencies(project, context, allDependencies) val transitiveDependencies = dependencyManager.calculateDependencies(project, context, false,
allDependencies)
transitiveDependencies.map { transitiveDependencies.map {
it.jarFile.get() it.jarFile.get()
}.forEach { file : File -> }.forEach { file : File ->

View file

@ -33,6 +33,6 @@ interface IDependencyManager {
* @return the classpath for this project, including the IClasspathContributors. * @return the classpath for this project, including the IClasspathContributors.
* allDependencies is typically either compileDependencies or testDependencies * 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<IClasspathDependency>): List<IClasspathDependency> vararg allDependencies: List<IClasspathDependency>): List<IClasspathDependency>
} }

View file

@ -84,11 +84,11 @@ class DependencyManager @Inject constructor(val executors: KobaltExecutors, val
* @return the classpath for this project, including the IClasspathContributors. * @return the classpath for this project, including the IClasspathContributors.
* allDependencies is typically either compileDependencies or testDependencies * 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<IClasspathDependency>): List<IClasspathDependency> { vararg allDependencies: List<IClasspathDependency>): List<IClasspathDependency> {
val result = arrayListOf<IClasspathDependency>() val result = arrayListOf<IClasspathDependency>()
allDependencies.forEach { dependencies -> allDependencies.forEach { dependencies ->
result.addAll(transitiveClosure(dependencies, project?.name)) result.addAll(transitiveClosure(dependencies, isTest, project?.name))
} }
result.addAll(runClasspathContributors(project, context)) result.addAll(runClasspathContributors(project, context))
result.addAll(dependentProjectDependencies(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. * Return the transitive closure of the dependencies *without* running the classpath contributors.
* TODO: This should be private, everyone should be calling calculateDependencies(). * TODO: This should be private, everyone should be calling calculateDependencies().
*/ */
fun transitiveClosure(dependencies : List<IClasspathDependency>, requiredBy: String? = null): fun transitiveClosure(dependencies : List<IClasspathDependency>, isTest: Boolean = false,
List<IClasspathDependency> { requiredBy: String? = null): List<IClasspathDependency> {
val result = arrayListOf<IClasspathDependency>() val result = arrayListOf<IClasspathDependency>()
dependencies.forEach { dependencies.forEach {
result.add(it) result.add(it)
if (it.isMaven) { 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) }) 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)))) 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) result.addAll(otherDependencies)
} }
@ -189,7 +189,7 @@ class DependencyManager @Inject constructor(val executors: KobaltExecutors, val
deps.add(testProvidedDependencies) deps.add(testProvidedDependencies)
} }
deps.filter { it.any() }.forEach { deps.filter { it.any() }.forEach {
transitive.addAll(calculateDependencies(project, context, it)) transitive.addAll(calculateDependencies(project, context, isTest, it))
} }
} }
} }

View file

@ -3,7 +3,6 @@ package com.beust.kobalt.maven.aether
import com.beust.kobalt.KobaltException import com.beust.kobalt.KobaltException
import com.beust.kobalt.api.IClasspathDependency import com.beust.kobalt.api.IClasspathDependency
import com.beust.kobalt.api.Kobalt import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.homeDir
import com.beust.kobalt.internal.KobaltSettings import com.beust.kobalt.internal.KobaltSettings
import com.beust.kobalt.internal.KobaltSettingsXml import com.beust.kobalt.internal.KobaltSettingsXml
import com.beust.kobalt.internal.getProxy 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.LocalDep
import com.beust.kobalt.maven.LocalRepo import com.beust.kobalt.maven.LocalRepo
import com.beust.kobalt.maven.MavenId import com.beust.kobalt.maven.MavenId
import com.beust.kobalt.misc.KobaltLogger
import com.beust.kobalt.misc.Versions import com.beust.kobalt.misc.Versions
import com.beust.kobalt.misc.log import com.beust.kobalt.misc.log
import com.beust.kobalt.misc.warn 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()) DependencyResult(AetherDependency(it.artifact), it.repository.toString())
} }
fun resolveAll(id: String): List<String> { fun resolveAll(id: String, isTest: Boolean): List<String> {
val results = aether.resolve(DefaultArtifact(id)) val results = aether.resolve(DefaultArtifact(id), isTest)
return results.map { it.artifact.toString() } 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") 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) { if (results.size > 0) {
return DependencyResult(AetherDependency(results[0].artifact), results[0].repository.toString()) return DependencyResult(AetherDependency(results[0].artifact), results[0].repository.toString())
} else { } 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 session = Booter.newRepositorySystemSession(system, localRepo, settings, eventBus)
private val classpathFilter = AndDependencyFilter( private val classpathFilter = AndDependencyFilter(
ExcludeOptionalDependencyFilter(), 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<RemoteRepository> private val kobaltRepositories : List<RemoteRepository>
get() = Kobalt.repos.map { get() = Kobalt.repos.map {
RemoteRepository.Builder("maven", "default", it.url) 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()) { with(CollectRequest()) {
root = Dependency(artifact, JavaScopes.COMPILE) root = Dependency(artifact, if (isTest) JavaScopes.TEST else JavaScopes.COMPILE)
repositories = kobaltRepositories repositories = kobaltRepositories
return this return this
@ -154,7 +158,7 @@ class Aether(val localRepo: File, val settings: KobaltSettings, val eventBus: Ev
} }
fun resolve(artifact: Artifact): List<ArtifactResult> { fun resolve(artifact: Artifact, isTest: Boolean = false): List<ArtifactResult> {
fun manageException(ex: Exception, artifact: Artifact) : List<ArtifactResult> { fun manageException(ex: Exception, artifact: Artifact) : List<ArtifactResult> {
if (artifact.extension == "pom") { if (artifact.extension == "pom") {
// Only display a warning for .pom files. Not resolving a .jar or other artifact // 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 { 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 val result = system.resolveDependencies(session, dependencyRequest).artifactResults
return result return result
} catch(ex: ArtifactNotFoundException) { } 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 transitiveDependencies(artifact: Artifact) = directDependencies(artifact)
fun directDependencies(artifact: Artifact): CollectResult? fun directDependencies(artifact: Artifact, isTest: Boolean = false): CollectResult?
= system.collectDependencies(session, collectRequest(artifact)) = system.collectDependencies(session, collectRequest(artifact, isTest))
} }
class AetherDependency(val artifact: Artifact): IClasspathDependency, Comparable<AetherDependency> { class AetherDependency(val artifact: Artifact): IClasspathDependency, Comparable<AetherDependency> {
@ -259,13 +264,29 @@ class AetherDependency(val artifact: Artifact): IClasspathDependency, Comparable
} }
fun main(argv: Array<String>) { fun main(argv: Array<String>) {
KobaltLogger.LOG_LEVEL = 1 val request = CollectRequest().apply {
val id = "org.testng:testng:6.9.11" root = Dependency(DefaultArtifact("org.testng:testng:6.9.11"), JavaScopes.COMPILE)
val aether = KobaltAether(KobaltSettings(KobaltSettingsXml()), Aether(File(homeDir(".aether")), repositories = listOf(
KobaltSettings(KobaltSettingsXml()), EventBus())) RemoteRepository.Builder("Maven", "default", "http://repo1.maven.org/maven2/").build(),
val r = aether.resolve(id) RemoteRepository.Builder("JCenter", "default", "https://jcenter.bintray.com").build())
val r2 = aether.resolve(id) }
val d = org.eclipse.aether.artifact.DefaultArtifact("org.testng:testng:6.9") val dependencyRequest = DependencyRequest().apply {
collectRequest = request
println("Artifact: " + d) }
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)
// 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)
} }

View file

@ -33,7 +33,8 @@ class DependencyData @Inject constructor(val executors: KobaltExecutors, val dep
return DependencyData(d.id, scope, dep.jarFile.get().absolutePath) return DependencyData(d.id, scope, dep.jarFile.get().absolutePath)
} }
fun allDeps(l: List<IClasspathDependency>, name: String) = dependencyManager.transitiveClosure(l, name) fun allDeps(l: List<IClasspathDependency>, name: String) = dependencyManager.transitiveClosure(l,
requiredBy = name)
val buildFile = BuildFile(Paths.get(buildFilePath), "GetDependenciesCommand") val buildFile = BuildFile(Paths.get(buildFilePath), "GetDependenciesCommand")
val buildFileCompiler = buildFileCompilerFactory.create(listOf(buildFile), pluginInfo) val buildFileCompiler = buildFileCompilerFactory.create(listOf(buildFile), pluginInfo)

View file

@ -183,7 +183,7 @@ class KotlinCompiler @Inject constructor(
val executor = executors.newExecutor("KotlinCompiler", 10) val executor = executors.newExecutor("KotlinCompiler", 10)
val compilerVersion = settings.kobaltCompilerVersion val compilerVersion = settings.kobaltCompilerVersion
val compilerDep = dependencyManager.create("org.jetbrains.kotlin:kotlin-compiler-embeddable:$compilerVersion") 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 // Force a download of the compiler dependencies
deps.forEach { it.jarFile.get() } deps.forEach { it.jarFile.get() }

View file

@ -121,7 +121,7 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
val analyzer = Analyzer().apply { val analyzer = Analyzer().apply {
jar = aQute.bnd.osgi.Jar(project.projectProperties.get(Archives.JAR_NAME) as String) jar = aQute.bnd.osgi.Jar(project.projectProperties.get(Archives.JAR_NAME) as String)
val dependencies = project.compileDependencies + project.compileRuntimeDependencies val dependencies = project.compileDependencies + project.compileRuntimeDependencies
dependencyManager.calculateDependencies(project, context, dependencies).forEach { dependencyManager.calculateDependencies(project, context, allDependencies = dependencies).forEach {
addClasspath(it.jarFile.get()) addClasspath(it.jarFile.get())
} }
setProperty(Analyzer.BUNDLE_VERSION, project.version) setProperty(Analyzer.BUNDLE_VERSION, project.version)

View file

@ -39,7 +39,8 @@ class WarGenerator @Inject constructor(val dependencyManager: DependencyManager)
// The transitive closure of libraries goes into WEB-INF/libs. // 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 // 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 outDir = project.buildDirectory + "/war"
val fullDir = outDir + "/" + LIB val fullDir = outDir + "/" + LIB

View file

@ -160,7 +160,7 @@ class DownloadTest @Inject constructor(
@Test @Test
fun variablesShouldBeExpanded() { fun variablesShouldBeExpanded() {
val dep = dependencyManager.createMaven("org.mapdb:mapdb:3.0.0-M3") val dep = dependencyManager.createMaven("org.mapdb:mapdb:3.0.0-M3")
val closure = dependencyManager.transitiveClosure(listOf(dep), "<testProject>") val closure = dependencyManager.transitiveClosure(listOf(dep), false, "<testProject>")
val d = closure.filter { it.id.contains("eclipse-collections-api")} val d = closure.filter { it.id.contains("eclipse-collections-api")}
Assert.assertEquals(d.size, 1) Assert.assertEquals(d.size, 1)
} }