Monday, June 9, 2014

Eclipse: Setting "derived" folders (Groovy Monkey script)

Using Eclipse with Java or Android projects you may have noticed "Open Resource" (CTRL+SHIFT+R) showing files from "bin" folder(s) that you don't want to edit as Eclipse will overwrite them on next build.


Per Eclipse documentation for Derived resources:
A derived resource is a regular file or folder that is created in the course of translating, compiling, copying, or otherwise processing other files. Derived resources are not original data, and can be recreated from other resources. It is commonplace to exclude derived resources from version and configuration management because they would otherwise clutter the team repository with version of these ever-changing files as each user regenerates them.
You could manually update the flag on each resource (Properties) but that is not persisted in a source repository so it is not set automatically when downloading a new project or will get lost when removing and recreating the particular resource.


This Groovy Monkey script will help you set the "derived" flag on chosen resources quickly, especially handy if having multiple projects in the same workspace. Copy and save into a "monkey" folder inside of your project.

/*
 * Menu: Set "derived" folders
 * Script-Path: /Your project/monkey/SetDerivedFolders.gm
 * Kudos: Dan Dar3 <dan.dar33@gmail.com>
 * License: EPL 1.0
 * LANG: Groovy
 * Job: UIJob
 * DOM: http://groovy-monkey.sourceforge.net/update/net.sf.groovyMonkey.dom
 */

import org.eclipse.core.resources.IFolder;

def derivedNames = [ "bin", "gen", "libs", "target" ]

out.println("");
out.println("Derived folders:");
out.println("~~~~~~~~~~~~~~~~");

def projects = workspace.getRoot().getProjects();
for (project in projects) {
  if (project.isOpen()) {
    for (member in project.members()) {
      if (member instanceof IFolder) {
        if (derivedNames.contains(member.getName())) {
          out.println(member.getFullPath().toString()); 
          member.setDerived(true);
        }
      }
    }
  }
}

If you have installed the Groovy Monkey plugin (see links at the end) you will get the script in the Groovy Monkey menu at the top where you can run it from.



Run it again whenever you see generated resources creeping into the "Open Resource" listings (making sure you don't have the "Show derived resources" checked though :-)


Resources:

No comments :

Post a Comment