Best way to get the gradle build file (as File) (Kotlin or Groovy) from a project

Answered

Hello,

To retrieve the project Gradle build file (build.gradle or build.gradle.kts), I used the following code:

project.basePath?.let {
val connection = GradleConnector.newConnector().forProjectDirectory(File(it)).connect()
val model = connection.model(GradleProject::class.java)
val gradleProjectModel = model.get()
val sourceFile = gradleProjectModel?.buildScript?.sourceFile?
...
}

Unfortunately, when there is an error in the content of the build.gradle(.kts) file the "model.get()" fails with an exception.

Is there another way to retrieve the build.gradle(.kts) file even if there are some errors in the file content?

I can of course use:

project.basePath?.let {
val file1 = File(it, "build.gradle")
val file2 = File(it, "build.gradle.kts")
}

but It's a little bit hard-coded!

Thanks a lot for help

2 comments
Comment actions Permalink

If you know the exact directory where the Gradle script file resides, you can try

 

 org.jetbrains.plugins.gradle.service.settings.GradleConfigLocator#adjust

0
Comment actions Permalink

Thanks Nikita for this answer. I'll try this solution.

0

Please sign in to leave a comment.