mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-27 00:38:11 -07:00
Cache downloaded artifacts.
This commit is contained in:
parent
dda854963c
commit
d7c41bf1ae
1 changed files with 29 additions and 3 deletions
|
@ -4,12 +4,16 @@ 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.internal.KobaltSettings
|
import com.beust.kobalt.internal.KobaltSettings
|
||||||
|
import com.beust.kobalt.internal.KobaltSettingsXml
|
||||||
import com.beust.kobalt.maven.CompletedFuture
|
import com.beust.kobalt.maven.CompletedFuture
|
||||||
import com.beust.kobalt.maven.MavenId
|
import com.beust.kobalt.maven.MavenId
|
||||||
import com.beust.kobalt.misc.KobaltLogger
|
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
|
||||||
|
import com.google.common.cache.CacheBuilder
|
||||||
|
import com.google.common.cache.CacheLoader
|
||||||
|
import com.google.common.cache.LoadingCache
|
||||||
import com.google.inject.Inject
|
import com.google.inject.Inject
|
||||||
import org.eclipse.aether.artifact.Artifact
|
import org.eclipse.aether.artifact.Artifact
|
||||||
import org.eclipse.aether.artifact.DefaultArtifact
|
import org.eclipse.aether.artifact.DefaultArtifact
|
||||||
|
@ -32,6 +36,15 @@ class DependencyResult(val dependency: IClasspathDependency, val repoUrl: String
|
||||||
class KobaltAether @Inject constructor (val settings: KobaltSettings) {
|
class KobaltAether @Inject constructor (val settings: KobaltSettings) {
|
||||||
val localRepo: File get() = File(settings.localRepo)
|
val localRepo: File get() = File(settings.localRepo)
|
||||||
|
|
||||||
|
class MaybeArtifact(val result: DependencyResult?, val error: String?)
|
||||||
|
|
||||||
|
private val CACHE : LoadingCache<String, MaybeArtifact> = CacheBuilder.newBuilder()
|
||||||
|
.build(object : CacheLoader<String, MaybeArtifact>() {
|
||||||
|
override fun load(id: String): MaybeArtifact {
|
||||||
|
return doResolve(id)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Don't call this method directly, use `DepFactory` instead.
|
* Don't call this method directly, use `DepFactory` instead.
|
||||||
*/
|
*/
|
||||||
|
@ -48,11 +61,20 @@ class KobaltAether @Inject constructor (val settings: KobaltSettings) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun resolve(id: String): DependencyResult {
|
fun resolve(id: String): DependencyResult {
|
||||||
|
val result = CACHE.get(id)
|
||||||
|
if (result.result != null) return result.result
|
||||||
|
else throw KobaltException("Couldn't resolve $id")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun doResolve(id: String): MaybeArtifact {
|
||||||
|
log(1, "Resolving $id")
|
||||||
val results = Aether(localRepo).resolve(DefaultArtifact(MavenId.toKobaltId(id)))
|
val results = Aether(localRepo).resolve(DefaultArtifact(MavenId.toKobaltId(id)))
|
||||||
if (results != null && results.size > 0) {
|
if (results != null && results.size > 0) {
|
||||||
return DependencyResult(AetherDependency(results[0].artifact), results[0].repository.toString())
|
return MaybeArtifact(
|
||||||
|
DependencyResult(AetherDependency(results[0].artifact), results[0].repository.toString()),
|
||||||
|
null)
|
||||||
} else {
|
} else {
|
||||||
throw KobaltException("Couldn't resolve $id")
|
return MaybeArtifact(null, "Couldn't locate $id")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -222,7 +244,11 @@ class AetherDependency(val artifact: Artifact): IClasspathDependency, Comparable
|
||||||
}
|
}
|
||||||
|
|
||||||
fun main(argv: Array<String>) {
|
fun main(argv: Array<String>) {
|
||||||
KobaltLogger.LOG_LEVEL = 2
|
KobaltLogger.LOG_LEVEL = 1
|
||||||
|
val id = "org.testng:testng:6.9.11"
|
||||||
|
val aether = KobaltAether(KobaltSettings(KobaltSettingsXml()))
|
||||||
|
val r = aether.resolve(id)
|
||||||
|
val r2 = aether.resolve(id)
|
||||||
val d = org.eclipse.aether.artifact.DefaultArtifact("org.testng:testng:6.9")
|
val d = org.eclipse.aether.artifact.DefaultArtifact("org.testng:testng:6.9")
|
||||||
|
|
||||||
println("Artifact: " + d)
|
println("Artifact: " + d)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue