-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Add multi-release jars to enable Java Modules #8767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b72276a
ab042de
879bf1a
69df152
ac24e72
ce79429
fe7e549
a828dc7
14e6e16
b268038
71057d1
f1a2f03
c56f1e6
aca5013
8b49e10
0973e8d
2cf11d3
1ba44f1
7eef165
6a3a64b
2b452f7
220529b
c2bb349
1488a1b
b028111
fb8636d
45ec46b
d9d121f
2398c5a
c6bf693
16cb3a2
17f6354
473ce2c
9a6f004
cb0dedd
37161cd
d9c1199
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -214,6 +214,36 @@ $ ./gradlew okcurl:nativeImage | |
| $ ./okcurl/build/graal/okcurl https://httpbin.org/get | ||
| ``` | ||
|
|
||
| Java Modules | ||
| ------------ | ||
|
|
||
| OkHttp (5.3+) implements Java 9 Modules. | ||
|
|
||
| With this in place Java builds should fail if apps attempt to use internal packages. | ||
|
|
||
| ``` | ||
| error: package okhttp3.internal.platform is not visible | ||
| okhttp3.internal.platform.Platform.get(); | ||
| ^ | ||
| (package okhttp3.internal.platform is declared in module okhttp3, | ||
| which does not export it to module i.am.bad.and.i.should.feel.bad) | ||
|
Comment on lines
+225
to
+229
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @swankjesse I guess this is the main point?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also added a CI test that jlink works, see https://www.baeldung.com/jlink
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @swankjesse do you need more convincing?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is good behavior. I wouldn’t use |
||
| ``` | ||
|
|
||
| The stable public API is based on the list of defined modules: | ||
|
|
||
| - okhttp3 | ||
| - okhttp3.brotli | ||
| - okhttp3.coroutines | ||
| - okhttp3.dnsoverhttps | ||
| - okhttp3.java.net.cookiejar | ||
| - okhttp3.logging | ||
| - okhttp3.sse | ||
| - okhttp3.tls | ||
| - okhttp3.urlconnection | ||
| - mockwebserver3 | ||
| - mockwebserver3.junit4 | ||
| - mockwebserver3.junit5 | ||
|
|
||
| License | ||
| ------- | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,9 @@ | ||
| rootProject.name = "okhttp-buildSrc" | ||
|
|
||
| dependencyResolutionManagement { | ||
| versionCatalogs { | ||
| create("libs") { | ||
| from(files("../gradle/libs.versions.toml")) | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * Copyright (C) 2025 Square, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import me.champeau.mrjar.MultiReleaseExtension | ||
| import org.gradle.api.Project | ||
| import org.gradle.api.tasks.compile.JavaCompile | ||
| import org.gradle.kotlin.dsl.configure | ||
| import org.gradle.kotlin.dsl.named | ||
| import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile | ||
|
|
||
| fun Project.applyJavaModules( | ||
| moduleName: String, | ||
| defaultVersion: Int = 8, | ||
| javaModuleVersion: Int = 9, | ||
| enableValidation: Boolean = true, | ||
| ) { | ||
| plugins.apply("me.champeau.mrjar") | ||
|
yschimke marked this conversation as resolved.
|
||
|
|
||
| configure<MultiReleaseExtension> { | ||
| targetVersions(defaultVersion, javaModuleVersion) | ||
| } | ||
|
|
||
| tasks.named<JavaCompile>("compileJava9Java").configure { | ||
| val compileKotlinTask = tasks.getByName("compileKotlin") as KotlinJvmCompile | ||
| dependsOn(compileKotlinTask) | ||
|
|
||
| if (enableValidation) { | ||
| compileKotlinTask.source(file("src/main/java9")) | ||
| } | ||
|
|
||
| // Ignore warnings about using 'requires transitive' on automatic modules. | ||
| // not needed when compiling with recent JDKs, e.g. 17 | ||
| options.compilerArgs.add("-Xlint:-requires-transitive-automatic") | ||
|
yschimke marked this conversation as resolved.
|
||
|
|
||
| // Patch the compileKotlinJvm output classes into the compilation so exporting packages works correctly. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very helpful comment! |
||
| options.compilerArgs.addAll( | ||
| listOf( | ||
| "--patch-module", | ||
| "$moduleName=${compileKotlinTask.destinationDirectory.get().asFile}", | ||
| ), | ||
| ) | ||
|
|
||
| classpath = compileKotlinTask.libraries | ||
| modularity.inferModulePath.set(true) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| @SuppressWarnings("module") | ||
| module okhttp3.mockwebserver { | ||
| requires okhttp3; | ||
| exports okhttp3.mockwebserver; | ||
| requires java.logging; | ||
| } | ||
|
yschimke marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| @SuppressWarnings("module") | ||
| module mockwebserver3.junit4 { | ||
| requires okhttp3; | ||
| exports mockwebserver3.junit4; | ||
| requires java.logging; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| @SuppressWarnings("module") | ||
| module mockwebserver3.junit5 { | ||
| requires okhttp3; | ||
| opens mockwebserver3.junit5.internal; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| @SuppressWarnings("module") | ||
| module mockwebserver3 { | ||
| requires okhttp3; | ||
| exports mockwebserver3; | ||
| requires java.logging; | ||
| } |
|
yschimke marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| plugins { | ||
| id("java") | ||
| id("application") | ||
| id("com.github.iherasymenko.jlink") version "0.7" | ||
| id("org.gradlex.extra-java-module-info") version "1.13" | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(projects.okhttp) | ||
| implementation(projects.loggingInterceptor) | ||
|
|
||
| testImplementation(projects.okhttp) | ||
| testImplementation(projects.loggingInterceptor) | ||
| testImplementation(projects.mockwebserver3) | ||
| testImplementation(projects.mockwebserver3Junit5) | ||
|
|
||
| testImplementation(libs.junit.jupiter.api) | ||
| testRuntimeOnly(libs.junit.jupiter.engine) | ||
| testRuntimeOnly(libs.junit.platform.launcher) | ||
| } | ||
|
|
||
| application { | ||
| mainClass = "okhttp3.modules.Main" | ||
|
yschimke marked this conversation as resolved.
|
||
| mainModule = "okhttp3.modules" | ||
| } | ||
|
|
||
| jlinkApplication { | ||
| stripDebug = true | ||
| stripJavaDebugAttributes = true | ||
| compress.set("zip-9") | ||
| addModules.addAll("jdk.crypto.ec", "java.logging") | ||
| vm.set("server") | ||
| } | ||
|
|
||
| extraJavaModuleInfo { | ||
| module("org.jetbrains:annotations", "org.jetbrains.annotations") { | ||
| exportAllPackages() | ||
| } | ||
| module("com.squareup.okio:okio-jvm", "okio") { | ||
| exportAllPackages() | ||
| requires("kotlin.stdlib") | ||
| requires("java.logging") | ||
| } | ||
| module("com.squareup.okio:okio", "okio") { | ||
| exportAllPackages() | ||
| } | ||
| } | ||
|
|
||
| val testJavaVersion = System.getProperty("test.java.version", "21").toInt() | ||
|
|
||
| tasks.withType<Test> { | ||
| useJUnitPlatform() | ||
|
|
||
| enabled = testJavaVersion > 8 | ||
|
|
||
| javaLauncher.set(javaToolchains.launcherFor { | ||
| languageVersion.set(JavaLanguageVersion.of(testJavaVersion)) | ||
| }) | ||
| } | ||
|
|
||
| java { | ||
| sourceCompatibility = JavaVersion.VERSION_11 | ||
| targetCompatibility = JavaVersion.VERSION_11 | ||
| toolchain { | ||
| languageVersion.set(JavaLanguageVersion.of(21)) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| @SuppressWarnings("module") | ||
| module okhttp3.modules { | ||
| requires okhttp3; | ||
| requires okhttp3.logging; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what the heck is okhttp3.logging ?!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or maybe favoured Stephen's comment over yours?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep yep, it’s the package name. I missed that we gave that module two different names. |
||
| requires jdk.crypto.ec; | ||
| exports okhttp3.modules; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * Copyright (C) 2025 Block, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package okhttp3.modules; | ||
|
|
||
| import okhttp3.Call; | ||
| import okhttp3.HttpUrl; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) throws IOException { | ||
| Call call = OkHttpCaller.callOkHttp(HttpUrl.get("https://square.com/robots.txt")); | ||
|
|
||
| System.out.println(call.execute().body().string()); | ||
| } | ||
| } |

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What the heck is OkHttp 5.3 ?! We have to ship 5.2 first!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooops, can fix.