本文介绍了如何将gradle生成的源文件夹添加到Eclipse项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的gradle项目使用注释处理器在gen / main / java内部生成一些java代码。当我将这个项目导入Eclipse时,Eclipse不会自动将gen / main / java作为源文件夹添加到构建路径中。我可以手动完成。但有没有一种方法来实现这一点?谢谢。
解决方案
如果您使用 gradle cleanEclipse eclipse
从命令行生成Eclipse项目,则Andreas的答案有效。如果您使用STS Eclipse Gradle插件,那么您必须在afterEclipseImport 任务中实现。以下是我的完整工作代码片段:
project.ext {
genSrcDir = projectDir.absolutePath +'/ gen / main / java'
}
compileJava {
options.compilerArgs + = ['-s',project.genSrcDir]
}
compileJava.doFirst {
任务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)
}
任务afterEclipseImport(描述:项目生成后的后处理,组: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)
}
}
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.
解决方案
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)
}
}
这篇关于如何将gradle生成的源文件夹添加到Eclipse项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!