Switch protobuf usages to regular dependency

This commit is contained in:
Ilya Chernikov
2017-11-09 14:31:32 +01:00
committed by Vyacheslav Gerasimov
parent a952d797f1
commit 4a47fb3ffb
6 changed files with 92 additions and 104 deletions
@@ -1,53 +1,29 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
import java.util.jar.JarEntry
import java.util.jar.JarFile
import java.util.zip.ZipOutputStream
import org.gradle.language.assembler.tasks.Assemble
buildscript {
repositories {
jcenter()
}
val relocatedProtobuf by configurations.creating
val relocatedProtobufSources by configurations.creating
dependencies {
classpath("com.github.jengelman.gradle.plugins:shadow:${property("versions.shadow")}")
}
}
apply { plugin("com.github.johnrengelman.shadow") }
val mainCfg = configurations.create("default")
val relocatedCfg = configurations.create("relocated")
val resultsCfg = configurations.create("default")
val protobufVersion = rootProject.extra["versions.protobuf-java"]
val protobufJarPrefix = "protobuf-$protobufVersion"
val renamedOutputJarPath = "$buildDir/jars/$protobufJarPrefix-relocated.jar"
val outputJarPath = "$buildDir/libs/$protobufJarPrefix-lite.jar"
artifacts.add(mainCfg.name, File(outputJarPath))
artifacts.add(relocatedCfg.name, File(renamedOutputJarPath))
dependencies {
mainCfg("com.google.protobuf:protobuf-java:$protobufVersion")
relocatedProtobuf(project(":custom-dependencies:protobuf-relocated", configuration = "default"))
relocatedProtobufSources(project(":custom-dependencies:protobuf-relocated", configuration = "sources"))
}
val relocateTask = task<ShadowJar>("prepare-relocated-protobuf") {
archiveName = renamedOutputJarPath
this.configurations = listOf(relocatedCfg)
from(mainCfg.files.find { it.name.startsWith("protobuf-java") }?.canonicalPath)
relocate("com.google.protobuf", "org.jetbrains.kotlin.protobuf" ) {
// TODO: remove "it." after #KT-12848 get addressed
exclude("META-INF/maven/com.google.protobuf/protobuf-java/pom.properties")
}
}
val mainTask = task("prepare") {
dependsOn(relocateTask)
val inputJar = renamedOutputJarPath
task("prepare") {
// TODO: find out why it doesn't work without explicit dependsOn
dependsOn(":custom-dependencies:protobuf-relocated:prepare")
val inputJar = relocatedProtobuf.files.single()
inputs.files(inputJar)
outputs.file(outputJarPath)
doFirst {
@@ -112,10 +88,18 @@ val mainTask = task("prepare") {
}
}
}
addArtifact("archives", this, outputs.files.singleFile) {
classifier = ""
}
addArtifact(resultsCfg, this, outputs.files.singleFile) {
classifier = ""
}
}
defaultTasks(mainTask.name)
tasks.withType<Assemble>() {
dependsOn(mainCfg)
val clean by task<Delete> {
delete(buildDir)
}
artifacts.add("archives", relocatedProtobufSources.files.single()) {
classifier = "sources"
}
@@ -0,0 +1,66 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
buildscript {
repositories {
jcenter()
}
dependencies {
classpath("com.github.jengelman.gradle.plugins:shadow:${property("versions.shadow")}")
}
}
val baseProtobuf by configurations.creating
val baseProtobufSources by configurations.creating
val resultsCfg = configurations.create("default")
val resultsSourcesCfg = configurations.create("sources")
val protobufVersion = rootProject.extra["versions.protobuf-java"] as String
val protobufJarPrefix = "protobuf-$protobufVersion"
val renamedSources = "$buildDir/renamedSrc/"
val outputJarsPath = "$buildDir/libs"
val artifactBaseName = "protobuf-java-relocated"
dependencies {
baseProtobuf("com.google.protobuf:protobuf-java:$protobufVersion")
baseProtobufSources("com.google.protobuf:protobuf-java:$protobufVersion:sources")
}
val prepare by task<ShadowJar> {
destinationDir = File(outputJarsPath)
baseName = artifactBaseName
version = protobufVersion
classifier = ""
from(baseProtobuf.files.find { it.name.startsWith("protobuf-java") }?.canonicalPath)
relocate("com.google.protobuf", "org.jetbrains.kotlin.protobuf" ) {
// TODO: remove "it." after #KT-12848 get addressed
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}")))
into(renamedSources)
filter { it.replace("com.google.protobuf", "org.jetbrains.kotlin.protobuf") }
}
val prepareSources by task<Jar> {
destinationDir = File(outputJarsPath)
baseName = artifactBaseName
version = protobufVersion
classifier = "sources"
from(relocateSources)
project.addArtifact("archives", this, this)
addArtifact(resultsSourcesCfg.name, this, this)
}
val clean by task<Delete> {
delete(buildDir)
}