mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 16:28:12 -07:00
Introducing IMavenIdInterceptor.
Allow plug-ins to rewrite Maven id's. Also fix the problem where .aar kept being refetched.
This commit is contained in:
parent
cd541f34cf
commit
7189fcde31
9 changed files with 86 additions and 34 deletions
|
@ -30,7 +30,7 @@ class ResolveDependency @Inject constructor(val repoFinder: RepoFinder) {
|
||||||
root.addChildren(findChildren(root, seen))
|
root.addChildren(findChildren(root, seen))
|
||||||
val repoResult = repoFinder.findCorrectRepo(id)
|
val repoResult = repoFinder.findCorrectRepo(id)
|
||||||
|
|
||||||
val simpleDep = SimpleDep(MavenId(id))
|
val simpleDep = SimpleDep(MavenId.create(id))
|
||||||
val url = repoResult.hostConfig.url + simpleDep.toJarFile(repoResult)
|
val url = repoResult.hostConfig.url + simpleDep.toJarFile(repoResult)
|
||||||
AsciiArt.logBox(listOf(id, url).map { " $it" }, {s -> println(s) })
|
AsciiArt.logBox(listOf(id, url).map { " $it" }, {s -> println(s) })
|
||||||
|
|
||||||
|
|
10
src/main/kotlin/com/beust/kobalt/api/IMavenIdInterceptor.kt
Normal file
10
src/main/kotlin/com/beust/kobalt/api/IMavenIdInterceptor.kt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
package com.beust.kobalt.api
|
||||||
|
|
||||||
|
import com.beust.kobalt.maven.MavenId
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plug-ins can rewrite Maven id's before Kobalt sees them with this interface.
|
||||||
|
*/
|
||||||
|
interface IMavenIdInterceptor: IInterceptor {
|
||||||
|
fun intercept(mavenId: MavenId) : MavenId
|
||||||
|
}
|
|
@ -75,6 +75,8 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
|
||||||
val buildConfigFieldContributors = arrayListOf<IBuildConfigFieldContributor>()
|
val buildConfigFieldContributors = arrayListOf<IBuildConfigFieldContributor>()
|
||||||
val taskContributors = arrayListOf<ITaskContributor>()
|
val taskContributors = arrayListOf<ITaskContributor>()
|
||||||
|
|
||||||
|
val mavenIdInterceptors = arrayListOf<IMavenIdInterceptor>()
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val PLUGIN_XML = "META-INF/kobalt-plugin.xml" // Plugins.PLUGIN_XML)
|
val PLUGIN_XML = "META-INF/kobalt-plugin.xml" // Plugins.PLUGIN_XML)
|
||||||
|
|
||||||
|
@ -142,6 +144,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
|
||||||
if (this is ITestRunnerContributor) testRunnerContributors.add(this)
|
if (this is ITestRunnerContributor) testRunnerContributors.add(this)
|
||||||
|
|
||||||
// Not documented yet
|
// Not documented yet
|
||||||
|
if (this is IMavenIdInterceptor) mavenIdInterceptors.add(this)
|
||||||
if (this is ITestSourceDirectoryContributor) testSourceDirContributors.add(this)
|
if (this is ITestSourceDirectoryContributor) testSourceDirContributors.add(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -171,6 +174,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
|
||||||
buildConfigFieldContributors.addAll(pluginInfo.buildConfigFieldContributors)
|
buildConfigFieldContributors.addAll(pluginInfo.buildConfigFieldContributors)
|
||||||
taskContributors.addAll(pluginInfo.taskContributors)
|
taskContributors.addAll(pluginInfo.taskContributors)
|
||||||
testSourceDirContributors.addAll(pluginInfo.testSourceDirContributors)
|
testSourceDirContributors.addAll(pluginInfo.testSourceDirContributors)
|
||||||
|
mavenIdInterceptors.addAll(pluginInfo.mavenIdInterceptors)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ public class DepFactory @Inject constructor(val localRepo: LocalRepo,
|
||||||
if (id.startsWith(FileDependency.PREFIX_FILE)) {
|
if (id.startsWith(FileDependency.PREFIX_FILE)) {
|
||||||
return FileDependency(id.substring(FileDependency.PREFIX_FILE.length))
|
return FileDependency(id.substring(FileDependency.PREFIX_FILE.length))
|
||||||
} else {
|
} else {
|
||||||
val mavenId = MavenId(id)
|
val mavenId = MavenId.create(id)
|
||||||
var version = mavenId.version
|
var version = mavenId.version
|
||||||
var packaging = mavenId.packaging
|
var packaging = mavenId.packaging
|
||||||
var repoResult: RepoFinder.RepoResult?
|
var repoResult: RepoFinder.RepoResult?
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package com.beust.kobalt.maven
|
package com.beust.kobalt.maven
|
||||||
|
|
||||||
|
import com.beust.kobalt.api.Kobalt
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encapsulate a Maven id captured in one string, as used by Gradle or Ivy, e.g. "org.testng:testng:6.9.9".
|
* Encapsulate a Maven id captured in one string, as used by Gradle or Ivy, e.g. "org.testng:testng:6.9.9".
|
||||||
* These id's are somewhat painful to manipulate because on top of containing groupId, artifactId
|
* These id's are somewhat painful to manipulate because on top of containing groupId, artifactId
|
||||||
|
@ -10,19 +12,61 @@ package com.beust.kobalt.maven
|
||||||
* This class accepts a versionless id, which needs to end with a :, e.g. "com.beust:jcommander:" (which
|
* This class accepts a versionless id, which needs to end with a :, e.g. "com.beust:jcommander:" (which
|
||||||
* usually means "latest version") but it doesn't handle version ranges yet.
|
* usually means "latest version") but it doesn't handle version ranges yet.
|
||||||
*/
|
*/
|
||||||
public class MavenId(val id: String) {
|
class MavenId private constructor(val groupId: String, val artifactId: String, val packaging: String?,
|
||||||
lateinit var groupId: String
|
val version: String?) {
|
||||||
lateinit var artifactId: String
|
|
||||||
var packaging: String? = null
|
|
||||||
var version: String? = null
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun isMavenId(id: String) = with(id.split(":")) {
|
fun isMavenId(id: String) = with(id.split(":")) {
|
||||||
size == 3 || size == 4
|
size == 3 || size == 4
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun isVersion(s: String) : Boolean = Character.isDigit(s[0])
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Similar to create(MavenId) but don't run IMavenIdInterceptors.
|
||||||
|
*/
|
||||||
|
fun createNoInterceptors(id: String) : MavenId {
|
||||||
|
var groupId: String? = null
|
||||||
|
var artifactId: String? = null
|
||||||
|
var version: String? = null
|
||||||
|
var packaging: String? = null
|
||||||
|
if (!isMavenId(id)) {
|
||||||
|
throw IllegalArgumentException("Illegal id: $id")
|
||||||
|
}
|
||||||
|
|
||||||
|
val c = id.split(":")
|
||||||
|
groupId = c[0]
|
||||||
|
artifactId = c[1]
|
||||||
|
if (!c[2].isEmpty()) {
|
||||||
|
if (isVersion(c[2])) {
|
||||||
|
version = c[2]
|
||||||
|
} else {
|
||||||
|
packaging = c[2]
|
||||||
|
version = c[3]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return MavenId(groupId, artifactId, packaging, version)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main entry point to create Maven Id's. Id's created by this function
|
||||||
|
* will run through IMavenIdInterceptors.
|
||||||
|
*/
|
||||||
|
fun create(id: String) : MavenId {
|
||||||
|
var originalMavenId = createNoInterceptors(id)
|
||||||
|
var interceptedMavenId = originalMavenId
|
||||||
|
val interceptors = Kobalt.context?.pluginInfo?.mavenIdInterceptors
|
||||||
|
if (interceptors != null) {
|
||||||
|
interceptedMavenId = interceptors.fold(originalMavenId, {
|
||||||
|
id, interceptor -> interceptor.intercept(id) })
|
||||||
|
}
|
||||||
|
|
||||||
|
return interceptedMavenId
|
||||||
|
}
|
||||||
|
|
||||||
fun create(groupId: String, artifactId: String, packaging: String?, version: String?) =
|
fun create(groupId: String, artifactId: String, packaging: String?, version: String?) =
|
||||||
MavenId(toId(groupId, artifactId, packaging, version))
|
create(toId(groupId, artifactId, packaging, version))
|
||||||
|
|
||||||
fun toId(groupId: String, artifactId: String, packaging: String? = null, version: String?) =
|
fun toId(groupId: String, artifactId: String, packaging: String? = null, version: String?) =
|
||||||
"$groupId:$artifactId" +
|
"$groupId:$artifactId" +
|
||||||
|
@ -30,24 +74,6 @@ public class MavenId(val id: String) {
|
||||||
":$version"
|
":$version"
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
|
||||||
if (! isMavenId(id)) {
|
|
||||||
throw IllegalArgumentException("Illegal id: $id")
|
|
||||||
}
|
|
||||||
val c = id.split(":")
|
|
||||||
groupId = c[0]
|
|
||||||
artifactId = c[1]
|
|
||||||
if (! c[2].isEmpty()) {
|
|
||||||
if (isVersion(c[2])) {
|
|
||||||
version = c[2]
|
|
||||||
} else {
|
|
||||||
packaging = c[2]
|
|
||||||
version = c[3]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun isVersion(s: String) : Boolean = Character.isDigit(s[0])
|
|
||||||
|
|
||||||
val hasVersion = version != null
|
val hasVersion = version != null
|
||||||
|
|
||||||
|
|
|
@ -72,7 +72,7 @@ public class RepoFinder @Inject constructor(val executors: KobaltExecutors) {
|
||||||
val repoUrl = repo.url
|
val repoUrl = repo.url
|
||||||
log(2, " Checking $repoUrl for $id")
|
log(2, " Checking $repoUrl for $id")
|
||||||
|
|
||||||
val mavenId = MavenId(id)
|
val mavenId = MavenId.create(id)
|
||||||
val groupId = mavenId.groupId
|
val groupId = mavenId.groupId
|
||||||
val artifactId = mavenId.artifactId
|
val artifactId = mavenId.artifactId
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,8 @@ import com.beust.kobalt.misc.Strings
|
||||||
|
|
||||||
open public class SimpleDep(open val mavenId: MavenId) : UnversionedDep(mavenId.groupId, mavenId.artifactId) {
|
open public class SimpleDep(open val mavenId: MavenId) : UnversionedDep(mavenId.groupId, mavenId.artifactId) {
|
||||||
companion object {
|
companion object {
|
||||||
fun create(id: String) = MavenId(id).let {
|
fun create(id: String) = MavenId.create(id).let {
|
||||||
SimpleDep(MavenId(id))
|
SimpleDep(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler,
|
||||||
val executors: KobaltExecutors, val dependencyManager: DependencyManager)
|
val executors: KobaltExecutors, val dependencyManager: DependencyManager)
|
||||||
: ConfigPlugin<AndroidConfig>(), IClasspathContributor, IRepoContributor, ICompilerFlagContributor,
|
: ConfigPlugin<AndroidConfig>(), IClasspathContributor, IRepoContributor, ICompilerFlagContributor,
|
||||||
ICompilerInterceptor, IBuildDirectoryIncerceptor, IRunnerContributor, IClasspathInterceptor,
|
ICompilerInterceptor, IBuildDirectoryIncerceptor, IRunnerContributor, IClasspathInterceptor,
|
||||||
ISourceDirectoryContributor, IBuildConfigFieldContributor, ITaskContributor {
|
ISourceDirectoryContributor, IBuildConfigFieldContributor, ITaskContributor, IMavenIdInterceptor {
|
||||||
companion object {
|
companion object {
|
||||||
const val PLUGIN_NAME = "Android"
|
const val PLUGIN_NAME = "Android"
|
||||||
const val TASK_GENERATE_DEX = "generateDex"
|
const val TASK_GENERATE_DEX = "generateDex"
|
||||||
|
@ -121,7 +121,7 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler,
|
||||||
project.compileDependencies.filter {
|
project.compileDependencies.filter {
|
||||||
it.jarFile.get().name.endsWith(".aar")
|
it.jarFile.get().name.endsWith(".aar")
|
||||||
}.forEach {
|
}.forEach {
|
||||||
val mavenId = MavenId(it.id)
|
val mavenId = MavenId.create(it.id)
|
||||||
val outputDir = AndroidFiles.intermediates(project)
|
val outputDir = AndroidFiles.intermediates(project)
|
||||||
val destDir = Paths.get(outputDir, "exploded-aar", mavenId.groupId,
|
val destDir = Paths.get(outputDir, "exploded-aar", mavenId.groupId,
|
||||||
mavenId.artifactId, mavenId.version)
|
mavenId.artifactId, mavenId.version)
|
||||||
|
@ -378,14 +378,17 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun isAar(id: MavenId) = id.groupId == "com.android.support" && id.artifactId != "support-annotations"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Automatically add the "aar" packaging for support libraries.
|
* For each com.android.support dependency or aar packaging, add a classpath dependency that points to the
|
||||||
|
* classes.jar inside that (exploded) aar.
|
||||||
*/
|
*/
|
||||||
// IClasspathInterceptor
|
// IClasspathInterceptor
|
||||||
override fun intercept(project: Project, dependencies: List<IClasspathDependency>): List<IClasspathDependency> {
|
override fun intercept(project: Project, dependencies: List<IClasspathDependency>): List<IClasspathDependency> {
|
||||||
val result = arrayListOf<IClasspathDependency>()
|
val result = arrayListOf<IClasspathDependency>()
|
||||||
dependencies.forEach {
|
dependencies.forEach {
|
||||||
if (it is MavenDependency && (it.groupId == "com.android.support" || it.mavenId.packaging == "aar")) {
|
if (it is MavenDependency && (isAar(it.mavenId) || it.mavenId.packaging == "aar")) {
|
||||||
val newDep = FileDependency(AndroidFiles.classesJar(project, it.mavenId))
|
val newDep = FileDependency(AndroidFiles.classesJar(project, it.mavenId))
|
||||||
result.add(newDep)
|
result.add(newDep)
|
||||||
val id = MavenId.create(it.groupId, it.artifactId, "aar", it.version)
|
val id = MavenId.create(it.groupId, it.artifactId, "aar", it.version)
|
||||||
|
@ -397,6 +400,15 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler,
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IMavenIdInterceptor
|
||||||
|
override fun intercept(mavenId: MavenId) : MavenId =
|
||||||
|
if (isAar(mavenId)) {
|
||||||
|
val version = mavenId.version ?: ""
|
||||||
|
MavenId.createNoInterceptors("${mavenId.groupId}:${mavenId.artifactId}:aar:$version")
|
||||||
|
} else {
|
||||||
|
mavenId
|
||||||
|
}
|
||||||
|
|
||||||
private val extraSourceDirectories = arrayListOf<File>()
|
private val extraSourceDirectories = arrayListOf<File>()
|
||||||
|
|
||||||
// ISourceDirectoryContributor
|
// ISourceDirectoryContributor
|
||||||
|
|
|
@ -23,7 +23,7 @@ class MavenIdTest {
|
||||||
@Test(dataProvider = "dp")
|
@Test(dataProvider = "dp")
|
||||||
fun parseVersions(id: String, groupId: String, artifactId: String, version: String?,
|
fun parseVersions(id: String, groupId: String, artifactId: String, version: String?,
|
||||||
packaging: String?, qualifier: String?) {
|
packaging: String?, qualifier: String?) {
|
||||||
val mi = MavenId(id)
|
val mi = MavenId.create(id)
|
||||||
Assert.assertEquals(mi.groupId, groupId)
|
Assert.assertEquals(mi.groupId, groupId)
|
||||||
Assert.assertEquals(mi.artifactId, artifactId)
|
Assert.assertEquals(mi.artifactId, artifactId)
|
||||||
Assert.assertEquals(mi.version, version)
|
Assert.assertEquals(mi.version, version)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue