Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 31 additions & 23 deletions build-logic/src/main/kotlin/Osgi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.getByName
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.provideDelegate

fun Project.applyOsgi(vararg bndProperties: String) {
plugins.withId("org.jetbrains.kotlin.jvm") {
applyOsgi("jar", "osgiApi", bndProperties)
}
plugins.withId("org.jetbrains.kotlin.jvm") { applyOsgi("jar", "osgiApi", bndProperties) }
}

private fun Project.applyOsgi(
Expand All @@ -43,24 +42,29 @@ private fun Project.applyOsgi(
val osgi = project.sourceSets.create("osgi")
val osgiApi = project.configurations.getByName(osgiApiConfigurationName)

project.dependencies {
osgiApi(kotlinOsgi)
}
project.dependencies { osgiApi(kotlinOsgi) }

val jarTask = tasks.getByName<Jar>(jarTaskName)
val bundleExtension =
jarTask.extensions.findByType() ?: jarTask.extensions.create(
BundleTaskExtension.NAME,
BundleTaskExtension::class.java,
jarTask,
)
jarTask.extensions.findByType()
?: jarTask.extensions.create(
BundleTaskExtension.NAME,
BundleTaskExtension::class.java,
jarTask,
)
bundleExtension.run {
setClasspath(osgi.compileClasspath + sourceSets["main"].compileClasspath)
bnd(*bndProperties)
}
// Call the convention when the task has finished, to modify the jar to contain OSGi metadata.
jarTask.doLast {
bundleExtension.buildAction().execute(this)
val okhttpForceConfigurationCache: String by project
if (!okhttpForceConfigurationCache.toBoolean()) {
val buildAction = bundleExtension.buildAction()
jarTask.doLast { buildAction.execute(this) }
} else {
logger.warn(
"Skipping OSGi metadata generation for ${jarTask.name} because configuration caching is enabled and BND is not compatible.",
)
}
}

Expand Down Expand Up @@ -100,16 +104,11 @@ fun Project.applyOsgiMultiplatform(vararg bndProperties: String) {
target: String?,
) = "${jvmMainSourceSet.getTaskName(verb, target)}ForFakeMain"
}
extensions
.getByType(JavaPluginExtension::class.java)
.sourceSets
.add(mainSourceSet)
extensions.getByType(JavaPluginExtension::class.java).sourceSets.add(mainSourceSet)
tasks.named { it.endsWith("ForFakeMain") }.configureEach { onlyIf { false } }

val osgiApi = configurations.create("osgiApi")
dependencies {
osgiApi(kotlinOsgi)
}
dependencies { osgiApi(kotlinOsgi) }

// Call the convention when the task has finished, to modify the jar to contain OSGi metadata.
tasks.named<Jar>("jvmJar").configure {
Expand All @@ -120,12 +119,21 @@ fun Project.applyOsgiMultiplatform(vararg bndProperties: String) {
BundleTaskExtension::class.java,
this,
).apply {
classpath(osgiApi.artifacts)
val osgiApiArtifacts = osgiApi.artifacts
classpath(osgiApiArtifacts)
classpath(tasks.named("jvmMainClasses").map { it.outputs })
bnd(*bndProperties)
}
doLast {
bundleExtension.buildAction().execute(this)
val okhttpForceConfigurationCache: String by project
if (!okhttpForceConfigurationCache.toBoolean()) {
val buildAction = bundleExtension.buildAction()
doLast { buildAction.execute(this) }
} else {
// Configuration caching is enabled, and BND's buildAction is not compatible.
// We skip OSGi metadata generation for now when configuration caching is enabled.
logger.warn(
"Skipping OSGi metadata generation for :okhttp:jvmJar because configuration caching is enabled and BND is not compatible.",
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ tasks.withType<KotlinCompile>().configureEach {
friendPaths.from(friendsTestImplementation.incoming.artifactView { }.files)
}

val resolvableConfigurations = configurations.filter { it.isCanBeResolved }
tasks.register("downloadDependencies") {
description = "Download all dependencies to the Gradle cache"
doLast {
for (configuration in configurations) {
if (configuration.isCanBeResolved) {
configuration.files
}
for (configuration in resolvableConfigurations) {
configuration.files
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package okhttp3.buildsupport
import org.gradle.api.Project

val Project.platform: String
get() = findProperty("okhttp.platform")?.toString() ?: "jdk9"
get() = findProperty("okhttp.platform")?.toString() ?: "jdk9"

val Project.testJavaVersion: Int
get() = findProperty("test.java.version")?.toString()?.toInt() ?: 21
get() = findProperty("test.java.version")?.toString()?.toInt() ?: 21

val Project.androidBuild: Boolean
get() = findProperty("androidBuild")?.toString()?.toBoolean() ?: false
get() = findProperty("androidBuild")?.toString()?.toBoolean() ?: false

val Project.alpnBootVersion: String?
get() = findProperty("alpn.boot.version")?.toString()
get() = findProperty("alpn.boot.version")?.toString()
6 changes: 6 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
org.gradle.caching=true
org.gradle.parallel=true

# Blocked on BND
org.gradle.configuration-cache=false

android.useAndroidX=true
kotlin.mpp.applyDefaultHierarchyTemplate=false

Expand All @@ -11,6 +14,9 @@ containerTests=false
okhttpModuleTests=false
okhttpDokka=false

# When true BND is disabled to allow testing configuration-cache
okhttpForceConfigurationCache=false

org.gradle.jvmargs='-Dfile.encoding=UTF-8'

# AGP 9.0 Settings
Expand Down
4 changes: 4 additions & 0 deletions okhttp-osgi-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ dependencies {
}

val testJavaVersion = project.testJavaVersion
val okhttpForceConfigurationCache: String by project
tasks.withType<Test> {
dependsOn(copyOsgiTestDeployment)
onlyIf("Tests require JDK 17") {
testJavaVersion >= 17
}
onlyIf("OSGi tests are incompatible with configuration cache due to BND limitations") {
!okhttpForceConfigurationCache.toBoolean()
}
}
5 changes: 2 additions & 3 deletions okhttp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ val copyKotlinTemplates =
filteringCharset = Charsets.UTF_8.toString()

expand(
// Build & use okhttp3/internal/-InternalVersion.kt
"projectVersion" to project.version,
"projectVersion" to project.version.toString(),
)
}

Expand Down Expand Up @@ -284,7 +283,7 @@ tasks.named<Jar>("jvmJar").configure {
)
}

from(compileJavaModuleInfo.get().destinationDirectory) {
from(compileJavaModuleInfo.map { it.destinationDirectory }) {
into("META-INF/versions/9/")
}
}
Expand Down
Loading