Updated to gradle 7.2 + kotlin 1.5

This commit is contained in:
Robinson 2021-09-11 01:48:18 +02:00
parent c1ad1fec7e
commit 578dbd78f9
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C

View File

@ -27,6 +27,7 @@ import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.jvm.tasks.Jar import org.gradle.jvm.tasks.Jar
import org.gradle.util.GradleVersion import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions
import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.io.File import java.io.File
import java.security.MessageDigest import java.security.MessageDigest
@ -37,6 +38,7 @@ import kotlin.reflect.full.declaredMemberFunctions
import kotlin.reflect.full.declaredMemberProperties import kotlin.reflect.full.declaredMemberProperties
@Suppress("unused", "MemberVisibilityCanBePrivate", "ObjectLiteralToLambda")
open class StaticMethodsAndTools(private val project: Project) { open class StaticMethodsAndTools(private val project: Project) {
companion object { companion object {
/** /**
@ -48,20 +50,20 @@ open class StaticMethodsAndTools(private val project: Project) {
// check if plugin is available // check if plugin is available
project.plugins.findPlugin("org.jetbrains.kotlin.jvm") ?: return false project.plugins.findPlugin("org.jetbrains.kotlin.jvm") ?: return false
if (debug) println("\t Has kotlin plugin") if (debug) println("\tHas kotlin plugin")
// this will check if the task exists, and throw an exception if it does not or return false // this will check if the task exists, and throw an exception if it does not or return false
project.tasks.named("compileKotlin", KotlinCompile::class.java).orNull ?: return false project.tasks.named("compileKotlin", KotlinCompile::class.java).orNull ?: return false
if (debug) println("\t Has compile kotlin task") if (debug) println("\tHas compile kotlin task")
// check to see if we have any kotlin file // check to see if we have any kotlin file
val sourceSets = project.extensions.getByName("sourceSets") as SourceSetContainer val sourceSets = project.extensions.getByName("sourceSets") as SourceSetContainer
val main = sourceSets.getByName("main") val main = sourceSets.getByName("main")
if (debug) { if (debug) {
println("\t main dirs: ${main.java.srcDirs}") println("\tmain dirs: ${main.java.srcDirs}")
println("\t kotlin dirs: ${main.kotlin.srcDirs}") println("\tkotlin dirs: ${main.kotlin.srcDirs}")
project.buildFile.parentFile.walkTopDown().filter { it.extension == "kt" }.forEach { project.buildFile.parentFile.walkTopDown().filter { it.extension == "kt" }.forEach {
println("\t\t$it") println("\t\t$it")
@ -115,7 +117,7 @@ open class StaticMethodsAndTools(private val project: Project) {
/** /**
* Maps the property (key/value) pairs of a property file onto the specified target object. Also maps fields in the targetObject to the * Maps the property (key/value) pairs of a property file onto the specified target object. Also maps fields in the targetObject to the
* project, if have the same name relationship (ie: field name is "version", project method is "setVersion") * project, if they have the same name relationship (ie: field name is "version", project method is "setVersion")
*/ */
fun load(propertyFile: String, targetObject: Any) { fun load(propertyFile: String, targetObject: Any) {
val kClass = targetObject::class val kClass = targetObject::class
@ -160,7 +162,7 @@ open class StaticMethodsAndTools(private val project: Project) {
// if we have a property name in our PROJECT, we set it // if we have a property name in our PROJECT, we set it
// project functions that can be called for setting properties // project functions that can be called for setting properties
val setterName = "set${key.capitalize()}" val setterName = "set${key.replaceFirstChar { it.titlecaseChar() }}"
propertyFunctions.find { prop -> prop.name == setterName }?.call(project, value) propertyFunctions.find { prop -> prop.name == setterName }?.call(project, value)
// assign this as an "extra property" // assign this as an "extra property"
@ -176,7 +178,7 @@ open class StaticMethodsAndTools(private val project: Project) {
// assign target fields to our project (if our project has matching setters) // assign target fields to our project (if our project has matching setters)
assignedExtraProperties.forEach { assignedExtraProperties.forEach {
val propertyName = it.name val propertyName = it.name
val setterName = "set${propertyName.capitalize()}" val setterName = "set${propertyName.replaceFirstChar { it.titlecaseChar() }}"
val projectMethod = propertyFunctions.find { prop -> prop.name == setterName } val projectMethod = propertyFunctions.find { prop -> prop.name == setterName }
if (projectMethod != null) { if (projectMethod != null) {
@ -194,7 +196,7 @@ open class StaticMethodsAndTools(private val project: Project) {
* Validates the minimum version of gradle supported * Validates the minimum version of gradle supported
*/ */
fun minVersion(version: String) { fun minVersion(version: String) {
val compared = org.gradle.util.GradleVersion.current().compareTo(org.gradle.util.GradleVersion.version(version)) val compared = GradleVersion.current().compareTo(GradleVersion.version(version))
if (compared == -1) { if (compared == -1) {
throw GradleException("This project requires Gradle $version or higher.") throw GradleException("This project requires Gradle $version or higher.")
} }
@ -204,7 +206,7 @@ open class StaticMethodsAndTools(private val project: Project) {
* Validates the maximum version of gradle supported * Validates the maximum version of gradle supported
*/ */
fun maxVersion(version: String) { fun maxVersion(version: String) {
val compared = org.gradle.util.GradleVersion.current().compareTo(org.gradle.util.GradleVersion.version(version)) val compared = GradleVersion.current().compareTo(GradleVersion.version(version))
if (compared == 1) { if (compared == 1) {
throw GradleException("This project requires Gradle $version or lower.") throw GradleException("This project requires Gradle $version or lower.")
} }
@ -419,7 +421,7 @@ open class StaticMethodsAndTools(private val project: Project) {
} }
if (hasKotlin) { if (hasKotlin) {
kotlin { kotlin.apply {
setSrcDirs(project.files("test")) setSrcDirs(project.files("test"))
include("**/*.kt") // want to include java files for the source. 'setSrcDirs' resets includes... include("**/*.kt") // want to include java files for the source. 'setSrcDirs' resets includes...
} }
@ -491,11 +493,15 @@ open class StaticMethodsAndTools(private val project: Project) {
* Always compile java with UTF-8, make it incremental, and compile `package-info.java` classes * Always compile java with UTF-8, make it incremental, and compile `package-info.java` classes
*/ */
fun defaultCompileOptions() { fun defaultCompileOptions() {
project.tasks.withType(JavaCompile::class.java) { project.tasks.withType(JavaCompile::class.java, object: Action<Task> {
it.options.encoding = "UTF-8" override fun execute(task: Task) {
it.options.isIncremental = true task as JavaCompile
it.options.compilerArgs.add("-Xpkginfo:always")
task.options.encoding = "UTF-8"
task.options.isIncremental = true
task.options.compilerArgs.add("-Xpkginfo:always")
} }
})
} }
/** /**
@ -506,12 +512,11 @@ open class StaticMethodsAndTools(private val project: Project) {
kotlinActions: KotlinJvmOptions.() -> Unit = {}) { kotlinActions: KotlinJvmOptions.() -> Unit = {}) {
val javaVer = javaVersion.toString() val javaVer = javaVersion.toString()
val kotlinJavaVer = kotlinJavaVersion.toString() val kotlinJavaVer = kotlinJavaVersion.toString()
val defaultKotlinVersion = "1.5.0" val defaultKotlinVersion = "1.5.21"
val kotlinVer: String = try { val kotlinVer: String = try {
if (hasKotlin) { if (hasKotlin) {
val kot = project.plugins.findPlugin("org.jetbrains.kotlin.jvm") as org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper? val version = project.getKotlinPluginVersion()
val version = kot?.kotlinPluginVersion ?: defaultKotlinVersion
// we ONLY care about the major.minor // we ONLY care about the major.minor
val secondDot = version.indexOf('.', version.indexOf('.')+1) val secondDot = version.indexOf('.', version.indexOf('.')+1)
@ -520,14 +525,21 @@ open class StaticMethodsAndTools(private val project: Project) {
defaultKotlinVersion defaultKotlinVersion
} }
} catch (e: Exception) { } catch (e: Exception) {
// in case we cannot parse it from the plugin, provide a reasonable default (latest stable) // in case we cannot parse it from the plugin, provide a reasonable default (the latest stable)
defaultKotlinVersion defaultKotlinVersion
} }
project.tasks.withType(JavaCompile::class.java) { task -> // NOTE: these must be anonymous inner classes because gradle cannot handle this in kotlin 1.5
task.doFirst { project.tasks.withType(JavaCompile::class.java, object: Action<Task> {
println("\tCompiling classes to Java ${JavaVersion.toVersion(task.targetCompatibility)}") override fun execute(task: Task) {
task as JavaCompile
task.doFirst(object: Action<Task> {
override fun execute(it: Task) {
it as JavaCompile
println("\tCompiling classes to Java ${JavaVersion.toVersion(it.targetCompatibility)}")
} }
})
task.options.encoding = "UTF-8" task.options.encoding = "UTF-8"
@ -540,29 +552,42 @@ open class StaticMethodsAndTools(private val project: Project) {
task.sourceCompatibility = javaVer task.sourceCompatibility = javaVer
task.targetCompatibility = javaVer task.targetCompatibility = javaVer
} }
})
project.tasks.withType(Jar::class.java) { // NOTE: these must be anonymous inner classes because gradle cannot handle this in kotlin 1.5
it.duplicatesStrategy = DuplicatesStrategy.FAIL project.tasks.withType(Jar::class.java, object: Action<Task> {
override fun execute(task: Task) {
task as Jar
task.duplicatesStrategy = DuplicatesStrategy.FAIL
} }
})
if (hasKotlin) { if (hasKotlin) {
project.tasks.withType(KotlinCompile::class.java) { task -> // NOTE: these must be anonymous inner classes because gradle cannot handle this in kotlin 1.5
task.doFirst { project.tasks.withType(KotlinCompile::class.java, object: Action<Task> {
println("\tCompiling classes to Kotlin ${task.kotlinOptions.languageVersion}, Java ${task.kotlinOptions.jvmTarget}") override fun execute(task: Task) {
task as KotlinCompile
task.doFirst(object: Action<Task> {
override fun execute(it: Task) {
it as KotlinCompile
println("\tCompiling classes to Kotlin ${it.kotlinOptions.languageVersion}, Java ${it.kotlinOptions.jvmTarget}")
} }
})
task.sourceCompatibility = kotlinJavaVer task.sourceCompatibility = kotlinJavaVer
task.targetCompatibility = kotlinJavaVer task.targetCompatibility = kotlinJavaVer
task.kotlinOptions.jvmTarget = kotlinJavaVer task.kotlinOptions.jvmTarget = kotlinJavaVer
// default is whatever the version is that we are running, or 1.4.32 if we cannot figure it out // default is whatever the version is that we are running, or XXXXX if we cannot figure it out
task.kotlinOptions.apiVersion = kotlinVer task.kotlinOptions.apiVersion = kotlinVer
task.kotlinOptions.languageVersion = kotlinVer task.kotlinOptions.languageVersion = kotlinVer
// see: https://kotlinlang.org/docs/reference/using-gradle.html // see: https://kotlinlang.org/docs/reference/using-gradle.html
kotlinActions(task.kotlinOptions) kotlinActions(task.kotlinOptions)
} }
})
} }
} }
@ -645,10 +670,13 @@ open class StaticMethodsAndTools(private val project: Project) {
fun inferJpmsModule() { fun inferJpmsModule() {
if (GradleVersion.current() >= GradleVersion.version("6.4")) { if (GradleVersion.current() >= GradleVersion.version("6.4")) {
project.gradle.taskGraph.whenReady { project.gradle.taskGraph.whenReady {
project.convention.configure(JavaPluginExtension::class.java) { // NOTE: these must be anonymous inner classes because gradle cannot handle this in kotlin 1.5
project.convention.configure(JavaPluginExtension::class.java, object: Action<JavaPluginExtension> {
override fun execute(task: JavaPluginExtension) {
// Should a --module-path be inferred by analysing JARs and class folders on the classpath? // Should a --module-path be inferred by analysing JARs and class folders on the classpath?
it.modularity.inferModulePath.set(true) task.modularity.inferModulePath.set(true)
} }
})
} }
} }
} }
@ -679,10 +707,14 @@ open class StaticMethodsAndTools(private val project: Project) {
// Make sure to cleanup the any possible license file on clean // Make sure to cleanup the any possible license file on clean
println("\tAllowing kotlin internal access for $moduleName") println("\tAllowing kotlin internal access for $moduleName")
project.tasks.withType(KotlinCompile::class.java).forEach { // NOTE: these must be anonymous inner classes because gradle cannot handle this in kotlin 1.5
project.tasks.withType(KotlinCompile::class.java, object: Action<Task> {
override fun execute(task: Task) {
task as KotlinCompile
// must be the same module name as the regular one (which is the project name). If it is a different name, it crashes at runtime // must be the same module name as the regular one (which is the project name). If it is a different name, it crashes at runtime
it.kotlinOptions.moduleName = moduleName task.kotlinOptions.moduleName = moduleName
} }
})
accessGroup.forEach { accessGroup.forEach {
// allow code in a *different* directory access to "internal" scope members of code. // allow code in a *different* directory access to "internal" scope members of code.
@ -697,9 +729,7 @@ open class StaticMethodsAndTools(private val project: Project) {
} }
} }
class AccessGroup(val sourceName: String, vararg targetNames: String) { class AccessGroup(val sourceName: String, vararg val targetNames: String)
val targetNames: Array<out String> = targetNames
}
// https://docs.oracle.com/javase/7/docs/api/java/security/MessageDigest.html // https://docs.oracle.com/javase/7/docs/api/java/security/MessageDigest.html
// Every implementation of the Java platform is required to support the following standard MessageDigest algorithms: // Every implementation of the Java platform is required to support the following standard MessageDigest algorithms: