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
12 changes: 10 additions & 2 deletions src/main/kotlin/nmcp/NmcpAggregationExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import gratatouille.GExtension
import javax.inject.Inject
import nmcp.internal.configureAttributes
import nmcp.internal.nmcpConsumerConfigurationName
import nmcp.internal.task.registerPublishTask
import nmcp.internal.task.registerPublishReleaseTask
import nmcp.internal.task.registerPublishSnapshotTask
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.file.ArchiveOperations
Expand Down Expand Up @@ -42,12 +43,19 @@ abstract class NmcpAggregationExtension(private val project: Project) {
})
}

project.registerPublishTask(
project.registerPublishReleaseTask(
taskName = "publishAggregationToCentralPortal",
inputFile = zipTaskProvider.flatMap { it.archiveFile },
artifactId = project.provider { "${project.name}" },
spec = spec
)
project.registerPublishSnapshotTask(
taskName = "publishAggregationToCentralSnapshots",
inputFile = zipTaskProvider.flatMap { it.archiveFile },
username = spec.username,
password = spec.password,
version = project.provider { "${project.version}" },
)
}

/**
Expand Down
20 changes: 16 additions & 4 deletions src/main/kotlin/nmcp/NmcpExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import gratatouille.GExtension
import gratatouille.capitalizeFirstLetter
import nmcp.internal.configureAttributes
import nmcp.internal.nmcpProducerConfigurationName
import nmcp.internal.task.registerPublishTask
import nmcp.internal.task.registerPublishReleaseTask
import nmcp.internal.task.registerPublishSnapshotTask
import nmcp.internal.withRequiredPlugin
import org.gradle.api.Action
import org.gradle.api.Project
Expand All @@ -15,8 +16,9 @@ import org.gradle.api.tasks.bundling.Zip
@GExtension(pluginId = "com.gradleup.nmcp")
open class NmcpExtension(private val project: Project) {
internal val spec = project.objects.newInstance(CentralPortalOptions::class.java)
// Lifecycle task to publish all the publications in the given project
// Lifecycle tasks to publish all the publications in the given project
private val publishAllPublicationsToCentralPortal = project.tasks.register("publishAllPublicationsToCentralPortal")
private val publishAllPublicationsToCentralSnapshots = project.tasks.register("publishAllPublicationsToCentralSnapshots")

init {
project.configurations.create(nmcpProducerConfigurationName) {
Expand Down Expand Up @@ -86,15 +88,25 @@ open class NmcpExtension(private val project: Project) {
} else {
project.provider { "${project.name}"}
}
val publishTaskProvider = project.registerPublishTask(
val publishRelease = project.registerPublishReleaseTask(
taskName = "publish${capitalized}PublicationToCentralPortal",
inputFile = zipTaskProvider.flatMap { it.archiveFile },
artifactId = artifactId,
spec = spec
)
val publishSnapshots = project.registerPublishSnapshotTask(
taskName = "publish${capitalized}PublicationToCentralSnapshots",
inputFile = zipTaskProvider.flatMap { it.archiveFile },
username = spec.username,
password = spec.password,
version = project.provider { "${project.version}" },
)

publishAllPublicationsToCentralSnapshots.configure {
it.dependsOn(publishSnapshots)
}
publishAllPublicationsToCentralPortal.configure {
it.dependsOn((publishTaskProvider))
it.dependsOn((publishRelease))
}

project.artifacts.add(nmcpProducerConfigurationName, zipTaskProvider)
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/nmcp/internal/task/okhttp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package nmcp.internal.task

import java.time.Duration
import okhttp3.OkHttpClient

internal val client = OkHttpClient.Builder()
.connectTimeout(Duration.ofSeconds(30))
.writeTimeout(Duration.ofSeconds(30))
.readTimeout(Duration.ofSeconds(60))
.build()
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import gratatouille.GInputFile
import gratatouille.GLogger
import gratatouille.GTask
import java.net.SocketTimeoutException
import java.time.Duration
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import nmcp.CentralPortalOptions
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
Expand All @@ -27,7 +25,7 @@ import kotlin.time.TimeSource.Monotonic.markNow


@GTask(pure = false)
fun publish(
fun publishRelease(
logger: GLogger,
username: String?,
password: String?,
Expand Down Expand Up @@ -144,12 +142,6 @@ private data object PUBLISHED : Status
// A deployment has encountered an error
private class FAILED(val error: String) : Status

private val client = OkHttpClient.Builder()
.connectTimeout(Duration.ofSeconds(30))
.writeTimeout(Duration.ofSeconds(30))
.readTimeout(Duration.ofSeconds(60))
.build()

private fun verifyStatus(
deploymentId: String,
baseUrl: String,
Expand Down Expand Up @@ -196,14 +188,14 @@ private fun verifyStatus(
}
}

internal fun Project.registerPublishTask(
internal fun Project.registerPublishReleaseTask(
taskName: String,
inputFile: Provider<RegularFile>,
artifactId: Provider<String>,
spec: CentralPortalOptions
): TaskProvider<PublishTask> {
): TaskProvider<PublishReleaseTask> {
val defaultPublicationName = artifactId.map { "${project.group}:${it}:${project.version}.zip" }
return registerPublishTask(
return registerPublishReleaseTask(
taskName = taskName,
inputFile = inputFile,
username = spec.username,
Expand Down
61 changes: 61 additions & 0 deletions src/main/kotlin/nmcp/internal/task/publishSnapshot.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package nmcp.internal.task

import gratatouille.GInputFile
import gratatouille.GLogger
import gratatouille.GTask
import java.util.zip.ZipInputStream
import okhttp3.Credentials
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okio.buffer
import okio.source

@GTask(pure = false)
fun publishSnapshot(
logger: GLogger,
username: String?,
password: String?,
version: String,
inputFile: GInputFile,
) {
check(!username.isNullOrBlank()) {
"Ncmp: username is missing"
}
check(!password.isNullOrBlank()) {
"Ncmp: password is missing"
}
check(version.endsWith("-SNAPSHOT")) {
"Ncmp: Cannot publish snapshot version '$version' without -SNAPSHOT suffix"
}

val okHttpClient = client.newBuilder()
.addInterceptor { chain ->
val builder = chain.request().newBuilder()
builder.addHeader("Authorization", Credentials.basic(username, password))
builder.addHeader("Accept", "application/json")
builder.addHeader("Content-Type", "application/json")
builder.addHeader("User-Agent", "vespene")
chain.proceed(builder.build())
}.build()

ZipInputStream(inputFile.inputStream()).use {
while (true) {
val entry = it.nextEntry ?: break
if (entry.isDirectory) continue
val relativePath = entry.name
logger.lifecycle("Nmcp: uploading $relativePath...")

val url = "https://central.sonatype.com/repository/maven-snapshots/$relativePath"
val request = Request.Builder()
.put(it.source().buffer().readByteArray().toRequestBody("application/octet-stream".toMediaType()))
.url(url)
.build()

val uploadResponse = okHttpClient.newCall(request).execute()
check(uploadResponse.isSuccessful) {
"Cannot put $url:\n${uploadResponse.body?.string()}"
}
}
}
}
9 changes: 7 additions & 2 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ Integration tests that check the contents of the zip bundle. To run from the roo
./gradlew -p tests/kmp build
```

* `tests/jvm` can be used to deploy to the real Central Portal.
* `tests/jvm` can be used to deploy to the real Central Portal:

```shell
./gradlew -p tests/jvm publishAggregationToCentralPortal
./gradlew -p tests/jvm publishAggregationToCentralSnapshots
Comment on lines +13 to +14
Copy link
Contributor

Choose a reason for hiding this comment

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

Should there be a single task that decides on snapshot/release depending on the project version?

E.g. if project.version.toString() ends with -SNAPSHOT, then it should publish to snaphots. Otherwise it should publish to releases.
WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea maybe that'd be better.

But I also kind of like the separation of intents. Having separate task names and separate CI workflows makes for a clear separation. Plus historically those were 2 different task names so there is some brain muscle attached. Not saying we shouldn't change it of course but I'm not 100% convinced it would help yet. Did you have any specific use cases in mind?

Copy link
Contributor

Choose a reason for hiding this comment

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

The reasoning I have in mind is that the action the same: "push publication to the repository".
I don't understand why should I push it differently depending on the project version.
It is pretty much like you don't have jar vs jarSnapshot depending on the project version. You always have a single jar task which builds the jar according to the current project version.

I'm fine if there are two independent technical task for the implementation details, however, in that case it would be nice to have an out of the box wrapper task which would depend on one of them as the project version requires.


Historically, I use "autosnapshot" project version: build script automatically adds -SNAPSHOT to the version unless there's -Prelease flag.

So it sounds reasonable to have ./gradlew publishAllPublicationsToCentralPortal and ./gradlew -Prelease publishAllPublicationsToCentralPortal. In other words: -Prelease selects the project version while the rest ("publish to central portal") being intact.

Copy link
Member Author

@martinbonnin martinbonnin May 13, 2025

Choose a reason for hiding this comment

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

It's not the same repository though. The url to consume and the immutability guarantees are not the same.

I get where you're coming from with the -Prelease flag, though. That's interesting and it's currently a pain in my workflows to make a commit specifically to drop the -SNAPSHOT... Having something like this would definitely make it easier.

Give me a few weeks so that I can try it out in a pet project and I'll report back? I'll open an issue so we keep track of this. (edit: here)

Copy link
Member Author

Choose a reason for hiding this comment

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

@vlsi do you have a sample project using -Prelease I could take inspiration from?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

@vlsi I ended up adopting the "autosnapshot" versioning in a couple of projects, it's nice! Thanks for pointing me to this 🙏

That being, said, I'm not 100% convinced that we need a wrapper task just yet. My snapshot and release workflows are usually different and having different task names doesn't hurt me too much. If anything, they act as an additional explicit check that I'm publishing in the correct location.

I might get there ultimately but since it's always easier to add than remove, I prefer keeping our options open there and not committing in that direction.

```
* `tests/kmp` uses a mockserver to verify the contents uploaded without having to do a real upload every time.

Open `tests/kmp` or `tests/jvm` in IntelliJ for development
Open `tests/kmp` or `tests/jvm` in IntelliJ for development
5 changes: 5 additions & 0 deletions tests/jvm/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ extensions.getByType<NmcpAggregationExtension>().apply {
}
}

/**
* A task that checks that the zip file generated by the aggregation plugin contains the expected files.
*
* This task assumes that no signing key is present (GPG_PRIVATE_KEY must be empty). If it fails, double check your environment variables.
*/
val checkZip = tasks.register("checkZip") {
inputs.file(tasks.named("zipAggregation").flatMap { (it as Zip).archiveFile })

Expand Down