Build: Convert protobuf to includable build with maven publication

#KT-29202
This commit is contained in:
Vyacheslav Gerasimov
2019-01-14 20:52:43 +03:00
parent 6a3e5c4c94
commit c7ab69829e
8 changed files with 100 additions and 58 deletions
+1
View File
@@ -4,6 +4,7 @@
<w>cidr</w>
<w>foldable</w>
<w>instrumentator</w>
<w>protobuf</w>
<w>redirector</w>
</words>
</dictionary>
+2 -9
View File
@@ -64,15 +64,8 @@ fun DependencyHandler.projectRuntimeJar(name: String): ProjectDependency = proje
fun DependencyHandler.projectArchives(name: String): ProjectDependency = project(name, configuration = "archives")
fun DependencyHandler.projectClasses(name: String): ProjectDependency = project(name, configuration = "classes-dirs")
val protobufLiteProject = ":custom-dependencies:protobuf:protobuf-lite"
val protobufRelocatedProject = ":custom-dependencies:protobuf:protobuf-relocated"
fun DependencyHandler.protobufLite(): ProjectDependency =
project(protobufLiteProject, configuration = "default").apply { isTransitive = false }
val protobufLiteTask = "$protobufLiteProject:prepare"
fun DependencyHandler.protobufFull(): ProjectDependency =
project(protobufRelocatedProject, configuration = "default").apply { isTransitive = false }
fun Project.protobufLite(): String = "org.jetbrains.kotlin:protobuf-lite:" + findProperty("versions.protobuf-java") as String
fun Project.protobufFull(): String = "org.jetbrains.kotlin:protobuf-relocated:" + findProperty("versions.protobuf-java") as String
fun File.matchMaybeVersionedArtifact(baseName: String) = name.matches(baseName.toMaybeVersionedJarRegex())
-5
View File
@@ -20,11 +20,6 @@ sourceSets {
}
tasks.withType<JavaCompile> {
dependsOn(protobufLiteTask)
sourceCompatibility = "1.6"
targetCompatibility = "1.6"
}
tasks.withType<KotlinCompile> {
dependsOn(protobufLiteTask)
}
@@ -0,0 +1,7 @@
val protobufVersion by extra("2.6.1")
allprojects {
group = "org.jetbrains.kotlin"
version = protobufVersion
}
@@ -6,21 +6,23 @@ import java.util.jar.JarEntry
import java.util.jar.JarFile
import java.util.zip.ZipOutputStream
plugins {
base
`maven-publish`
}
val relocatedProtobuf by configurations.creating
val relocatedProtobufSources by configurations.creating
val resultsCfg = configurations.create("default")
val protobufVersion = rootProject.extra["versions.protobuf-java"]
val protobufVersion: String by rootProject.extra
val protobufJarPrefix = "protobuf-$protobufVersion"
val outputJarPath = "$buildDir/libs/$protobufJarPrefix-lite.jar"
dependencies {
relocatedProtobuf(project(":custom-dependencies:protobuf:protobuf-relocated", configuration = "default"))
relocatedProtobufSources(project(":custom-dependencies:protobuf:protobuf-relocated", configuration = "sources"))
relocatedProtobuf(project(":protobuf-relocated"))
}
task("prepare") {
val prepare by tasks.creating {
inputs.files(relocatedProtobuf) // this also adds a dependency
outputs.file(outputJarPath)
doFirst {
@@ -41,7 +43,11 @@ task("prepare") {
return result
}
val allFiles = loadAllFromJar(relocatedProtobuf.singleFile)
val mainJar = relocatedProtobuf.resolvedConfiguration.resolvedArtifacts.single {
it.name == "protobuf-relocated" && it.classifier == null
}.file
val allFiles = loadAllFromJar(mainJar)
val keepClasses = arrayListOf<String>()
@@ -80,18 +86,38 @@ task("prepare") {
}
}
}
addArtifact("archives", this, outputs.files.singleFile) {
classifier = ""
}
addArtifact(resultsCfg, this, outputs.files.singleFile) {
classifier = ""
}
}
val clean by task<Delete> {
delete(buildDir)
val mainArtifact = artifacts.add(
"default",
provider {
prepare.outputs.files.singleFile
}
) {
builtBy(prepare)
classifier = ""
}
artifacts.add("archives", relocatedProtobufSources.files.single()) {
val sourcesArtifact = artifacts.add(
"default",
provider {
relocatedProtobuf.resolvedConfiguration.resolvedArtifacts.single { it.name == "protobuf-relocated" && it.classifier == "sources" }.file
}
) {
classifier = "sources"
}
publishing {
publications {
create<MavenPublication>("maven") {
artifact(mainArtifact)
artifact(sourcesArtifact)
}
}
repositories {
maven {
url = uri("${rootProject.buildDir}/internal/repo")
}
}
}
@@ -2,61 +2,79 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import java.io.File
plugins {
base
id("pill-configurable")
`java-base`
`maven-publish`
id("com.github.johnrengelman.shadow") version "4.0.3" apply false
}
repositories {
jcenter()
}
val baseProtobuf by configurations.creating
val baseProtobufSources by configurations.creating
val resultsCfg = configurations.getByName("default")
val resultsSourcesCfg = configurations.create("sources")
val protobufVersion = rootProject.extra["versions.protobuf-java"] as String
val protobufVersion: String by rootProject.extra
val protobufJarPrefix = "protobuf-$protobufVersion"
val renamedSources = "$buildDir/renamedSrc/"
val outputJarsPath = "$buildDir/libs"
val artifactBaseName = "protobuf-java-relocated"
pill {
libraryPath = File(outputJarsPath)
}
setProperty("archivesBaseName", "$artifactBaseName-$protobufVersion")
dependencies {
baseProtobuf("com.google.protobuf:protobuf-java:$protobufVersion")
baseProtobufSources("com.google.protobuf:protobuf-java:$protobufVersion:sources")
}
val prepare by task<ShadowJar> {
val prepare = task<ShadowJar>("prepare") {
destinationDir = File(outputJarsPath)
baseName = artifactBaseName
version = protobufVersion
classifier = ""
from(baseProtobuf.files.find { it.name.startsWith("protobuf-java") }?.canonicalPath)
from(
provider {
baseProtobuf.files.find { it.name.startsWith("protobuf-java") }?.canonicalPath
}
)
relocate("com.google.protobuf", "org.jetbrains.kotlin.protobuf" ) {
exclude("META-INF/maven/com.google.protobuf/protobuf-java/pom.properties")
}
addArtifact("archives", this, this)
addArtifact(resultsCfg.name, this, this)
}
val relocateSources by task<Copy> {
from(zipTree(baseProtobufSources.files.find { it.name.startsWith("protobuf-java") && it.name.endsWith("-sources.jar") }
?: throw GradleException("sources jar not found among ${baseProtobufSources.files}")))
artifacts.add("default", prepare)
val relocateSources = task<Copy>("relocateSources") {
from(
provider {
zipTree(baseProtobufSources.files.find { it.name.startsWith("protobuf-java") && it.name.endsWith("-sources.jar") }
?: throw GradleException("sources jar not found among ${baseProtobufSources.files}"))
}
)
into(renamedSources)
filter { it.replace("com.google.protobuf", "org.jetbrains.kotlin.protobuf") }
}
val prepareSources by task<Jar> {
val prepareSources = task<Jar>("prepareSources") {
destinationDir = File(outputJarsPath)
baseName = artifactBaseName
version = protobufVersion
classifier = "sources"
from(relocateSources)
project.addArtifact("archives", this, this)
addArtifact(resultsSourcesCfg.name, this, this)
}
}
artifacts.add("default", prepareSources)
publishing {
publications {
create<MavenPublication>("maven") {
artifact(prepare)
artifact(prepareSources)
}
}
repositories {
maven {
url = uri("${rootProject.buildDir}/internal/repo")
}
}
}
@@ -0,0 +1 @@
include ":protobuf-lite", ":protobuf-relocated"
+3 -2
View File
@@ -73,8 +73,6 @@ include ":kotlin-build-common",
":core:metadata.jvm",
":core:builtins",
":core:util.runtime",
":custom-dependencies:protobuf:protobuf-relocated",
":custom-dependencies:protobuf:protobuf-lite",
":custom-dependencies:android-sdk",
":idea:fir-view",
":idea:idea-jvm",
@@ -349,3 +347,6 @@ project(':kotlinx-serialization-ide-plugin').projectDir = file("$rootDir/plugins
project(':kotlin-serialization').projectDir = file("$rootDir/libraries/tools/kotlin-serialization")
project(':kotlin-serialization-unshaded').projectDir = file("$rootDir/libraries/tools/kotlin-serialization-unshaded")
project(':kotlin-idl2k').projectDir = file("$rootDir/libraries/tools/idl2k")
includeBuild("custom-dependencies/protobuf")