From b61388f04bedf25585d667e73ddeeed91f78e144 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Wed, 28 Oct 2015 04:18:12 -0700 Subject: [PATCH] aar work. --- .../com/beust/kobalt/ProjectGenerator.kt | 3 +- .../kotlin/com/beust/kobalt/api/Project.kt | 1 + .../com/beust/kobalt/maven/DepFactory.kt | 18 ++++---- .../kobalt/maven/IClasspathDependency.kt | 2 +- .../kotlin/com/beust/kobalt/maven/LocalDep.kt | 4 +- .../com/beust/kobalt/maven/LocalRepo.kt | 4 +- .../com/beust/kobalt/maven/MavenDependency.kt | 22 ++++----- .../kotlin/com/beust/kobalt/maven/MavenId.kt | 21 +++++++++ src/main/kotlin/com/beust/kobalt/maven/Pom.kt | 9 ++-- .../com/beust/kobalt/maven/PomGenerator.kt | 3 +- .../com/beust/kobalt/maven/RepoFinder.kt | 46 +++++++++++++++---- .../com/beust/kobalt/maven/SimpleDep.kt | 23 ++++++---- .../beust/kobalt/plugin/java/JavaProject.kt | 7 ++- .../kobalt/plugin/kotlin/KotlinProject.kt | 6 ++- 14 files changed, 116 insertions(+), 53 deletions(-) create mode 100644 src/main/kotlin/com/beust/kobalt/maven/MavenId.kt diff --git a/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt b/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt index 8e841a12..60accc5c 100644 --- a/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt @@ -105,7 +105,8 @@ public class ProjectGenerator { private fun updateVersion(dep: Dependency, mapped: Map) = if ( dep.version.startsWith("\${")) { val property = dep.version.substring(2, dep.version.length - 1) - Dependency(dep.groupId, dep.artifactId, "\${${mapped.get(property)}}", dep.optional, dep.scope) + Dependency(dep.groupId, dep.artifactId, dep.packaging, "\${${mapped.get(property)}}", dep.optional, + dep.scope) } else { dep } diff --git a/src/main/kotlin/com/beust/kobalt/api/Project.kt b/src/main/kotlin/com/beust/kobalt/api/Project.kt index ab7a9686..e7f6d7d4 100644 --- a/src/main/kotlin/com/beust/kobalt/api/Project.kt +++ b/src/main/kotlin/com/beust/kobalt/api/Project.kt @@ -13,6 +13,7 @@ open public class Project( @Directive open var buildDirectory: String? = KFiles.KOBALT_BUILD_DIR, @Directive open var group: String? = null, @Directive open var artifactId: String? = null, + @Directive open var packaging: String? = null, @Directive open var dependencies: Dependencies? = null, @Directive open var sourceSuffix : String = "", @Directive open var compilerInfo : ICompilerInfo, diff --git a/src/main/kotlin/com/beust/kobalt/maven/DepFactory.kt b/src/main/kotlin/com/beust/kobalt/maven/DepFactory.kt index f75c9afd..3b6b42e8 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/DepFactory.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/DepFactory.kt @@ -18,26 +18,26 @@ public class DepFactory @Inject constructor(val localRepo: LocalRepo, if (id.startsWith(IClasspathDependency.PREFIX_FILE)) { return FileDependency(id.substring(IClasspathDependency.PREFIX_FILE.length)) } else { - val c = id.split(":") + val mavenId = MavenId(id) + var version = mavenId.version + var packaging = mavenId.packaging var repoResult: RepoFinder.RepoResult? - var version: String? = null - if (! MavenDependency.hasVersion(id)) { - if (localFirst) version = localRepo.findLocalVersion(c[0], c[1]) + if (! mavenId.hasVersion) { + if (localFirst) version = localRepo.findLocalVersion(mavenId.groupId, mavenId.artifactId, + mavenId.packaging) if (! localFirst || version == null) { repoResult = repoFinder.findCorrectRepo(id) if (!repoResult.found) { - throw KobaltException("Couldn't resolve ${id}") + throw KobaltException("Couldn't resolve $id") } else { version = repoResult.version } } - } else { - version = c[2] } - return MavenDependency(c[0], c[1], version, executor, localRepo, repoFinder, - pomFactory, downloadManager) + return MavenDependency(mavenId.groupId, mavenId.artifactId, packaging, version!!, + executor, localRepo, repoFinder, pomFactory, downloadManager) } } } diff --git a/src/main/kotlin/com/beust/kobalt/maven/IClasspathDependency.kt b/src/main/kotlin/com/beust/kobalt/maven/IClasspathDependency.kt index 3ca91115..b13a670f 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/IClasspathDependency.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/IClasspathDependency.kt @@ -8,7 +8,7 @@ import java.util.concurrent.Future interface IClasspathDependency { companion object { - val PREFIX_FILE: String = "file:" + val PREFIX_FILE: String = "file:/" } /** Identifier for this dependency */ diff --git a/src/main/kotlin/com/beust/kobalt/maven/LocalDep.kt b/src/main/kotlin/com/beust/kobalt/maven/LocalDep.kt index 7d9553f6..c958b54b 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/LocalDep.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/LocalDep.kt @@ -7,8 +7,8 @@ import java.util.concurrent.Future import kotlin.properties.Delegates open public class LocalDep(override val groupId: String, override val artifactId: String, - override val version: String, - open val localRepo: LocalRepo) : SimpleDep(groupId, artifactId, version) { + override val packaging: String?, override val version: String, + open val localRepo: LocalRepo) : SimpleDep(groupId, artifactId, packaging, version) { fun toAbsoluteJarFilePath(v: String) = localRepo.toFullPath(toJarFile(v)) diff --git a/src/main/kotlin/com/beust/kobalt/maven/LocalRepo.kt b/src/main/kotlin/com/beust/kobalt/maven/LocalRepo.kt index 7577af80..0e177df5 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/LocalRepo.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/LocalRepo.kt @@ -26,7 +26,7 @@ open public class LocalRepo(open val localRepo: String = KFiles.localRepo) { /** * If the dependency is local, return the correct version for it */ - fun findLocalVersion(groupId: String, artifactId: String) : String? { + fun findLocalVersion(groupId: String, artifactId: String, packaging: String? = null) : String? { // No version: look at all the directories under group/artifactId, pick the latest and see // if it contains a maven and jar file val dir = toFullPath(KFiles.joinDir(groupId.replace(".", File.separator), artifactId)) @@ -41,7 +41,7 @@ open public class LocalRepo(open val localRepo: String = KFiles.localRepo) { v2.compareTo(v1) // we want the most recent at position 0 }) val result = directories.get(0).name - val newDep = LocalDep(groupId, artifactId, result, this) + val newDep = LocalDep(groupId, artifactId, packaging, result, this) if (existsPom(newDep, result) && existsJar(newDep, result)) { return result } diff --git a/src/main/kotlin/com/beust/kobalt/maven/MavenDependency.kt b/src/main/kotlin/com/beust/kobalt/maven/MavenDependency.kt index 7d33e990..5f0a0730 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/MavenDependency.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/MavenDependency.kt @@ -12,13 +12,14 @@ import kotlin.properties.Delegates public class MavenDependency @Inject constructor(override @Assisted("groupId") val groupId : String, override @Assisted("artifactId") val artifactId : String, + override @Assisted("packaging") val packaging: String?, override @Assisted("version") val version : String, val executor: ExecutorService, override val localRepo: LocalRepo, val repoFinder: RepoFinder, val pomFactory: Pom.IFactory, val downloadManager: DownloadManager) - : LocalDep(groupId, artifactId, version, localRepo), IClasspathDependency, + : LocalDep(groupId, artifactId, packaging, version, localRepo), IClasspathDependency, Comparable { override var jarFile: Future by Delegates.notNull() var pomFile: Future by Delegates.notNull() @@ -30,7 +31,7 @@ public class MavenDependency @Inject constructor(override @Assisted("groupId") v jarFile = CompletedFuture(jar) pomFile = CompletedFuture(pom) } else { - val repoResult = repoFinder.findCorrectRepo(toId(groupId, artifactId, version)) + val repoResult = repoFinder.findCorrectRepo(toId(groupId, artifactId, packaging, version)) if (repoResult.found) { jarFile = if (repoResult.hasJar) { @@ -41,7 +42,7 @@ public class MavenDependency @Inject constructor(override @Assisted("groupId") v pomFile = downloadManager.download(repoResult.repoUrl + toPomFile(repoResult), pom.absolutePath, executor) } else { - throw KobaltException("Couldn't resolve ${toId(groupId, artifactId, version)}") + throw KobaltException("Couldn't resolve ${toId(groupId, artifactId, packaging, version)}") } } } @@ -61,18 +62,15 @@ public class MavenDependency @Inject constructor(override @Assisted("groupId") v return depFactory.create(id, ex) } - fun hasVersion(id: String) : Boolean { - val c = id.split(":") - return c.size == 3 && !Strings.isEmpty(c[2]) - } - - fun toId(g: String, a: String, v: String) = "$g:$a:$v" + fun toId(g: String, a: String, packaging: String?, v: String) = + if (packaging.isNullOrBlank()) "$g:$a:$v" + else "$g:$a:$packaging:$v" } - public override fun toString() = toId(groupId, artifactId, version) + public override fun toString() = toId(groupId, artifactId, packaging, version) - override val id = toId(groupId, artifactId, version) + override val id = toId(groupId, artifactId, packaging, version) override fun toMavenDependencies(): org.apache.maven.model.Dependency { with(org.apache.maven.model.Dependency()) { @@ -94,7 +92,7 @@ public class MavenDependency @Inject constructor(override @Assisted("groupId") v pomFactory.create(id, pomFile.get()).dependencies.filter { it.mustDownload && it.isValid }.forEach { - result.add(create(toId(it.groupId, it.artifactId, it.version))) + result.add(create(toId(it.groupId, it.artifactId, it.packaging, it.version))) } return result } diff --git a/src/main/kotlin/com/beust/kobalt/maven/MavenId.kt b/src/main/kotlin/com/beust/kobalt/maven/MavenId.kt new file mode 100644 index 00000000..836940cd --- /dev/null +++ b/src/main/kotlin/com/beust/kobalt/maven/MavenId.kt @@ -0,0 +1,21 @@ +package com.beust.kobalt.maven + +public class MavenId(val id: String) { + lateinit var groupId: String + lateinit var artifactId: String + var packaging: String? = null + var version: String? = null + + init { + val c = id.split(":") + if (c.size != 3 && c.size != 4) { + throw IllegalArgumentException("Illegal id: $id") + } + groupId = c[0] + artifactId = c[1] + packaging = if (c.size == 4) c[2] else null + version = if (c.size == 4) c[3] else c[2] + } + + val hasVersion = version != null +} diff --git a/src/main/kotlin/com/beust/kobalt/maven/Pom.kt b/src/main/kotlin/com/beust/kobalt/maven/Pom.kt index 8086c816..2654c55c 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/Pom.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/Pom.kt @@ -16,6 +16,7 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String, val XPATH = XPATH_FACTORY.newXPath() var groupId: String? = null var artifactId: String? = null + var packaging: String? = null var version: String? = null var name: String? = null var properties = sortedMapOf() @@ -25,8 +26,8 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String, fun create(@Assisted id: String, @Assisted documentFile: java.io.File): Pom } - data public class Dependency(val groupId: String, val artifactId: String, val version: String, - val optional: Boolean = false, val scope: String? = null) { + data public class Dependency(val groupId: String, val artifactId: String, val packaging: String?, + val version: String, val optional: Boolean = false, val scope: String? = null) { /** When a variable is used in a maven file, e.g. ${version} */ private val VAR = "$" + "{" @@ -79,6 +80,7 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String, val d = deps.item(i) as NodeList var groupId: String? = null var artifactId: String? = null + var packaging: String? = null var version: String = "" var optional: Boolean? = false var scope: String? = null @@ -88,6 +90,7 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String, when (e.tagName) { "groupId" -> groupId = e.textContent "artifactId" -> artifactId = e.textContent + "packaging" -> packaging = e.textContent "version" -> version = e.textContent "optional" -> optional = "true".equals(e.textContent, true) "scope" -> scope = e.textContent @@ -95,7 +98,7 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String, } } log(3, "Done parsing: $groupId $artifactId $version") - val tmpDependency = Dependency(groupId!!, artifactId!!, version, optional!!, scope) + val tmpDependency = Dependency(groupId!!, artifactId!!, packaging, version, optional!!, scope) dependencies.add(tmpDependency) } } diff --git a/src/main/kotlin/com/beust/kobalt/maven/PomGenerator.kt b/src/main/kotlin/com/beust/kobalt/maven/PomGenerator.kt index 736bf249..bc50b0b2 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/PomGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/PomGenerator.kt @@ -54,7 +54,8 @@ public class PomGenerator @Inject constructor(@Assisted val project: Project) { val buildDir = com.beust.kobalt.misc.KFiles.makeDir(project.directory, project.buildDirectory!!) val outputDir = com.beust.kobalt.misc.KFiles.makeDir(buildDir.path, "libs") - val pomFile = SimpleDep(project.group!!, project.artifactId!!, project.version!!).toPomFileName() + val pomFile = SimpleDep(project.group!!, project.artifactId!!, project.packaging, project.version!!) + .toPomFileName() val outputFile = File(outputDir, pomFile) outputFile.writeText(s.toString(), Charset.defaultCharset()) log(1, " Wrote $outputFile") diff --git a/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt b/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt index 3ac4e3dd..37dafce2 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt @@ -9,6 +9,9 @@ import com.google.common.cache.CacheBuilder import com.google.common.cache.CacheLoader import com.google.common.cache.LoadingCache import java.io.File +import java.net.HttpURLConnection +import java.net.URI +import java.net.URL import java.util.concurrent.Callable import java.util.concurrent.ExecutorCompletionService import java.util.concurrent.TimeUnit @@ -63,17 +66,34 @@ public class RepoFinder @Inject constructor(val http: Http, val executors: Kobal } } + private fun urlExists(url: String) : Boolean { + val connection = URL(url).openConnection() + val result = + if (connection is HttpURLConnection) { + connection.responseCode == 200 + } else if (url.startsWith(IClasspathDependency.PREFIX_FILE)) { + val fileName = url.substring(IClasspathDependency.PREFIX_FILE.length) + File(fileName).exists() + } else { + false + } + return result + } + /** * Execute a single HTTP request to one repo. */ - inner class RepoFinderCallable(val id: String, val repoUrl: String) : Callable { override fun call(): RepoResult { log(2, "Checking $repoUrl for $id") - val c = id.split(":") - if (! MavenDependency.hasVersion(id)) { - val ud = UnversionedDep(c[0], c[1]) + val mavenId = MavenId(id) + val groupId = mavenId.groupId + val artifactId = mavenId.artifactId + var packaging = mavenId.packaging + val version = mavenId.version + if (! mavenId.hasVersion) { + val ud = UnversionedDep(groupId, artifactId) val foundVersion = findCorrectVersionRelease(ud.toMetadataXmlPath(false), repoUrl) if (foundVersion != null) { return RepoResult(repoUrl, true, foundVersion) @@ -81,19 +101,25 @@ public class RepoFinder @Inject constructor(val http: Http, val executors: Kobal return RepoResult(repoUrl, false, "") } } else { - if (c[2].contains("SNAPSHOT")) { - val dep = SimpleDep(c[0], c[1], c[2]) + if (version!!.contains("SNAPSHOT")) { + val dep = SimpleDep(groupId, artifactId, packaging, version) val snapshotVersion = findSnapshotVersion(dep.toMetadataXmlPath(false), repoUrl) if (snapshotVersion != null) { - return RepoResult(repoUrl, true, c[2], true /* hasJar, potential bug here */, snapshotVersion) + return RepoResult(repoUrl, true, version, true /* hasJar, potential bug here */, + snapshotVersion) } else { return RepoResult(repoUrl, false, "") } } else { - val dep = SimpleDep(c[0], c[1], c[2]) + val dep = SimpleDep(groupId, artifactId, packaging, version) // Try to find the jar file val urlJar = repoUrl + dep.toJarFile(dep.version) - val hasJar = http.get(urlJar).code == 200 + + if (repoUrl.contains("beust")) { + println("DONOTCOMMIT") + } + + val hasJar = urlExists(urlJar) val found = if (! hasJar) { @@ -129,7 +155,7 @@ public class RepoFinder @Inject constructor(val http: Http, val executors: Kobal } }) } catch(ex: Exception) { - log(2, "Couldn't find metadata at $url") + log(2, "Couldn't find metadata at $url: ${ex.message}") } return null } diff --git a/src/main/kotlin/com/beust/kobalt/maven/SimpleDep.kt b/src/main/kotlin/com/beust/kobalt/maven/SimpleDep.kt index fddcbe92..e0094cd6 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/SimpleDep.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/SimpleDep.kt @@ -1,14 +1,16 @@ package com.beust.kobalt.maven import com.beust.kobalt.misc.Strings -import com.google.common.base.CharMatcher -import java.io.File -import kotlin.properties.Delegates open public class SimpleDep(override val groupId: String, override val artifactId: String, - open val version: String) : UnversionedDep(groupId, artifactId) { + open val packaging: String?, open val version: String) : UnversionedDep(groupId, artifactId) { companion object { - fun create(id: String) = id.split(":").let { SimpleDep(it[0], it[1], it[2])} + fun create(id: String) = MavenId(id).let { + if (id.contains("android")) { + println("DONOTCOMMIT") + } + SimpleDep(it.groupId, it.artifactId, it.packaging, it.version!!) + } } override public fun toMetadataXmlPath(fileSystem: Boolean): String { @@ -25,9 +27,14 @@ open public class SimpleDep(override val groupId: String, override val artifactI fun toPomFile(r: RepoFinder.RepoResult) = toFile(r.version, r.snapshotVersion, ".pom") - fun toJarFile(v: String = version) = toFile(v, "", ".jar") + fun toJarFile(v: String = version) = toFile(v, "", suffix) - fun toJarFile(r: RepoFinder.RepoResult) = toFile(r.version, r.snapshotVersion, ".jar") + fun toJarFile(r: RepoFinder.RepoResult) = toFile(r.version, r.snapshotVersion, suffix) - fun toPomFileName() = "${artifactId}-${version}.pom" + fun toPomFileName() = "$artifactId-$version.pom" + + val suffix : String + get() { + return if (packaging != null && ! packaging.isNullOrBlank()) ".${packaging!!}" else ".jar" + } } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaProject.kt b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaProject.kt index f5090399..2427ae19 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaProject.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaProject.kt @@ -23,8 +23,11 @@ public class JavaProject( @Directive override var artifactId: String? = null, @Directive - override var dependencies: Dependencies? = null) - : Project(name, version, directory, buildDirectory, group, artifactId, dependencies, ".java", JavaCompilerInfo()) { + override var dependencies: Dependencies? = null, + @Directive + override var packaging: String? = null) + : Project(name, version, directory, buildDirectory, group, artifactId, packaging, dependencies, + ".java", JavaCompilerInfo()) { override public fun toString() = ToString("JavaProject", "name", name!!).s } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinProject.kt b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinProject.kt index 20930fc3..5bf9cbb3 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinProject.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinProject.kt @@ -24,8 +24,10 @@ public class KotlinProject( @Directive override var artifactId: String? = name, @Directive - override var dependencies: Dependencies? = null) - : Project(name, version, directory, buildDirectory, group, artifactId, dependencies, ".kt", + override var dependencies: Dependencies? = null, + @Directive + override var packaging: String? = null) + : Project(name, version, directory, buildDirectory, group, artifactId, packaging, dependencies, ".kt", KotlinCompilerInfo()) { override public fun toString() = ToString("KotlinProject", "name", name!!).s