Build: Introduce flag for disabling jar post processing

proguard, relocation, etc.
This commit is contained in:
Vyacheslav Gerasimov
2019-06-26 17:34:18 +03:00
parent 6e20e03cbc
commit 2d6a3cb2c8
4 changed files with 41 additions and 36 deletions
+11 -2
View File
@@ -28,7 +28,8 @@ class KotlinBuildProperties(
private operator fun get(key: String): Any? = localProperties.getProperty(key) ?: propertiesProvider.getProperty(key) private operator fun get(key: String): Any? = localProperties.getProperty(key) ?: propertiesProvider.getProperty(key)
private fun getBoolean(key: String): Boolean = this[key]?.toString()?.toBoolean() == true private fun getBoolean(key: String, default: Boolean = false): Boolean =
(this[key]?.toString()?.toBoolean() ?: default) == true
val isJpsBuildEnabled: Boolean = getBoolean("jpsBuild") val isJpsBuildEnabled: Boolean = getBoolean("jpsBuild")
@@ -70,9 +71,17 @@ class KotlinBuildProperties(
} }
return kotlinUltimateExists && (explicitlyEnabled || isTeamcityBuild) return kotlinUltimateExists && (explicitlyEnabled || isTeamcityBuild)
} }
val postProcessing: Boolean get() = isTeamcityBuild || getBoolean("kotlin.build.postprocessing", true)
val relocation: Boolean get() = postProcessing
val proguard: Boolean get() = postProcessing && getBoolean("kotlin.build.proguard")
val jsIrDist: Boolean get() = getBoolean("kotlin.stdlib.js.ir.dist")
} }
private const val extensionName = "kotlinBuildFlags" private const val extensionName = "kotlinBuildProperties"
class ProjectProperties(val project: Project) : PropertiesProvider { class ProjectProperties(val project: Project) : PropertiesProvider {
override val rootProjectDir: File override val rootProjectDir: File
+18 -13
View File
@@ -35,7 +35,6 @@ val core = "$rootDir/core"
val relocatedCoreSrc = "$buildDir/core-relocated" val relocatedCoreSrc = "$buildDir/core-relocated"
val libsDir = property("libsDir") val libsDir = property("libsDir")
val proguardDeps by configurations.creating val proguardDeps by configurations.creating
val proguardAdditionalInJars by configurations.creating val proguardAdditionalInJars by configurations.creating
@@ -109,23 +108,25 @@ class KotlinModuleShadowTransformer(private val logger: Logger) : Transformer {
} }
val reflectShadowJar by task<ShadowJar> { val reflectShadowJar by task<ShadowJar> {
classifier = "shadow" archiveClassifier.set("shadow")
version = null configurations = listOf(embedded)
callGroovy("manifestAttributes", manifest, project, "Main" /*true*/) callGroovy("manifestAttributes", manifest, project, "Main" /*true*/)
exclude("**/*.proto") exclude("**/*.proto")
transform(KotlinModuleShadowTransformer(logger))
configurations = listOf(embedded)
relocate("org.jetbrains.kotlin", "kotlin.reflect.jvm.internal.impl")
relocate("javax.inject", "kotlin.reflect.jvm.internal.impl.javax.inject")
mergeServiceFiles() mergeServiceFiles()
if (kotlinBuildProperties.relocation) {
transform(KotlinModuleShadowTransformer(logger))
relocate("org.jetbrains.kotlin", "kotlin.reflect.jvm.internal.impl")
relocate("javax.inject", "kotlin.reflect.jvm.internal.impl.javax.inject")
}
} }
val stripMetadata by tasks.creating { val stripMetadata by tasks.creating {
dependsOn("reflectShadowJar") dependsOn(reflectShadowJar)
val inputJar = reflectShadowJar.archivePath val inputJar = reflectShadowJar.outputs.files.singleFile
val outputJar = File("$libsDir/kotlin-reflect-stripped.jar") val outputJar = File("$libsDir/kotlin-reflect-stripped.jar")
inputs.file(inputJar) inputs.file(inputJar)
outputs.file(outputJar) outputs.file(outputJar)
@@ -189,14 +190,18 @@ addArtifact("archives", sourcesJar)
addArtifact("sources", sourcesJar) addArtifact("sources", sourcesJar)
val result by task<Jar> { val result by task<Jar> {
dependsOn(proguard) val task = if (kotlinBuildProperties.proguard) proguard else reflectShadowJar
from(zipTree(file(proguardOutput))) dependsOn(task)
from {
zipTree(task.outputs.files.singleFile)
}
callGroovy("manifestAttributes", manifest, project, "Main") callGroovy("manifestAttributes", manifest, project, "Main")
} }
val modularJar by task<Jar> { val modularJar by task<Jar> {
dependsOn(proguard) dependsOn(proguard)
classifier = "modular" archiveClassifier.set("modular")
from(zipTree(file(proguardOutput))) from(zipTree(file(proguardOutput)))
from(zipTree(reflectShadowJar.archivePath)) { from(zipTree(reflectShadowJar.archivePath)) {
include("META-INF/versions/**") include("META-INF/versions/**")
@@ -8,11 +8,6 @@ plugins {
id("jps-compatible") id("jps-compatible")
} }
// You can run Gradle with "-Pkotlin.build.proguard=true" to enable ProGuard run on the jar (on TeamCity, ProGuard always runs)
val shrink =
findProperty("kotlin.build.proguard")?.toString()?.toBoolean()
?: hasProperty("teamcity")
val jarBaseName = property("archivesBaseName") as String val jarBaseName = property("archivesBaseName") as String
val proguardLibraryJars by configurations.creating val proguardLibraryJars by configurations.creating
@@ -58,9 +53,8 @@ val mainKtsRelocatedDepsRootPackage = "$mainKtsRootPackage.relocatedDeps"
val packJar by task<ShadowJar> { val packJar by task<ShadowJar> {
configurations = emptyList() configurations = emptyList()
duplicatesStrategy = DuplicatesStrategy.EXCLUDE duplicatesStrategy = DuplicatesStrategy.EXCLUDE
destinationDir = File(buildDir, "libs") destinationDirectory.set(File(buildDir, "libs"))
archiveClassifier.set("before-proguard")
setupPublicJar(project.the<BasePluginConvention>().archivesBaseName, "before-proguard")
from(mainSourceSet.output) from(mainSourceSet.output)
from(project.configurations.embedded) from(project.configurations.embedded)
@@ -68,8 +62,10 @@ val packJar by task<ShadowJar> {
// don't add this files to resources classpath to avoid IDE exceptions on kotlin project // don't add this files to resources classpath to avoid IDE exceptions on kotlin project
from("jar-resources") from("jar-resources")
packagesToRelocate.forEach { if (kotlinBuildProperties.relocation) {
relocate(it, "$mainKtsRelocatedDepsRootPackage.$it") packagesToRelocate.forEach {
relocate(it, "$mainKtsRelocatedDepsRootPackage.$it")
}
} }
} }
@@ -90,7 +86,7 @@ val proguard by task<ProGuardTask> {
} }
val resultJar = tasks.register<Jar>("resultJar") { val resultJar = tasks.register<Jar>("resultJar") {
val pack = if (shrink) proguard else packJar val pack = if (kotlinBuildProperties.proguard) proguard else packJar
dependsOn(pack) dependsOn(pack)
setupPublicJar(jarBaseName) setupPublicJar(jarBaseName)
from { from {
+5 -10
View File
@@ -12,11 +12,6 @@ plugins {
java java
} }
// You can run Gradle with "-Pkotlin.build.proguard=true" to enable ProGuard run on kotlin-compiler.jar (on TeamCity, ProGuard always runs)
val shrink = findProperty("kotlin.build.proguard")?.toString()?.toBoolean() ?: hasProperty("teamcity")
val jsIrDist = findProperty("kotlin.stdlib.js.ir.dist")?.toString()?.toBoolean() == true
val fatJarContents by configurations.creating val fatJarContents by configurations.creating
val fatJarContentsStripMetadata by configurations.creating val fatJarContentsStripMetadata by configurations.creating
val fatJarContentsStripServices by configurations.creating val fatJarContentsStripServices by configurations.creating
@@ -85,10 +80,10 @@ val distLibraryProjects = listOfNotNull(
":kotlin-scripting-compiler", ":kotlin-scripting-compiler",
":kotlin-scripting-compiler-impl", ":kotlin-scripting-compiler-impl",
":kotlin-scripting-jvm", ":kotlin-scripting-jvm",
":kotlin-stdlib-js-ir".takeIf { jsIrDist }, ":kotlin-stdlib-js-ir".takeIf { kotlinBuildProperties.jsIrDist },
":kotlin-source-sections-compiler-plugin", ":kotlin-source-sections-compiler-plugin",
":kotlin-test:kotlin-test-js", ":kotlin-test:kotlin-test-js",
":kotlin-test:kotlin-test-js-ir".takeIf { jsIrDist }, ":kotlin-test:kotlin-test-js-ir".takeIf { kotlinBuildProperties.jsIrDist },
":kotlin-test:kotlin-test-junit", ":kotlin-test:kotlin-test-junit",
":kotlin-test:kotlin-test-junit5", ":kotlin-test:kotlin-test-junit5",
":kotlin-test:kotlin-test-jvm", ":kotlin-test:kotlin-test-jvm",
@@ -109,9 +104,9 @@ val distCompilerPluginProjects = listOf(
val distSourcesProjects = listOfNotNull( val distSourcesProjects = listOfNotNull(
":kotlin-annotations-jvm", ":kotlin-annotations-jvm",
":kotlin-script-runtime", ":kotlin-script-runtime",
":kotlin-stdlib-js-ir".takeIf { jsIrDist }, ":kotlin-stdlib-js-ir".takeIf { kotlinBuildProperties.jsIrDist },
":kotlin-test:kotlin-test-js", ":kotlin-test:kotlin-test-js",
":kotlin-test:kotlin-test-js-ir".takeIf { jsIrDist }, ":kotlin-test:kotlin-test-js-ir".takeIf { kotlinBuildProperties.jsIrDist },
":kotlin-test:kotlin-test-junit", ":kotlin-test:kotlin-test-junit",
":kotlin-test:kotlin-test-junit5", ":kotlin-test:kotlin-test-junit5",
":kotlin-test:kotlin-test-jvm", ":kotlin-test:kotlin-test-jvm",
@@ -262,7 +257,7 @@ val proguard by task<ProGuardTask> {
} }
} }
val pack = if (shrink) proguard else packCompiler val pack = if (kotlinBuildProperties.proguard) proguard else packCompiler
val distDir: String by rootProject.extra val distDir: String by rootProject.extra
val jar = runtimeJar { val jar = runtimeJar {