Use targetOutputDir instead.
targetOutputDir.set(project.layout.projectDirectory.dir(targetDir))
The path to the target directory for the generated lexer.