Skip to content
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

feat(jpms): add module-info.java descriptors #556

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
60 changes: 51 additions & 9 deletions api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,32 +1,74 @@
description = "reactive-streams"
def jdkFlow = false
def isJdk9OrGreater = false
try {
Class.forName("java.util.concurrent.Flow")
jdkFlow = true
isJdk9OrGreater = true
} catch (ClassNotFoundException cnfe) {

}

sourceSets {
main {
java {
if (jdkFlow)
srcDirs = ['src/main/java', 'src/main/java9']
else
srcDirs = ['src/main/java']
srcDirs = ['src/main/java']
}
}
if (isJdk9OrGreater) java9 {
java {
srcDirs = ['src/main/java', 'src/main/java9']
}
}
Comment on lines +16 to +20
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now a second source set, instead of an additional directory in the main source set. This allows a second compile task to target a different JVM bytecode level.

}

configurations {
mrjarArtifact
}
Comment on lines +23 to 25
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MRJAR artifact is exported from the project, so that tests can depend on it. This is important to accurately test JPMS, otherwise Gradle will put these classes on the classpath downstream, which runs tests without modular encapsulation.


jar {
if (isJdk9OrGreater) into('META-INF/versions/9') {
from(sourceSets.java9.output) {
include '**/FlowAdapters*'
include '**/module-info.class'
}
}
Comment on lines +28 to +33
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amends the JAR with the META-INF/versions/9 root, from the output of the sourceSets.java9 compile. include is used here to avoid duplicating classes which reside in the root of the module, at JDK 6, which are identical anyway.


bnd ('Bundle-Name': 'reactive-streams-jvm',
'Bundle-Vendor': 'Reactive Streams SIG',
'Bundle-Description': 'Reactive Streams API',
'Bundle-DocURL': 'http://reactive-streams.org',
'Bundle-Version': project.version,
'Export-Package': 'org.reactivestreams.*',
'Automatic-Module-Name': 'org.reactivestreams',
'Bundle-SymbolicName': 'org.reactivestreams.reactive-streams'
'Export-Package': 'org.reactivestreams.*,!META-INF.*',
'Multi-Release': true,
'Bundle-SymbolicName': 'org.reactivestreams.reactive-streams',
'-fixupmessages': '^Classes found in the wrong directory: .*'
Comment on lines -27 to +43
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Export-Package has added !META-INF.* to avoid processing the Java 9 class root; Multi-Release was added, to replace Automatic-Module-Name. -fixupmessages avoids a BND warning about classes in the Java 9 root.

)
}

artifacts { mrjarArtifact jar }

compileJava {
group = "build"
description = "Compile Java 1.6 classes"
}

if (isJdk9OrGreater) {
compileJava9Java {
group = "build"
description = "Compile Java 9 classes for MR JAR"

sourceCompatibility = 9
targetCompatibility = 9
}
Comment on lines +54 to +61
Copy link
Author

@sgammon sgammon Mar 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compile task; in this PR, the task is conditional based on whether the build-time JVM is at least Java 9. The existing build behavior is preserved in this way.

In the other PR, this is addressed by Gradle Toolchains, so the build task is not conditional and is always expected to run at JDK 9 or greater during build time.


tasks.register('validateModularJar', Exec) {
dependsOn jar
inputs.file jar.outputs.files.asPath

executable = "jar"
args = ['--describe-module', '--file', jar.outputs.files.asPath]
}

tasks.check {
finalizedBy validateModularJar
}
Comment on lines +63 to +73
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New task: validateModularJar runs as part of check, and it executes:

jar --describe-module --file api/build/libs/reactive-streams-1.0.4.jar

The expected output is an exit code of 0. On the command line this shows:

➜  reactive-streams-jvm git:(chore/build-updates) jar --describe-module --file api/build/libs/reactive-streams-1.0.4.jar
releases: 9

No root module descriptor, specify --release
➜  reactive-streams-jvm git:(chore/build-updates) jar --describe-module --file api/build/libs/reactive-streams-1.0.4.jar --release 9
releases: 9

org.reactivestreams jar:file:///Volumes/VAULTROOM/reactive-streams-jvm/api/build/libs/reactive-streams-1.0.4.jar!/META-INF/versions/9/module-info.class
exports org.reactivestreams
requires java.base mandated

}
10 changes: 10 additions & 0 deletions api/src/main/java9/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/***************************************************
* Licensed under MIT No Attribution (SPDX: MIT-0) *
***************************************************/

/**
* Reactive Streams
*/
module org.reactivestreams {
exports org.reactivestreams;
Comment on lines +8 to +9
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The module name matches the previous Automatic-Module-Name. The exported package is the only package in the JAR, so export-related risk is minimal. If it needs to be an open module let me know.

}
69 changes: 37 additions & 32 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ buildscript {
}
}

def sonatypeRepo = 'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
def sonatypeSnapshots = 'https://oss.sonatype.org/content/repositories/snapshots/'

def distributionRepository = properties["distributionRepository"] ?: sonatypeRepo
def snapshotsRepository = properties["snapshotsRepository"] ?: sonatypeSnapshots
Comment on lines +14 to +18
Copy link
Author

@sgammon sgammon Mar 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I included a small unrelated change which allows parameterization of the deploy repositories. This will allow me to easily test integration of this JPMS support with downstream libraries. It should be completely inert to this codebase.

The username and password for releases are still required, but only when the deployment repo is Sonatype.


subprojects {
apply plugin: "java-library"
apply plugin: 'biz.aQute.bnd.builder'

group = "org.reactivestreams"
version = "1.0.4"

sourceCompatibility = 1.6
targetCompatibility = 1.6
tasks.compileJava {
sourceCompatibility = 1.6
targetCompatibility = 1.6
}

tasks.withType(JavaCompile) {
configure(options) {
Expand Down Expand Up @@ -53,14 +61,15 @@ subprojects {
"reactive-streams-tck",
"reactive-streams-tck-flow",
"reactive-streams-examples"]) {
apply plugin: "maven"
apply plugin: "signing"
apply plugin: "publishing"
apply plugin: "maven-publish"
Comment on lines -56 to 66
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small build cleanup because Gradle 7.x eliminates the maven plugin in favor of publishing. Other than DSL updates, the logic is unchanged.


signing {
sign configurations.archives
required { gradle.taskGraph.hasTask(uploadArchives) }
required { gradle.taskGraph.hasTask("publish") }
}

task sourcesJar(type: Jar) {
classifier "sources"
from sourceSets.main.allSource
Expand All @@ -79,41 +88,37 @@ subprojects {
}
repositories {
mavenLocal()

gradle.taskGraph.whenReady { taskGraph ->
if (taskGraph.hasTask("publish")) {
def userProp = "sonatypeOssUsername"
def passwordProp = "sonatypeOssPassword"
def user = project.properties[userProp]
def password = project.properties[passwordProp]

if ((user == null || password == null) && distributionRepository == sonatypeRepo) {
throw new InvalidUserDataException(
"Cannot perform $uploadArchives.path due to missing credentials.\n" +
"Run with command line args `-P$userProp=«username» -P$passwordProp=«password»` or add these properties to $gradle.gradleUserHomeDir/gradle.properties.\n")
}

repository(url: distributionRepository) {
authentication(userName: user, password: password)
}
if (sonatypeSnapshots != "") {
snapshotRepository(url: snapshotsRepository) {
authentication(userName: user, password: password)
}
}
}
}
}
}

artifacts {
archives sourcesJar, javadocJar
}

uploadArchives {
repositories {
mavenDeployer {
gradle.taskGraph.whenReady { taskGraph ->
if (taskGraph.hasTask(uploadArchives)) {
def userProp = "sonatypeOssUsername"
def passwordProp = "sonatypeOssPassword"
def user = project.properties[userProp]
def password = project.properties[passwordProp]

if (user == null || password == null) {
throw new InvalidUserDataException(
"Cannot perform $uploadArchives.path due to missing credentials.\n" +
"Run with command line args `-P$userProp=«username» -P$passwordProp=«password»` or add these properties to $gradle.gradleUserHomeDir/gradle.properties.\n")
}

repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
authentication(userName: user, password: password)
}
snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
authentication(userName: user, password: password)
}
}
}
}
}
}

tasks.withType(Upload) {
repositories.withType(MavenResolver) {
it.beforeDeployment { signing.signPom(it) }
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.4-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading