Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

How do you split large kotlin based Gradle build files with variables?

I have a large gradle.build.kts file and I'm trying to break it into smaller pieces using apply(from = "xxx") (I'm open to better solutions if one exists).

The problem is that I've run into a problem where one of my scripts cant handle variables. Here is the sample code:

build.gradle.kts

plugins { java }
repositories { mavenCentral() }
dependencies { testCompile("junit", "junit", "4.12") }

println("Before Apply")
apply(from = "./build.gradle.test.kts")
println("After Apply")

build.gradle.test.kts

println("Applying")

tasks.test {
    val failedTests = mutableListOf<TestDescriptor>()
}

println("Applied")

both files are at the root project folder.

output...

>gradle clean build test

> Configure project :
Before Apply

FAILURE: Build failed with an exception.

* Where:
Build file 'D:UsersjamesdmcmillianProjectsFacilitatorLabsLabsgradle-dsl-script-variablesuild.gradle.kts' line: 6

* What went wrong:
Could not open cp_dsl generic class cache for script 'D:UsersjamesdmcmillianProjectsFacilitatorLabsLabsgradle-dsl-script-variablesuild.gradle.test.kts' (C:Usersjamesdmcmillian.gradlecaches6.8scripts7kclhsg56vjpfm1z90j
4hwepn).
> Could not compile script 'D:UsersjamesdmcmillianProjectsFacilitatorLabsLabsgradle-dsl-script-variablesuild.gradle.test.kts'.
   > startup failed:
     script 'D:UsersjamesdmcmillianProjectsFacilitatorLabsLabsgradle-dsl-script-variablesuild.gradle.test.kts': 4: expecting '}', found 'failedTests' @ line 4, column 9.
            val failedTests = mutableListOf<TestDescriptor>()
                ^

     1 error


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.8/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 4s

Is it possible to have variables/values contained inside applied scripts?
This seems like an awfully bad restriction if it's not.

These posts are close, but don't answer the variable question: Is there a way to split/factor out common parts of Gradle build

Also, I've read the kotlin-dsl primer, and not seeing anything specific to this issue, or maybe i missed it. The rest of the code works fine, so I've eliminated it from this question.
Any help is appreciated, thanks in advance.

Versions Info:
Gradle 6.8
Kotlin: 1.4.20
Groovy: 2.5.12
Ant: Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM: 11.0.2 (Oracle Corporation 11.0.2+9)
OS: Windows 10 10.0 amd64

question from:https://stackoverflow.com/questions/65944640/how-do-you-split-large-kotlin-based-gradle-build-files-with-variables

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...