IntelliJ + groovy DSL: How to exclude files from being compiled by groovy plugin?

7.5k views Asked by At

I am working on a Java web project that uses Liquibase groovy DSL to managae DB changes. For the sake of this topic, it could be any other 3rd party library that uses *.groovy files as sources. The project is built with gradle. In one of my modules (dao-base) under the src/main/resources folder I have groovy files (changelog01.groovy, master_changelog.groovy etc.). these files should be loaded by the liquibase jar at runtime.

Now when I try to make the project in IntelliJ it gives the following error message:

Groovyc: Cannot compile Groovy files: no Groovy library is defined for module 'dao-base'.

I understood that the groovy plugin detects *.groovy files, tries to compile them and unsurprisingly fails. these are groovy DSL files that should be loaded only by the 3rd party liquibase parser and I don't need IntelliJ's groovy plugin to try and compile them.

I managed to come up with 2 partial solutions: 1. disabling groovy plugin in intellij. The problem with this solution is that the gradle plugin depends on the groovy plugin and thus is automatically disabled when disabling the groovy plugin. I need the gradle plugin enabled. 2. excluding the src/main/resources folder in project settings --> modules --> dao-base (my module) --> sources tab. The problem with this solution is that when I build the project and deploy to tomcat, the files from the resources folder are missing and since the files in it are required in runtime, I get file not found exception when the war loads.

I was hoping someone could come up with a better solution for this problem.

3

There are 3 answers

1
Anton Arhipov On BEST ANSWER

Try removing the groovy file suffix from the compiler's resources list:

Settings -> Compiler

IntelliJ IDEA Compiler Settings

1
Patel On

I was facing same issue. I disabled groovy plugin from setting after that successfully compile as well as executed my project.

0
Rob Smith On

Are you generating the IntelliJ project with the Gradle Idea Plugin? If so, you can add those groovy files to the compiler resources list in IntelliJ via gradle by adding this to build.gradle:

idea {
  project {
    // customize the .ipr file generation.
    ipr {
      withXml { xmlProvider ->
        def project = xmlProvider.asNode()

        // Add appropriate groovy files to the compiler resources. These files will not be compiled by the groovy compiler but will be copied to the classes directory during compilation.
        def compilerSettings = project.component.find { it.@name == 'CompilerConfiguration' }
        compilerSettings.wildcardResourcePatterns*.appendNode('entry', [name: '*/changelog*.groovy'])
        compilerSettings.wildcardResourcePatterns*.appendNode('entry', [name: '*/master_changelog.groovy'])
      }

    }
  }
}

This is nice because you will not have to remember to do this manually when you generate the project from gradle.