I have a problem with the intersection of Kotlin, ...
# getting-started
n
I have a problem with the intersection of Kotlin, Gradle, Antlr, and IntelliJ. I use Antlr4 to generate a parser. I can compile and run unit tests using this parser from the command line. However, I cannot do the same in IntellJ, because it produces compile errors for the import statements of the generated classes. The classes are generated in
build/generated-src/antlr/main
and have a
package com.example.antlr
first line (i.e. there is a mismatch between the directory path and the package path, but AFAIK the Koltin compiler does not care about that). I tried to fix the compile error using
Copy code
val antlrGeneratedFilesDirectory = file("${buildDir}/generated-src/antlr/main")
idea {
    module {
        generatedSourceDirs.add(antlrGeneratedFilesDirectory)
        sourceDirs.add(antlrGeneratedFilesDirectory)
    }
}
but that did not help either. Anyone has an idea what I could try to fix this? BTW: I can navigate in IntelliJ to the generated sources from the import statement, so IntelliJ does somehow find the files.
v
Iirc if you move your
g4
file from
src/main/antlr
to
src/main/antlr/com/example/antlr/
, the files should also land in the proper subdirectory tree.
but AFAIK the Koltin compiler does not care about that
Does ANTLR now also support generating Kotlin files? If not, which I assume, then this statement is irrelevant, as you are compiling Java files there, not Kotlin files, and afaik Kotlin compiler does not do joint compilation. And regarding having the files marked as generated sources in IntelliJ, this should suffice:
Copy code
val generateGrammarSource by tasks.existing(AntlrTask::class)
idea.module {
    generatedSourceDirs.add(generateGrammarSource.get().outputDirectory)
}
n
I tried to move the .g4 files, but my parser has a separate lexer and parser .g4, and whe nI move them into the subdir, the parser somehow does not see the lexer output.
I just switched my "gradle build" settings in IntelliJ from "use IntelliJ" to use "gradle", and everything started to work!!!
I will try the suggested change and report back here
v
That's the default anyway, yeah, as Gradle often as the more correct information about a build. ­čÖé
n
I now only use the following (have dropped the `idea`configuration because it works from cmdline and from within IntelliJ w/o it after switching to gradle build)
Copy code
kotlin {
    sourceSets.main.get().kotlin.srcDirs += tasks.generateGrammarSource.get().outputDirectory
}
e
use
.srcDir(tasks.generateGrammarSource)
instead, it'll get the task dependencies right
n
can you elaborate on this? I can't find an object which has a
srcDir
method.
e
where you have
.srcDirs += tasks.generateGrammarSource.get().outputDirectory
, should work as
.srcDir(tasks.generateGrammarSource)
(or something like
tasks.generateGrammarSource.flatMap { it.outputDirectory }
if it has multiple outputs and you need to specify which one)
n
ok, I think I got it. I only have a single output directory from antlr, and thus now use
Copy code
kotlin {
    sourceSets.main.get().kotlin.srcDir(tasks.generateGrammarSource)
}
153 Views