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

Successful compilation.

This commit is contained in:
Cedric Beust 2016-03-24 13:57:45 +04:00
parent 2ef9db700c
commit 22ccb868c8
5 changed files with 172 additions and 111 deletions

View file

@ -1,14 +1,12 @@
package com.beust.kobalt.maven
import com.beust.kobalt.KobaltException
import com.beust.kobalt.api.IClasspathDependency
import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.maven.aether.Aether
import com.beust.kobalt.maven.aether.KobaltAether
import com.beust.kobalt.maven.dependency.FileDependency
import com.beust.kobalt.maven.dependency.MavenDependency
import com.beust.kobalt.misc.DependencyExecutor
import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.warn
import com.google.inject.Key
import java.util.concurrent.ExecutorService
import javax.inject.Inject
@ -16,7 +14,7 @@ import javax.inject.Inject
public class DepFactory @Inject constructor(val localRepo: LocalRepo,
val remoteRepo: RepoFinder,
val executors: KobaltExecutors,
val aether: Aether,
val aether: KobaltAether,
val mavenDependencyFactory: MavenDependency.IFactory) {
companion object {
@ -34,35 +32,38 @@ public class DepFactory @Inject constructor(val localRepo: LocalRepo,
if (id.startsWith(FileDependency.PREFIX_FILE)) {
return FileDependency(id.substring(FileDependency.PREFIX_FILE.length))
} else {
val mavenId = MavenId.create(id)
var tentativeVersion = mavenId.version
var packaging = mavenId.packaging
var repoResult: RepoFinder.RepoResult?
val version =
if (tentativeVersion != null && ! MavenId.isRangedVersion(tentativeVersion)) tentativeVersion
else {
var localVersion: String? = tentativeVersion
if (localFirst) localVersion = localRepo.findLocalVersion(mavenId.groupId, mavenId.artifactId,
mavenId.packaging)
if (localFirst && localVersion != null) {
localVersion
} else {
if (! localFirst && showNetworkWarning) {
warn("The id \"$id\" doesn't contain a version, which will cause a network call")
}
repoResult = remoteRepo.findCorrectRepo(id)
if (!repoResult.found) {
throw KobaltException("Couldn't resolve $id")
} else {
repoResult.version?.version
}
}
}
val resultMavenId = MavenId.create(mavenId.groupId, mavenId.artifactId, packaging, version)
return mavenDependencyFactory.create(resultMavenId, executor, downloadSources, downloadJavadocs)
val result = aether.create(id)
return result
// return deps.root
// val mavenId = MavenId.create(id)
// var tentativeVersion = mavenId.version
// var packaging = mavenId.packaging
// var repoResult: RepoFinder.RepoResult?
//
// val version =
// if (tentativeVersion != null && ! MavenId.isRangedVersion(tentativeVersion)) tentativeVersion
// else {
// var localVersion: String? = tentativeVersion
// if (localFirst) localVersion = localRepo.findLocalVersion(mavenId.groupId, mavenId.artifactId,
// mavenId.packaging)
// if (localFirst && localVersion != null) {
// localVersion
// } else {
// if (! localFirst && showNetworkWarning) {
// warn("The id \"$id\" doesn't contain a version, which will cause a network call")
// }
// repoResult = remoteRepo.findCorrectRepo(id)
// if (!repoResult.found) {
// throw KobaltException("Couldn't resolve $id")
// } else {
// repoResult.version?.version
// }
// }
// }
//
//
// val resultMavenId = MavenId.create(mavenId.groupId, mavenId.artifactId, packaging, version)
// return mavenDependencyFactory.create(resultMavenId, executor, downloadSources, downloadJavadocs)
}
}
}

View file

@ -1,102 +1,158 @@
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.maven.CompletedFuture
import com.beust.kobalt.misc.KobaltLogger
import com.beust.kobalt.misc.Versions
import com.beust.kobalt.misc.log
import com.beust.kobalt.misc.warn
import org.eclipse.aether.artifact.Artifact
import org.eclipse.aether.artifact.DefaultArtifact
import org.eclipse.aether.collection.CollectRequest
import org.eclipse.aether.collection.CollectResult
import org.eclipse.aether.graph.Dependency
import org.eclipse.aether.graph.DependencyFilter
import org.eclipse.aether.graph.DependencyNode
import org.eclipse.aether.resolution.ArtifactResult
import org.eclipse.aether.resolution.DependencyRequest
import org.eclipse.aether.resolution.DependencyResolutionException
import org.eclipse.aether.util.artifact.JavaScopes
import org.eclipse.aether.util.filter.AndDependencyFilter
import org.eclipse.aether.util.filter.DependencyFilterUtils
import java.io.File
import java.util.concurrent.Future
class Aether(val localRepo: File = File(homeDir(".kobalt/repository"))) {
fun transitiveDependencies(id: String): List<ArtifactResult>? {
println("------------------------------------------------------------")
val TEST_DIR = ".aether/repository"
val system = Booter.newRepositorySystem()
val session = Booter.newRepositorySystemSession(system, localRepo)
val artifact = DefaultArtifact(id)
val classpathFlter = DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE)
val collectRequest = CollectRequest()
collectRequest.root = Dependency(artifact, JavaScopes.COMPILE)
collectRequest.repositories = Booter.newRepositories(Kobalt.repos.map { it.url })
val dependencyRequest = DependencyRequest(collectRequest, classpathFlter)
val result = system.resolveDependencies(session, dependencyRequest).artifactResults
if (KobaltLogger.LOG_LEVEL > 1) {
for (artifactResult in result) {
log(2, artifactResult.artifact.toString() + " resolved to " + artifactResult.artifact.file)
}
}
class KobaltAether(val localRepo: File = File(homeDir(TEST_DIR))) {
fun create(id: String): IClasspathDependency {
val aether = Aether(localRepo)
val cr = aether.transitiveDependencies(DefaultArtifact(id))
return if (cr != null) AetherDependency(cr.root.artifact)
else throw KobaltException("Couldn't resolve $id")
}
}
class ExcludeOptionalDependencyFilter: DependencyFilter {
override fun accept(node: DependencyNode?, p1: MutableList<DependencyNode>?): 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
}
}
fun directDependencies(id: String): CollectResult? {
println("------------------------------------------------------------")
class Aether(val localRepo: File = File(homeDir(TEST_DIR))) {
private val system = Booter.newRepositorySystem()
private val session = Booter.newRepositorySystemSession(system, localRepo)
private val classpathFilter = AndDependencyFilter(
ExcludeOptionalDependencyFilter(),
DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE))
val system = Booter.newRepositorySystem()
private fun collectRequest(artifact: Artifact) : CollectRequest {
with(CollectRequest()) {
root = Dependency(artifact, JavaScopes.COMPILE)
repositories = Booter.newRepositories(Kobalt.repos.map { it.url })
val session = Booter.newRepositorySystemSession(system, localRepo)
return this
}
}
val artifact = DefaultArtifact(id)
fun resolve(artifact: Artifact): List<ArtifactResult>? {
try {
val dependencyRequest = DependencyRequest(collectRequest(artifact), classpathFilter)
val classpathFilter = DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE)
val result = system.resolveDependencies(session, dependencyRequest).artifactResults
return result
} catch(ex: DependencyResolutionException) {
warn("Couldn't resolve $artifact")
return emptyList()
}
}
val collectRequest = CollectRequest()
collectRequest.root = Dependency(artifact, JavaScopes.COMPILE)
collectRequest.repositories = Booter.newRepositories(Kobalt.repos.map { it.url })
fun transitiveDependencies(artifact: Artifact) = directDependencies(artifact)
val result = system.collectDependencies(session, collectRequest)
fun directDependencies(artifact: Artifact): CollectResult? {
val result = system.collectDependencies(session, collectRequest(artifact))
val root = result.root
val icp = AetherDependency(root)
println("Dep: " + root)
return result
}
}
class AetherDependency(val root: DependencyNode): IClasspathDependency {
override val id: String = toId(root.artifact)
class AetherDependency(val artifact: Artifact): IClasspathDependency, Comparable<AetherDependency> {
constructor(node: DependencyNode) : this(node.artifact) {}
private fun toId(a: Artifact) = with(a) {
groupId + ":" + artifactId + ":" + version
}
override val id: String = toId(artifact)
override val jarFile: Future<File>
get() = CompletedFuture(root.artifact.file)
private fun toId(a: Artifact) = with(a) {
groupId + ":" + artifactId + ":" + version
}
override fun toMavenDependencies() = let { md ->
org.apache.maven.model.Dependency().apply {
root.artifact.let { md ->
groupId = md.groupId
artifactId = md.artifactId
version = md.version
override val jarFile: Future<File>
get() = if (artifact.file != null) {
CompletedFuture(artifact.file)
} else {
val td = Aether().transitiveDependencies(artifact)
if (td?.root?.artifact?.file != null) {
CompletedFuture(td!!.root.artifact.file)
} else {
val resolved = Aether().resolve(artifact)
if (resolved != null && resolved.size > 0) {
CompletedFuture(resolved[0].artifact.file)
} else {
CompletedFuture(File("DONOTEXIST")) // will be filtered out
}
}
}
override fun directDependencies() = root.children.map { AetherDependency(it) }
override val shortId = root.artifact.groupId + ":" + root.artifact.artifactId
override fun toMavenDependencies() = let { md ->
org.apache.maven.model.Dependency().apply {
artifact.let { md ->
groupId = md.groupId
artifactId = md.artifactId
version = md.version
}
}
}
override fun directDependencies() : List<IClasspathDependency> {
val result = arrayListOf<IClasspathDependency>()
val deps = Aether().directDependencies(artifact)
val td = Aether().transitiveDependencies(artifact)
if (deps != null) {
if (! deps.root.dependency.optional) {
deps.root.children.forEach {
if (! it.dependency.isOptional) {
result.add(AetherDependency(it.artifact))
} else {
log(2, "Skipping optional dependency " + deps.root.artifact)
}
}
} else {
log(2, "Skipping optional dependency " + deps.root.artifact)
}
} else {
warn("Couldn't resolve $artifact")
}
return result
}
override val shortId = artifact.groupId + ":" + artifact.artifactId
override fun compareTo(other: AetherDependency): Int {
return Versions.toLongVersion(artifact.version).compareTo(Versions.toLongVersion(
other.artifact.version))
}
override fun toString() = id
}
fun main(argv: Array<String>) {
val dd = Aether().directDependencies("org.testng:testng:6.9.9")
println("DD: " + dd)
val d2 = Aether().transitiveDependencies(DefaultArtifact("com.google.inject:guice-parent:4.0"))
// val dd = Aether().directDependencies("org.testng:testng:6.9.9")
val artifact = d2?.root?.artifact
println("DD: " + d2)
}

View file

@ -1,5 +1,6 @@
package com.beust.kobalt.maven.aether
import com.beust.kobalt.misc.log
import org.eclipse.aether.AbstractRepositoryListener
import org.eclipse.aether.RepositoryEvent
import java.io.PrintStream
@ -16,72 +17,72 @@ class ConsoleRepositoryListener @JvmOverloads constructor(out: PrintStream? = nu
}
override fun artifactDeployed(event: RepositoryEvent?) {
out.println("Deployed " + event!!.artifact + " to " + event.repository)
log(2, "Deployed " + event!!.artifact + " to " + event.repository)
}
override fun artifactDeploying(event: RepositoryEvent?) {
out.println("Deploying " + event!!.artifact + " to " + event.repository)
log(2, "Deploying " + event!!.artifact + " to " + event.repository)
}
override fun artifactDescriptorInvalid(event: RepositoryEvent?) {
out.println("Invalid artifact descriptor for " + event!!.artifact + ": "
log(2, "Invalid artifact descriptor for " + event!!.artifact + ": "
+ event.exception.message)
}
override fun artifactDescriptorMissing(event: RepositoryEvent?) {
out.println("Missing artifact descriptor for " + event!!.artifact)
log(2, "Missing artifact descriptor for " + event!!.artifact)
}
override fun artifactInstalled(event: RepositoryEvent?) {
out.println("Installed " + event!!.artifact + " to " + event.file)
log(2, "Installed " + event!!.artifact + " to " + event.file)
}
override fun artifactInstalling(event: RepositoryEvent?) {
out.println("Installing " + event!!.artifact + " to " + event.file)
log(2, "Installing " + event!!.artifact + " to " + event.file)
}
override fun artifactResolved(event: RepositoryEvent?) {
out.println("Resolved artifact " + event!!.artifact + " from " + event.repository)
log(2, "Resolved artifact " + event!!.artifact + " from " + event.repository)
}
override fun artifactDownloading(event: RepositoryEvent?) {
out.println("Downloading artifact " + event!!.artifact + " from " + event.repository)
log(2, "Downloading artifact " + event!!.artifact + " from " + event.repository)
}
override fun artifactDownloaded(event: RepositoryEvent?) {
out.println("Downloaded artifact " + event!!.artifact + " from " + event.repository)
log(2, "Downloaded artifact " + event!!.artifact + " from " + event.repository)
}
override fun artifactResolving(event: RepositoryEvent?) {
out.println("Resolving artifact " + event!!.artifact)
log(2, "Resolving artifact " + event!!.artifact)
}
override fun metadataDeployed(event: RepositoryEvent?) {
out.println("Deployed " + event!!.metadata + " to " + event.repository)
log(2, "Deployed " + event!!.metadata + " to " + event.repository)
}
override fun metadataDeploying(event: RepositoryEvent?) {
out.println("Deploying " + event!!.metadata + " to " + event.repository)
log(2, "Deploying " + event!!.metadata + " to " + event.repository)
}
override fun metadataInstalled(event: RepositoryEvent?) {
out.println("Installed " + event!!.metadata + " to " + event.file)
log(2, "Installed " + event!!.metadata + " to " + event.file)
}
override fun metadataInstalling(event: RepositoryEvent?) {
out.println("Installing " + event!!.metadata + " to " + event.file)
log(2, "Installing " + event!!.metadata + " to " + event.file)
}
override fun metadataInvalid(event: RepositoryEvent?) {
out.println("Invalid metadata " + event!!.metadata)
log(2, "Invalid metadata " + event!!.metadata)
}
override fun metadataResolved(event: RepositoryEvent?) {
out.println("Resolved metadata " + event!!.metadata + " from " + event.repository)
log(2, "Resolved metadata " + event!!.metadata + " from " + event.repository)
}
override fun metadataResolving(event: RepositoryEvent?) {
out.println("Resolving metadata " + event!!.metadata + " from " + event.repository)
log(2, "Resolving metadata " + event!!.metadata + " from " + event.repository)
}
}

View file

@ -1,5 +1,7 @@
package com.beust.kobalt.maven.aether
import com.beust.kobalt.misc.KobaltLogger
import com.beust.kobalt.misc.log
import org.eclipse.aether.transfer.AbstractTransferListener
import org.eclipse.aether.transfer.MetadataNotFoundException
import org.eclipse.aether.transfer.TransferEvent
@ -25,7 +27,7 @@ class ConsoleTransferListener @JvmOverloads constructor(out: PrintStream? = null
override fun transferInitiated(event: TransferEvent?) {
val message = if (event!!.requestType == TransferEvent.RequestType.PUT) "Uploading" else "Downloading"
out.println(message + ": " + event.resource.repositoryUrl + event.resource.resourceName)
log(2, message + ": " + event.resource.repositoryUrl + event.resource.resourceName)
}
override fun transferProgressed(event: TransferEvent?) {
@ -90,7 +92,7 @@ class ConsoleTransferListener @JvmOverloads constructor(out: PrintStream? = null
throughput = " at " + format.format(kbPerSec) + " KB/sec"
}
out.println(type + ": " + resource.repositoryUrl + resource.resourceName + " (" + len
log(2, type + ": " + resource.repositoryUrl + resource.resourceName + " (" + len
+ throughput + ")")
}
}
@ -99,7 +101,9 @@ class ConsoleTransferListener @JvmOverloads constructor(out: PrintStream? = null
transferCompleted(event)
if (event.exception !is MetadataNotFoundException) {
event.exception.printStackTrace(out)
if (KobaltLogger.LOG_LEVEL > 1) {
event.exception.printStackTrace(out)
}
}
}