Added possibility to create a custom usable KotlinCompile task without using internals ownModuleName and moduleName
#KT-60541 Ready for Review
This commit is contained in:
committed by
Space Team
parent
103ebb0ab4
commit
8299fffae2
@@ -1026,6 +1026,7 @@ public abstract interface class org/jetbrains/kotlin/gradle/plugin/KotlinJvmFact
|
||||
public abstract fun registerKaptGenerateStubsTask (Ljava/lang/String;)Lorg/gradle/api/tasks/TaskProvider;
|
||||
public abstract fun registerKaptTask (Ljava/lang/String;)Lorg/gradle/api/tasks/TaskProvider;
|
||||
public abstract fun registerKotlinJvmCompileTask (Ljava/lang/String;)Lorg/gradle/api/tasks/TaskProvider;
|
||||
public abstract fun registerKotlinJvmCompileTask (Ljava/lang/String;Ljava/lang/String;)Lorg/gradle/api/tasks/TaskProvider;
|
||||
}
|
||||
|
||||
public final class org/jetbrains/kotlin/gradle/plugin/KotlinPlatformType : java/lang/Enum, java/io/Serializable, org/gradle/api/Named {
|
||||
|
||||
+17
-1
@@ -38,9 +38,25 @@ interface KotlinJvmFactory {
|
||||
|
||||
fun createCompilerJvmOptions(): KotlinJvmCompilerOptions
|
||||
|
||||
/** Creates a Kotlin compile task. */
|
||||
/**
|
||||
* Creates a Kotlin compile task.
|
||||
*/
|
||||
@Deprecated(
|
||||
message = "Replaced by registerKotlinJvmCompileTask with module name",
|
||||
replaceWith = ReplaceWith("registerKotlinJvmCompileTask(taskName: String, moduleName: String)")
|
||||
)
|
||||
fun registerKotlinJvmCompileTask(taskName: String): TaskProvider<out KotlinJvmCompile>
|
||||
|
||||
|
||||
/**
|
||||
* Creates a Kotlin JVM compile task.
|
||||
*
|
||||
* @param taskName The name of the task to be created.
|
||||
* @param moduleName The name of the module for which the task is being created.
|
||||
* @return The task provider for the Kotlin JVM compile task.
|
||||
*/
|
||||
fun registerKotlinJvmCompileTask(taskName: String, moduleName: String): TaskProvider<out KotlinJvmCompile>
|
||||
|
||||
/** Creates a stub generation task which creates Java sources stubs from Kotlin sources. */
|
||||
fun registerKaptGenerateStubsTask(taskName: String): TaskProvider<out KaptGenerateStubs>
|
||||
|
||||
|
||||
+40
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.gradle.tasks
|
||||
import org.gradle.util.GradleVersion
|
||||
import org.jetbrains.kotlin.gradle.testbase.*
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import kotlin.io.path.appendText
|
||||
import kotlin.io.path.createDirectories
|
||||
import kotlin.io.path.deleteRecursively
|
||||
import kotlin.io.path.writeText
|
||||
@@ -58,6 +59,45 @@ class KotlinJvmApiTest : KGPBaseTest() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@DisplayName("KT-60541: checks that configuring custom KotlinCompile does not require using internals")
|
||||
@JvmGradlePluginTests
|
||||
@GradleTest
|
||||
internal fun kotlinCompileCustomTask(gradleVersion: GradleVersion) {
|
||||
project(projectName = "jvm-with-common", gradleVersion = gradleVersion) {
|
||||
|
||||
val customModuleName = "customModuleName"
|
||||
val customTaskName = "customTask"
|
||||
val outputDirName = "customTaskOutput"
|
||||
buildGradleKts.appendText(
|
||||
"""
|
||||
val compileKotlin = tasks.getByName("compileKotlinJvm") as KotlinCompile
|
||||
|
||||
apply<KotlinBaseApiPlugin>()
|
||||
|
||||
val myCustomTask = plugins
|
||||
.findPlugin(KotlinBaseApiPlugin::class)!!
|
||||
.registerKotlinJvmCompileTask("$customTaskName", moduleName = "$customModuleName")
|
||||
|
||||
myCustomTask {
|
||||
source("src/jvmMain", "src/commonMain")
|
||||
libraries.from(compileKotlin.libraries)
|
||||
useModuleDetection.set(false)
|
||||
multiPlatformEnabled.set(false)
|
||||
destinationDirectory.set(File(project.buildDir, "$outputDirName"))
|
||||
}
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
build(customTaskName) {
|
||||
assertOutputDoesNotContain("No value has been specified for property 'ownModuleName'")
|
||||
assertFileInProjectExists("build/$outputDirName/org/example/application/MainKt.class")
|
||||
assertFileInProjectExists("build/$outputDirName/org/example/Lib.class")
|
||||
assertFileInProjectExists("build/$outputDirName/META-INF/$customModuleName.kotlin_module")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@DisplayName("KAPT can be set up using APIs")
|
||||
@OtherGradlePluginTests
|
||||
@GradleTestVersions(minVersion = TestVersions.Gradle.G_7_0)
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
import org.jetbrains.kotlin.gradle.plugin.KotlinBaseApiPlugin
|
||||
|
||||
plugins {
|
||||
kotlin("multiplatform")
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvm()
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package org.example
|
||||
|
||||
class Lib {
|
||||
val hello = "Hello from lib"
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package org.example.application
|
||||
|
||||
import org.example.Lib
|
||||
|
||||
fun main() {
|
||||
println(Lib().hello)
|
||||
}
|
||||
+20
@@ -64,6 +64,10 @@ abstract class KotlinBaseApiPlugin : DefaultKotlinBasePlugin(), KotlinJvmFactory
|
||||
myProject.objects.newInstance(KaptExtension::class.java)
|
||||
}
|
||||
|
||||
@Deprecated(
|
||||
message = "Replaced by registerKotlinJvmCompileTask with module name",
|
||||
replaceWith = ReplaceWith("registerKotlinJvmCompileTask(taskName: String, moduleName: String)")
|
||||
)
|
||||
override fun registerKotlinJvmCompileTask(taskName: String): TaskProvider<out KotlinJvmCompile> {
|
||||
return taskCreator.registerKotlinJVMTask(
|
||||
myProject,
|
||||
@@ -73,6 +77,22 @@ abstract class KotlinBaseApiPlugin : DefaultKotlinBasePlugin(), KotlinJvmFactory
|
||||
)
|
||||
}
|
||||
|
||||
override fun registerKotlinJvmCompileTask(taskName: String, moduleName: String): TaskProvider<out KotlinJvmCompile> {
|
||||
val compilerOptions = createCompilerJvmOptions()
|
||||
compilerOptions.moduleName.set(moduleName)
|
||||
val registeredKotlinJvmCompileTask = taskCreator.registerKotlinJVMTask(
|
||||
myProject,
|
||||
taskName,
|
||||
compilerOptions,
|
||||
KotlinCompileConfig(myProject, kotlinExtension)
|
||||
)
|
||||
registeredKotlinJvmCompileTask.configure {
|
||||
it.ownModuleName.set(compilerOptions.moduleName)
|
||||
it.moduleName.set(compilerOptions.moduleName)
|
||||
}
|
||||
return registeredKotlinJvmCompileTask
|
||||
}
|
||||
|
||||
override fun registerKaptGenerateStubsTask(taskName: String): TaskProvider<out KaptGenerateStubs> {
|
||||
val taskConfig = KaptGenerateStubsConfig(myProject, kotlinExtension, kaptExtension)
|
||||
return myProject.registerTask(taskName, KaptGenerateStubsTask::class.java, listOf(myProject)).also {
|
||||
|
||||
+2
-1
@@ -31,13 +31,14 @@ class KotlinCompileApiTest {
|
||||
|
||||
companion object {
|
||||
private const val TASK_NAME = "kotlinCompile"
|
||||
private const val MODULE_NAME = "customModuleName"
|
||||
}
|
||||
|
||||
@Before
|
||||
fun setUpProject() {
|
||||
project = buildProject {}
|
||||
plugin = project.plugins.apply(KotlinApiPlugin::class.java)
|
||||
plugin.registerKotlinJvmCompileTask(TASK_NAME).configure { task ->
|
||||
plugin.registerKotlinJvmCompileTask(TASK_NAME, MODULE_NAME).configure { task ->
|
||||
taskApi = task
|
||||
}
|
||||
taskImpl = project.tasks.getByName(TASK_NAME) as KotlinCompile
|
||||
|
||||
Reference in New Issue
Block a user