禁用 Google 的 KSP 测试中的代码生成任务

huangapple go评论118阅读模式
英文:

Disable task for code-generation in test for google's KSP

问题

以下是您要翻译的部分:

I've a multi-module project using [google's KSP][1] version 1.8.10-1.0.9 and it would be helpful if someone can help me with disabling the task of code-generation in the tests sub-directory of the build directory.

This is roughly the structure of the project:

```sh
myProject
    - api
    - engine
    - methods
        - build.gradle.kts
            ```
            dependencies {
                implementation(project(":engine"))
                implementation(project(":methodsProcessor"))

                testImplementation(kotlin("test"))
                testImplementation(testFixtures(project(":engine")))

                testImplementation("org.junit.jupiter:junit-jupiter-params:5.8.2")

                ksp(project(":methodsProcessor"))
            }
        ```
        - build
            - generated
                - ksp
                    - main
                        - kotlin
                            - methods
                                - PublicMethods.kt

                    - test   ##  I don't want this directory to be created at all
                        - kotlin
                            - methods
                                - PublicMethods.kt
    - methodsProcessor
        - build.gradle.kts (contents below)
            ```
            dependencies {
                implementation(libs.ksp.api)
            }
            ```
        - src.main.kotlin.processor.MethodsProcessor
            ## this file has a SymbolProcessor which creates a new file (PublicMethods.kt) in
            ## `methods/build/generated/ksp/main/kotlin/methods` &
            ## `methods/build/generated/ksp/test/kotlin/methods` (THIS I would like not to be created)
    - build.gradle.kts
    - settings.gradle.kts
        ```
            rootProject.name = "myProject"

            include("engine")
            include("methods")
            include("api")
            include("methodsProcessor")

            val kotlinVersion: String by settings
            val kspVersion: String by settings

            pluginManagement {
                plugins {
                    // For some reason the global variables are not accessible here
                    val kotlinVersion: String by settings
                    val kspVersion: String by settings
                    kotlin("jvm") version kotlinVersion
                    kotlin("plugin.jpa") version kotlinVersion
                    kotlin("plugin.spring") version kotlinVersion
                    id("com.google.devtools.ksp") version kspVersion
                }
            }

            dependencyResolutionManagement {
                repositories {
                    mavenCentral()
                }
                versionCatalogs {
                    create("libs") {
                        library("ksp-api", "com.google.devtools.ksp:symbol-processing-api:$kspVersion")
                    }
                }
            }
            ```

When I run ./gradlew build for my project, the output on the terminal looks like:


> Task :api:openApiGenerate
################################################################################
# Thanks for using OpenAPI Generator.                                          #
# Please consider donation to help us maintain this project 🙏                 #
# https://opencollective.com/openapi_generator/donate                          #
################################################################################
Successfully generated code to /Users/gxyd/projects/solver-engine/api/build/generated-src/openapi

> Task :methods:kspKotlin
w: [ksp] hello world

Daemon will be stopped at the end of the build after running out of JVM memory

> Task :methods:kspTestKotlin
w: [ksp] hello world

So, the second task that you see, i.e. :methods:kspTestKotlin is something I don't to it to run.

I've already tried having the below configuration in methods.build.gradle.kts:

ksp {
    tasks.named("kspTestKotlin") {
        enabled = false
    }
}

which raises the error saying, there is no task named "kspTestKotlin", while building my project.


<details>
<summary>英文:</summary>

I&#39;ve a multi-module project using [google&#39;s KSP][1] version 1.8.10-1.0.9 and it would be helpful if someone can help me with disabling the task of code-generation in the tests sub-directory of the build directory.

This is roughly the structure of the project:

```sh
myProject
    - api
    - engine
    - methods
        - build.gradle.kts
            ```
            dependencies {
                implementation(project(&quot;:engine&quot;))
                implementation(project(&quot;:methodsProcessor&quot;))

                testImplementation(kotlin(&quot;test&quot;))
                testImplementation(testFixtures(project(&quot;:engine&quot;)))

                testImplementation(&quot;org.junit.jupiter:junit-jupiter-params:5.8.2&quot;)

                ksp(project(&quot;:methodsProcessor&quot;))
            }
        ```
        - build
            - generated
                - ksp
                    - main
                        - kotlin
                            - methods
                                - PublicMethods.kt

                    - test   ##  I don&#39;t want this directory to be created at all
                        - kotlin
                            - methods
                                - PublicMethods.kt
    - methodsProcessor
        - build.gradle.kts (contents below)
            ```
            dependencies {
                implementation(libs.ksp.api)
            }
            ```
        - src.main.kotlin.processor.MethodsProcessor
            ## this file has a SymbolProcessor which creates a new file (PublicMethods.kt) in
            ## `methods/build/generated/ksp/main/kotlin/methods` &amp;
            ## `methods/build/generated/ksp/test/kotlin/methods` (THIS I would like not to be created)
    - build.gradle.kts
    - settings.gradle.kts
        ```
            rootProject.name = &quot;myProject&quot;

            include(&quot;engine&quot;)
            include(&quot;methods&quot;)
            include(&quot;api&quot;)
            include(&quot;methodsProcessor&quot;)

            val kotlinVersion: String by settings
            val kspVersion: String by settings

            pluginManagement {
                plugins {
                    // For some reason the global variables are not accessible here
                    val kotlinVersion: String by settings
                    val kspVersion: String by settings
                    kotlin(&quot;jvm&quot;) version kotlinVersion
                    kotlin(&quot;plugin.jpa&quot;) version kotlinVersion
                    kotlin(&quot;plugin.spring&quot;) version kotlinVersion
                    id(&quot;com.google.devtools.ksp&quot;) version kspVersion
                }
            }

            dependencyResolutionManagement {
                repositories {
                    mavenCentral()
                }
                versionCatalogs {
                    create(&quot;libs&quot;) {
                        library(&quot;ksp-api&quot;, &quot;com.google.devtools.ksp:symbol-processing-api:$kspVersion&quot;)
                    }
                }
            }
            ```

When I run ./gradlew build for my project, the output on the terminal looks like:


&gt; Task :api:openApiGenerate
################################################################################
# Thanks for using OpenAPI Generator.                                          #
# Please consider donation to help us maintain this project &#128591;                 #
# https://opencollective.com/openapi_generator/donate                          #
################################################################################
Successfully generated code to /Users/gxyd/projects/solver-engine/api/build/generated-src/openapi

&gt; Task :methods:kspKotlin
w: [ksp] hello world

Daemon will be stopped at the end of the build after running out of JVM memory

&gt; Task :methods:kspTestKotlin
w: [ksp] hello world

So, the second task that you see, i.e. :methods:kspTestKotlin is something I don't to it to run.

I've already tried having the below configuration in methods.build.gradle.kts:

ksp {
    tasks.named(&quot;kspTestKotlin&quot;) {
        enabled = false
    }
}

which raises the error saying, there is no task named "kspTestKotlin", while building my project.

答案1

得分: 3

默认情况下,ksp 配置适用于所有源集,而其他配置(如 kspTest)仅适用于特定的源集。

从 KSP 1.0.1 开始,有一个属性允许你为 ksp 配置启用此行为。只需将以下内容添加到你的 gradle.properties 文件中:

ksp.allow.all.target.configuration=false

有了这个设置,ksp 配置仅适用于主源集。从 KSP 2.0 开始,默认情况下,ksp 配置将以这种方式运行。

这在这里有文档记录:KSP with Kotlin Multiplatform

这份文档是为多平台项目准备的,但对于单平台项目也适用。

英文:

By default, the ksp configuration applies to all source sets, while the other configurations (like kspTest) only apply to specific ones.

Starting from KSP 1.0.1, there is a property that allows you to enable this behavior for the ksp configuration as well. Simply add the following to your gradle.properties file:

ksp.allow.all.target.configuration=false

With this, the ksp configuration will only apply to the main source set. Starting from KSP 2.0, the ksp configuration will behave like this by default.

This is documented here: KSP with Kotlin Multiplatform

The documentation is for multiplatform, but this applies to single-platform projects as well.

答案2

得分: 0

我有类似的问题,我缺少JUnit引擎依赖项:

testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junit}"
英文:

I had similar issue, I was missing JUnit engine dependency:

testRuntimeOnly &quot;org.junit.jupiter:junit-jupiter-engine:${junit}&quot;

huangapple
  • 本文由 发表于 2023年3月7日 18:44:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定