Search code examples
spring-bootgradlegroovy

Can not import groovy class in settings.gradle for java project in IDEA


I created a spring boot project which uses gradle. The project has a settings.gradle file, and i used groovy class in this file but can not be imported. The code is used to include sub modules.

import groovy.io.FileType  // can not be imported
import java.nio.file.Paths

rootProject.name = 'godlenfire'
String rootPath = rootProject.projectDir.path

['app'].forEach {
    File dir = Paths.get(rootPath, it).toFile()
    dir.traverse([maxDepth: 3, nameFilter: 'build.gradle', type: FileType.FILES]) {
        def module = relativePath(it.parent).replace File.separator, Project.PATH_SEPARATOR
        include module
        logger.lifecycle("include ${module}")
    }
}

Thanks.

enter image description here

I excepted to import groovy class in .gradle file successfully.


Here is the gradle-wrapper.properties

gradle-wrapper.properties


Solution

  • I was able to reproduce your issue by playing with Preferences | Build, Execution, Deployment | Build Tools | Gradle | Use Gradle from option in IntelliJ. The idea is that this value should point to the valid Gradle binaries.

    In my example below I'm using Gradle wrapper which means that Gradle will always be downloaded if it's missing.

    point to a valid directory with Gradle binaries

    However, when I reproduce the issue I'm just using the "Specified location" option that points to a random directory without Gradle binaries which causes my IntelliJ to "break". point to a directory without Gradle binaries

    import is broken

    So, make sure that option is configured properly in your IntelliJ. You can also make sure that you are able to run gradle commands within the terminal as I mentioned in my comment to the question (if you are using Gradle wrapper you should likely run ./gradlew commands).