Is it possible to have gradle mark the build directory as derived in eclipse


(Josh Brackett) #1

This would help when trying to open resources (ctrl-shift-r). Today all the .class files in the gradle created build directories show up.


(Ben Manes) #2

Yes, see the code sample below. This was derived from a previous poster’s code. However a slight bug resulted in an unstable Eclipse project which a Gradle STS developer kindly helped me debug.

/**
 * Configures the project in Eclipse.
 *
 * @see http://gradle.org/docs/current/userguide/eclipse_plugin.html
 */
apply plugin: "eclipse"
  // For the Gradle Eclipse plugin
eclipse.project.file.withXml { provider ->
  ignoreDerivedResources(provider.asNode())
}
  // For the Eclipse STS plugin
task afterEclipseImport(description: "Post processing after project generation", group: "IDE") {
  ext.srcFile = file(".project")
  inputs.file srcFile
  outputs.dir srcFile
    onlyIf { srcFile.exists() }
    doLast {
    def projectDescription = new XmlParser().parse(srcFile)
    ignoreDerivedResources(projectDescription)
      def writer = new FileWriter(srcFile)
    def printer = new XmlNodePrinter(new PrintWriter(writer))
    printer.setPreserveWhitespace(true)
    printer.print(projectDescription)
  }
}
  def ignoreDerivedResources(projectDescription, directories = ["build", "target", "test-output"]) {
  def count = directories.count { file(it).exists() }
  if (count > 0) {
    def filter = projectDescription
      .appendNode("filteredResources")
      .appendNode("filter")
    filter.appendNode("id", System.currentTimeMillis().toString().trim())
    filter.appendNode("type", "26")
    filter.appendNode("name")
    def matcher = filter.appendNode("matcher")
    matcher.appendNode("id", "org.eclipse.ui.ide.orFilterMatcher")
    def arguments = matcher.appendNode("arguments")
    directories.each {
      if (file(it).exists()) {
        def dirMatcher = arguments.appendNode("matcher")
        dirMatcher.appendNode("id", "org.eclipse.ui.ide.multiFilter")
        dirMatcher.appendNode("arguments", "1.0-projectRelativePath-matches-false-false-${it}")
      }
    }
  }
}

(Josh Brackett) #3

Thanks Benjamin this seems to work pretty well. Would be nice if this happened automatically, but one of the strengths of gradle has been how easily it can be extended to do new things.


(Ben Manes) #4

Watch STS-2832