How to add gradle generated source folder to Eclipse project?
Asked Answered
B

3

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.

Bib answered 5/11, 2014 at 6:19 Comment(1)
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.Although
B
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

Bib answered 7/11, 2014 at 20:6 Comment(2)
Ah ok, I see but I am not using STS Eclipse Gradle plugin as it a bit unstable IMHO.Flirtatious
please provide link to the source :) linkQuench
F
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'.

Flirtatious answered 5/11, 2014 at 13:11 Comment(1)
You should do this in beforeMerged, as otherwise rerunning gradle eclipse will add a second classpath entry (and Eclipse will complain about that)Antagonize
H
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.

Herbert answered 29/11, 2017 at 16:8 Comment(1)
This gave me a build error from the IntelliJ environment and had to get rid of it.Skat
B
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

Bib answered 7/11, 2014 at 20:6 Comment(2)
Ah ok, I see but I am not using STS Eclipse Gradle plugin as it a bit unstable IMHO.Flirtatious
please provide link to the source :) linkQuench

© 2022 - 2025 — McMap. All rights reserved.