[kpm] Embed protobuf runtime into kgp-idea-proto runtime jar

^KT-52568 Verification Pending
This commit is contained in:
sebastian.sellmair
2022-06-15 14:18:11 +02:00
committed by Space
parent 0eb4551e94
commit 514ea01175
7 changed files with 98 additions and 31 deletions
@@ -1,3 +1,7 @@
@file:Suppress("HasPlatformType")
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
plugins {
kotlin("jvm")
}
@@ -8,18 +12,32 @@ kotlin {
}
}
publish()
javadocJar()
sourcesJar()
val shadows by configurations.creating {
isCanBeConsumed = false
isCanBeResolved = false
isTransitive = false
configurations.getByName("compileOnly").extendsFrom(this)
configurations.getByName("testImplementation").extendsFrom(this)
}
val shadowsRuntime by configurations.creating {
isCanBeConsumed = false
isCanBeResolved = true
isTransitive = false
extendsFrom(shadows)
attributes.attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME))
attributes.attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.LIBRARY))
}
dependencies {
api(project(":kotlin-gradle-plugin-idea"))
implementation("com.google.protobuf:protobuf-java:3.19.4")
implementation("com.google.protobuf:protobuf-kotlin:3.19.4")
shadows("com.google.protobuf:protobuf-java:3.19.4")
shadows("com.google.protobuf:protobuf-kotlin:3.19.4")
testImplementation(project(":kotlin-test:kotlin-test-junit"))
testImplementation(testFixtures(project(":kotlin-gradle-plugin-idea")))
}
configureKotlinCompileTasksGradleCompatibility()
sourceSets.main.configure {
@@ -27,6 +45,17 @@ sourceSets.main.configure {
java.srcDir("src/generated/kotlin")
}
javadocJar()
sourcesJar()
runtimeJar(tasks.register<ShadowJar>("shadowJar")) {
exclude("**/*.proto")
from(mainSourceSet.output)
configurations = listOf(shadowsRuntime)
relocate("com.google.protobuf", "org.jetbrains.kotlin.kpm.idea.proto.com.google.protobuf")
}
publish()
tasks.register<Exec>("protoc") {
val protoSources = file("src/main/proto")
val javaOutput = file("src/generated/java/")
@@ -0,0 +1,27 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle
import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.testbase.*
import org.junit.jupiter.api.DisplayName
@MppGradlePluginTests
@GradleTestVersions(minVersion = TestVersions.Gradle.MIN_SUPPORTED_KPM)
class BuildIdeaKpmProjectModelIT : KGPBaseTest() {
@GradleTest
@DisplayName("Check 'buildIdeaKpmProjectModel' creates expected files")
fun `test - simple kpm project`(gradleVersion: GradleVersion) {
project("kpm-simple", gradleVersion) {
build("buildIdeaKpmProjectModel") {
assertFileInProjectExists("build/IdeaKpmProject/model.txt")
assertFileInProjectExists("build/IdeaKpmProject/model.java.bin")
assertFileInProjectExists("build/IdeaKpmProject/model.proto.bin")
}
}
}
}
@@ -20,6 +20,7 @@ interface TestVersions {
const val G_7_3 = "7.3.3"
const val G_7_4 = "7.4.2"
const val MIN_SUPPORTED = minSupportedGradleVersion
const val MIN_SUPPORTED_KPM = G_7_0
const val MAX_SUPPORTED = G_7_0
}
@@ -51,4 +52,4 @@ interface TestVersions {
const val MAX_SUPPORTED = "7.0.4" // AGP_70 - Update once Gradle MAX_SUPPORTED version will be bumped to 7.2+
}
}
}
}
@@ -0,0 +1,18 @@
import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.*
import org.jetbrains.kotlin.project.model.*
plugins {
kotlin("multiplatform.pm20")
}
repositories {
mavenLocal()
mavenCentral()
}
configure<KotlinPm20ProjectExtension> {
main {
jvm
}
}
@@ -0,0 +1,4 @@
package testProject.kpmSimple
class Foo {
}
@@ -37,7 +37,6 @@ internal open class IdeaKpmBuildProjectModelTask : DefaultTask() {
outputs.upToDateWhen { false }
}
@OptIn(ExperimentalTime::class)
@TaskAction
protected fun buildIdeaKpmProjectModel() {
outputDirectory.mkdirs()
@@ -47,32 +46,20 @@ internal open class IdeaKpmBuildProjectModelTask : DefaultTask() {
val textFile = outputDirectory.resolve("model.txt")
textFile.writeText(model.toString())
val javaIoSerializableDuration = measureTime {
val javaIoSerializableBinaryFile = outputDirectory.resolve("model.java.bin")
javaIoSerializableBinaryFile.writeBytes(ByteArrayOutputStream().use { byteArrayOutputStream ->
ObjectOutputStream(byteArrayOutputStream).use { objectOutputStream -> objectOutputStream.writeObject(model) }
byteArrayOutputStream.toByteArray()
})
val javaIoSerializableBinaryFile = outputDirectory.resolve("model.java.bin")
javaIoSerializableBinaryFile.writeBytes(ByteArrayOutputStream().use { byteArrayOutputStream ->
ObjectOutputStream(byteArrayOutputStream).use { objectOutputStream -> objectOutputStream.writeObject(model) }
byteArrayOutputStream.toByteArray()
})
val protoBinaryFile = outputDirectory.resolve("model.proto.bin")
if (protoBinaryFile.exists()) protoBinaryFile.delete()
protoBinaryFile.outputStream().use { stream ->
model.writeTo(stream, serializationContext)
}
val protoDuration = measureTime {
val protoBinaryFile = outputDirectory.resolve("model.proto.bin")
if (protoBinaryFile.exists()) protoBinaryFile.delete()
protoBinaryFile.outputStream().use { stream ->
model.writeTo(stream, serializationContext)
}
}
val gsonDuration = measureTime {
val jsonFile = outputDirectory.resolve("model.json")
jsonFile.writeText(GsonBuilder().setLenient().setPrettyPrinting().create().toJson(model))
}
logger.quiet(
"java.io.Serializable took $javaIoSerializableDuration\n" +
"protobuf took $protoDuration\n" +
"Gson took $gsonDuration"
)
val jsonFile = outputDirectory.resolve("model.json")
jsonFile.writeText(GsonBuilder().setLenient().setPrettyPrinting().create().toJson(model))
}
companion object {