How to add gradle source folder to Eclipse project?

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

Thanks.

+6
source share
3 answers

Andreas's answer works if you create an Eclipse project from the command line using gradle cleanEclipse eclipse . If you are using the STS Eclipse Gradle plugin, then you need to implement the 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

+1
source

You can easily add the created folder manually to the classpath with

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

resulting in null as the second arg constructor means that Eclipse should put the compiled "cool" files in the default output folder. If you want to change this, just specify String instead, for example. 'Ben-gene'.

+7
source

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

Add this to your build.gradle :

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

This will result in the following line generated in .classpath :

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

I tested this with Gradle 4.1, but I suspect that it will work with older versions too.

+2
source

Source: https://habr.com/ru/post/977748/


All Articles