10

My gradle project generates some java code inside gen/main/java using annotation processor. When I import this project into Eclipse, Eclipse will not automatically add gen/main/java as source folder to buildpath. I can do it manually. But is there a way to automate this?

Thanks.

codefx
  • 9,872
  • 16
  • 53
  • 81
  • Do you have a more detailed example? I can think of two possible reasons why the source folder isn't added. 1) the source folder isn't part of the model gradle has of your source folders... or 2) when the project is imported, the folder doesn't exist yet. Whether you are in case '1' or '2' a different solution might be in order. So if you could determine which it is that would help. One way to try this is to make sure you create the folder (even if it is empty) before importing. If the folder is now getting added, then it must be because of the 2nd reason. – Kris Nov 07 '14 at 18:38

3 Answers3

13

You can easily add the generated folder manually to the classpath by

eclipse {
    classpath {
        file.whenMerged { cp ->
            cp.entries.add( new org.gradle.plugins.ide.eclipse.model.SourceFolder('gen/main/java', null) )
        }
    }
}

whereby null as a second constructor arg means that Eclipse should put the compiled "class" files within the default output folder. If you want to change this, just provide a String instead, e.g. 'bin-gen'.

user2219808
  • 542
  • 6
  • 15
Andreas Schmid
  • 1,195
  • 8
  • 13
  • 3
    You should do this in `beforeMerged`, as otherwise rerunning `gradle eclipse` will add a _second_ classpath entry (and Eclipse will complain about that) – Alice Purcell Jun 13 '16 at 14:57
3

I think it's a little bit cleaner just to add a second source directory to the main source set.

Add this to your build.gradle:

sourceSets {
    main {
        java {
            srcDirs += ["src/gen/java"]
        }
    }
}

This results in the following line generated in your .classpath:

<classpathentry kind="src" path="src/gen/java"/>

I've tested this with Gradle 4.1, but I suspect it'd work with older versions as well.

jstricker
  • 2,132
  • 1
  • 30
  • 44
0

Andreas' answer works if you generate Eclipse project from command line using gradle cleanEclipse eclipse. If you use STS Eclipse Gradle plugin, then you have to implement afterEclipseImport task. Below is my full working snippet:

project.ext {
  genSrcDir = projectDir.absolutePath + '/gen/main/java'
}
compileJava {
  options.compilerArgs += ['-s', project.genSrcDir]
}
compileJava.doFirst {
  task createGenDir << {
    ant.mkdir(dir: project.genSrcDir)
  }
  createGenDir.execute()
  println 'createGenDir DONE'
}
eclipse.classpath.file.whenMerged {
  classpath - >
    def genSrc = new org.gradle.plugins.ide.eclipse.model.SourceFolder('gen/main/java', null)
  classpath.entries.add(genSrc)
}
task afterEclipseImport(description: "Post processing after project generation", group: "IDE") {
  doLast {
    compileJava.execute()
    def classpath = new XmlParser().parse(file(".classpath"))
    new Node(classpath, "classpathentry", [kind: 'src', path: 'gen/main/java']);
    def writer = new FileWriter(file(".classpath"))
    def printer = new XmlNodePrinter(new PrintWriter(writer))
    printer.setPreserveWhitespace(true)
    printer.print(classpath)
  }
}

enter image description here

codefx
  • 9,872
  • 16
  • 53
  • 81
  • Ah ok, I see but I am not using STS Eclipse Gradle plugin as it a bit unstable IMHO. – Andreas Schmid Nov 10 '14 at 13:52
  • please provide link to the source :) [link](http://blog.tamalsaha.com/post/102154319104/how-to-add-gradle-generated-source-folder-to) – Nico Aug 25 '16 at 07:16