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

Optional compile dependencies with "compileOptional(...)".

This commit is contained in:
Cedric Beust 2016-09-22 11:05:39 -07:00
parent 37ef22cccc
commit 91baf6e86f
10 changed files with 45 additions and 29 deletions

View file

@ -11,6 +11,7 @@ interface IDependencyHolder {
var project: Project var project: Project
val compileDependencies : ArrayList<IClasspathDependency> val compileDependencies : ArrayList<IClasspathDependency>
val optionalDependencies : ArrayList<IClasspathDependency>
val compileProvidedDependencies : ArrayList<IClasspathDependency> val compileProvidedDependencies : ArrayList<IClasspathDependency>
val compileRuntimeDependencies : ArrayList<IClasspathDependency> val compileRuntimeDependencies : ArrayList<IClasspathDependency>
val excludedDependencies : ArrayList<IClasspathDependency> val excludedDependencies : ArrayList<IClasspathDependency>
@ -26,6 +27,7 @@ interface IDependencyHolder {
open class DependencyHolder : IDependencyHolder { open class DependencyHolder : IDependencyHolder {
override lateinit var project: Project override lateinit var project: Project
override val compileDependencies : ArrayList<IClasspathDependency> = arrayListOf() override val compileDependencies : ArrayList<IClasspathDependency> = arrayListOf()
override val optionalDependencies : ArrayList<IClasspathDependency> = arrayListOf()
override val compileProvidedDependencies : ArrayList<IClasspathDependency> = arrayListOf() override val compileProvidedDependencies : ArrayList<IClasspathDependency> = arrayListOf()
override val compileRuntimeDependencies : ArrayList<IClasspathDependency> = arrayListOf() override val compileRuntimeDependencies : ArrayList<IClasspathDependency> = arrayListOf()
override val excludedDependencies : ArrayList<IClasspathDependency> = arrayListOf() override val excludedDependencies : ArrayList<IClasspathDependency> = arrayListOf()
@ -34,7 +36,7 @@ open class DependencyHolder : IDependencyHolder {
override var dependencies : Dependencies? = null override var dependencies : Dependencies? = null
override fun dependencies(init: Dependencies.() -> Unit) : Dependencies { override fun dependencies(init: Dependencies.() -> Unit) : Dependencies {
dependencies = Dependencies(project, compileDependencies, compileProvidedDependencies, dependencies = Dependencies(project, compileDependencies, optionalDependencies, compileProvidedDependencies,
compileRuntimeDependencies, excludedDependencies, nativeDependencies) compileRuntimeDependencies, excludedDependencies, nativeDependencies)
dependencies!!.init() dependencies!!.init()
return dependencies!! return dependencies!!

View file

@ -22,6 +22,9 @@ interface IClasspathDependency {
/** @return true if this dependency represents a Maven coordinate */ /** @return true if this dependency represents a Maven coordinate */
val isMaven: Boolean val isMaven: Boolean
/** @return whether this dependency is optional */
val optional: Boolean
/** Absolute path to the jar file on the local file system */ /** Absolute path to the jar file on the local file system */
val jarFile: Future<File> val jarFile: Future<File>

View file

@ -9,12 +9,12 @@ interface IDependencyManager {
/** /**
* Parse the id and return the correct IClasspathDependency * Parse the id and return the correct IClasspathDependency
*/ */
fun create(id: String, projectDirectory: String? = null): IClasspathDependency fun create(id: String, optional: Boolean = false, projectDirectory: String? = null): IClasspathDependency
/** /**
* Create an IClasspathDependency from a Maven id. * Create an IClasspathDependency from a Maven id.
*/ */
fun createMaven(id: String): IClasspathDependency fun createMaven(id: String, optional: Boolean = false): IClasspathDependency
/** /**
* Create an IClasspathDependency from a path. * Create an IClasspathDependency from a path.

View file

@ -6,7 +6,6 @@ import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.aether.KobaltAether import com.beust.kobalt.maven.aether.KobaltAether
import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.kobaltLog import com.beust.kobalt.misc.kobaltLog
import com.beust.kobalt.misc.log
import org.apache.maven.model.Model import org.apache.maven.model.Model
import java.io.File import java.io.File
import java.util.* import java.util.*
@ -86,8 +85,8 @@ open class Project(
@Directive @Directive
fun dependenciesTest(init: Dependencies.() -> Unit) : Dependencies { fun dependenciesTest(init: Dependencies.() -> Unit) : Dependencies {
dependencies = Dependencies(this, testDependencies, testProvidedDependencies, compileRuntimeDependencies, dependencies = Dependencies(this, testDependencies, arrayListOf(),
excludedDependencies, nativeDependencies) testProvidedDependencies, compileRuntimeDependencies, excludedDependencies, nativeDependencies)
dependencies!!.init() dependencies!!.init()
return dependencies!! return dependencies!!
} }
@ -133,6 +132,7 @@ class Sources(val project: Project, val sources: HashSet<String>) {
class Dependencies(val project: Project, class Dependencies(val project: Project,
val dependencies: ArrayList<IClasspathDependency>, val dependencies: ArrayList<IClasspathDependency>,
val optionalDependencies: ArrayList<IClasspathDependency>,
val providedDependencies: ArrayList<IClasspathDependency>, val providedDependencies: ArrayList<IClasspathDependency>,
val runtimeDependencies: ArrayList<IClasspathDependency>, val runtimeDependencies: ArrayList<IClasspathDependency>,
val excludedDependencies: ArrayList<IClasspathDependency>, val excludedDependencies: ArrayList<IClasspathDependency>,
@ -145,7 +145,7 @@ class Dependencies(val project: Project,
* future tasks receive a get(), the repos will be correct. * future tasks receive a get(), the repos will be correct.
*/ */
private fun addToDependencies(project: Project, dependencies: ArrayList<IClasspathDependency>, private fun addToDependencies(project: Project, dependencies: ArrayList<IClasspathDependency>,
dep: Array<out String>): List<Future<File>> dep: Array<out String>, optional: Boolean = false): List<Future<File>>
= with(dep.map { = with(dep.map {
val resolved = val resolved =
if (KobaltAether.isRangeVersion(it)) { if (KobaltAether.isRangeVersion(it)) {
@ -155,7 +155,7 @@ class Dependencies(val project: Project,
} else { } else {
it it
} }
DependencyManager.create(resolved, project.directory) DependencyManager.create(resolved, optional, project.directory)
}) { }) {
dependencies.addAll(this) dependencies.addAll(this)
this.map { FutureTask { it.jarFile.get() } } this.map { FutureTask { it.jarFile.get() } }
@ -164,6 +164,10 @@ class Dependencies(val project: Project,
@Directive @Directive
fun compile(vararg dep: String) = addToDependencies(project, dependencies, dep) fun compile(vararg dep: String) = addToDependencies(project, dependencies, dep)
@Directive
fun compileOptional(vararg dep: String) = addToDependencies(project, optionalDependencies, dep,
optional = true)
@Directive @Directive
fun provided(vararg dep: String) = addToDependencies(project, providedDependencies, dep) fun provided(vararg dep: String) = addToDependencies(project, providedDependencies, dep)

View file

@ -18,14 +18,14 @@ class DependencyManager @Inject constructor(val executors: KobaltExecutors, val
: IDependencyManager { : IDependencyManager {
companion object { companion object {
fun create(id: String, projectDirectory: String? = null) = fun create(id: String, optional: Boolean = false, projectDirectory: String? = null) =
Kobalt.INJECTOR.getInstance(DependencyManager::class.java).create(id, projectDirectory) Kobalt.INJECTOR.getInstance(DependencyManager::class.java).create(id, optional, projectDirectory)
} }
/** /**
* Parse the id and return the correct IClasspathDependency * Parse the id and return the correct IClasspathDependency
*/ */
override fun create(id: String, projectDirectory: String?) : IClasspathDependency { override fun create(id: String, optional: Boolean, projectDirectory: String?) : IClasspathDependency {
if (id.startsWith(FileDependency.PREFIX_FILE)) { if (id.startsWith(FileDependency.PREFIX_FILE)) {
val path = if (projectDirectory != null) { val path = if (projectDirectory != null) {
val idPath = id.substring(FileDependency.PREFIX_FILE.length) val idPath = id.substring(FileDependency.PREFIX_FILE.length)
@ -50,18 +50,18 @@ class DependencyManager @Inject constructor(val executors: KobaltExecutors, val
} else { } else {
// Convert to a Kobalt id first (so that if it doesn't have a version, it gets translated to // Convert to a Kobalt id first (so that if it doesn't have a version, it gets translated to
// an Aether ranged id "[0,)") // an Aether ranged id "[0,)")
return createMaven(MavenId.create(id).toId) return createMaven(MavenId.create(id).toId, optional)
} }
} }
/** /**
* Create an IClasspathDependency from a Maven id. * Create an IClasspathDependency from a Maven id.
*/ */
override fun createMaven(id: String) : IClasspathDependency= override fun createMaven(id: String, optional: Boolean) : IClasspathDependency=
if (KobaltAether.isRangeVersion(id)) { if (KobaltAether.isRangeVersion(id)) {
Kobalt.INJECTOR.getInstance(KobaltAether::class.java).resolve(id).dependency Kobalt.INJECTOR.getInstance(KobaltAether::class.java).resolve(id).dependency
} else { } else {
aether.create(id) aether.create(id, optional)
} }
/** /**

View file

@ -16,7 +16,7 @@ class DependencyManager2 @Inject constructor(val aether: KobaltAether) {
/** /**
* Create an IClasspathDependency from a Maven id. * Create an IClasspathDependency from a Maven id.
*/ */
fun createMaven(id: String) : IClasspathDependency = aether.create(id) fun createMaven(id: String, optional: Boolean) : IClasspathDependency = aether.create(id, optional)
/** /**
* Create an IClasspathDependency from a path. * Create an IClasspathDependency from a path.
@ -26,7 +26,7 @@ class DependencyManager2 @Inject constructor(val aether: KobaltAether) {
/** /**
* Parse the id and return the correct IClasspathDependency * Parse the id and return the correct IClasspathDependency
*/ */
fun create(id: String, projectDirectory: String?) : IClasspathDependency { fun create(id: String, optional: Boolean, projectDirectory: String?) : IClasspathDependency {
if (id.startsWith(FileDependency.PREFIX_FILE)) { if (id.startsWith(FileDependency.PREFIX_FILE)) {
val path = if (projectDirectory != null) { val path = if (projectDirectory != null) {
val idPath = id.substring(FileDependency.PREFIX_FILE.length) val idPath = id.substring(FileDependency.PREFIX_FILE.length)
@ -51,7 +51,7 @@ class DependencyManager2 @Inject constructor(val aether: KobaltAether) {
} else { } else {
// Convert to a Kobalt id first (so that if it doesn't have a version, it gets translated to // Convert to a Kobalt id first (so that if it doesn't have a version, it gets translated to
// an Aether ranged id "[0,)") // an Aether ranged id "[0,)")
return createMaven(MavenId.create(id).toId) return createMaven(MavenId.create(id).toId, optional)
} }
} }
@ -106,7 +106,7 @@ class DependencyManager2 @Inject constructor(val aether: KobaltAether) {
ids.forEach { ids.forEach {
if (it.isMaven) { if (it.isMaven) {
val resolved = aether.resolveAll(it.id, filterScopes = scopeFilters) val resolved = aether.resolveAll(it.id, filterScopes = scopeFilters)
.map { create(it.toString(), project.directory) } .map { create(it.toString(), false, project.directory) }
i++ i++
result.addAll(resolved) result.addAll(resolved)
} else { } else {

View file

@ -62,12 +62,17 @@ class PomGenerator @Inject constructor(@Assisted val project: Project) {
// //
pom.dependencies = arrayListOf<org.apache.maven.model.Dependency>() pom.dependencies = arrayListOf<org.apache.maven.model.Dependency>()
// 1. Compile dependencies // Compile dependencies
project.compileDependencies.forEach { dep -> project.compileDependencies.forEach { dep ->
pom.dependencies.add(dep.toMavenDependencies()) pom.dependencies.add(dep.toMavenDependencies())
} }
// 2. Project dependencies // Optional compile dependencies
project.optionalDependencies.forEach { dep ->
pom.dependencies.add(dep.toMavenDependencies())
}
// Project dependencies
project.dependsOn.forEach { project.dependsOn.forEach {
pom.dependencies.add(org.apache.maven.model.Dependency().apply { pom.dependencies.add(org.apache.maven.model.Dependency().apply {
version = it.version version = it.version

View file

@ -84,7 +84,7 @@ class KobaltAether @Inject constructor (val settings: KobaltSettings, val aether
/** /**
* Create an IClasspathDependency from a Kobalt id. * Create an IClasspathDependency from a Kobalt id.
*/ */
fun create(id: String) = AetherDependency(DefaultArtifact(id)) fun create(id: String, optional: Boolean) = AetherDependency(DefaultArtifact(id), optional)
/** /**
* @return the latest artifact for the given group and artifactId. * @return the latest artifact for the given group and artifactId.
@ -231,12 +231,10 @@ class Aether(localRepo: File, val settings: KobaltSettings, eventBus: EventBus)
= system.collectDependencies(session, collectRequest(artifact, artifactScope)) = system.collectDependencies(session, collectRequest(artifact, artifactScope))
} }
class AetherDependency(val artifact: Artifact) : IClasspathDependency, Comparable<AetherDependency> { class AetherDependency(val artifact: Artifact, override val optional: Boolean = false)
: IClasspathDependency, Comparable<AetherDependency> {
val aether: Aether get() = Kobalt.INJECTOR.getInstance(Aether::class.java) val aether: Aether get() = Kobalt.INJECTOR.getInstance(Aether::class.java)
constructor(node: DependencyNode) : this(node.artifact) {
}
override val id: String = toId(artifact) override val id: String = toId(artifact)
override val version: String = artifact.version override val version: String = artifact.version
@ -268,12 +266,15 @@ class AetherDependency(val artifact: Artifact) : IClasspathDependency, Comparabl
} }
} }
override fun toMavenDependencies() = override fun toMavenDependencies() : org.apache.maven.model.Dependency {
org.apache.maven.model.Dependency().apply { val op = this.optional
return org.apache.maven.model.Dependency().apply {
groupId = artifact.groupId groupId = artifact.groupId
artifactId = artifact.artifactId artifactId = artifact.artifactId
version = artifact.version version = artifact.version
if (op) optional = op.toString()
} }
}
override fun directDependencies(): List<IClasspathDependency> { override fun directDependencies(): List<IClasspathDependency> {
val result = arrayListOf<IClasspathDependency>() val result = arrayListOf<IClasspathDependency>()

View file

@ -5,7 +5,8 @@ import com.beust.kobalt.maven.CompletedFuture
import org.apache.maven.model.Dependency import org.apache.maven.model.Dependency
import java.io.File import java.io.File
open class FileDependency(open val fileName: String) : IClasspathDependency, Comparable<FileDependency> { open class FileDependency(open val fileName: String, override val optional: Boolean = false)
: IClasspathDependency, Comparable<FileDependency> {
companion object { companion object {
val PREFIX_FILE: String = "file://" val PREFIX_FILE: String = "file://"
} }

View file

@ -24,7 +24,7 @@ class CheckVersions @Inject constructor(val depManager: DependencyManager,
cds.forEach { dep -> cds.forEach { dep ->
if (MavenId.isMavenId(dep.id)) { if (MavenId.isMavenId(dep.id)) {
try { try {
val latestDep = depManager.create(dep.shortId, project.directory) val latestDep = depManager.create(dep.shortId, false, project.directory)
val artifact = (latestDep as AetherDependency).artifact val artifact = (latestDep as AetherDependency).artifact
val versions = aether.resolveVersion(artifact) val versions = aether.resolveVersion(artifact)
val releases = versions?.versions?.filter { !it.toString().contains("SNAP")} val releases = versions?.versions?.filter { !it.toString().contains("SNAP")}