Tuesday, September 18, 2012

Marking target directories derived for Eclipse Maven projects


Just a quick one.  It drives me crazy when I do a search in our maven-based Eclipse project and it includes all the class files etc found in target directories.  This is one way around it.

There may be better ways to do this because for one, Monkey scripting for Eclipse seems to be archived.  And two, I can see that Eclipse has a variety of change requests for supporting this natively.  But although I can see the support for derived files/folders in Eclipse, I couldn't see a built-in way for marking Maven target directories as derived in some automated way.  And Resource filtering was too aggressive as it affected the project building.

On the RHQ Project we had an older script to do this, written in Javascript and using an old Monkey script plugin.  This is an adapted script for a newer (although maybe still old) plugin called GroovyMonkey.  You can install it into Eclipse from here:

http://groovy-monkey.sourceforge.net/update 

Use the plugin to create a new script.  It will have given you a new Groovy Monkey entry on the main menu and will add a new GroovyMonkyScripts project for you.  Make the name (you'll need to check the 'override' box):

MakeTargetsDerived_Python.gm

Here is the script:

==================================================================
/* 
 * Menu: Make Targets Derived > Python
 * Script-Path: /GroovyMonkeyScripts/monkey/MakeTargetsDerived_Python.gm
 * Kudos: jshaughn
 * License: EPL 1.0
 * LANG: Python
 * Job: UIJob
 */

# Install GroovyMonkey from:
#   http://groovy-monkey.sourceforge.net/update
#

files = resources.filesMatching(".*/pom\\.xml")
for file in files:
  targetFolder = file.eclipseObject.parent.findMember("target")
  if targetFolder != None:
    targetFolder.setDerived(True)

files = resources.filesMatching(".*/dev-container/jbossas/bin/run\\.sh")
for file in files:
  files.eclipseObject.parent.parent.parent.setDerived(True)

window.getActivePage().showView( 'org.eclipse.ui.views.TaskList' )

==================================================================

Only the first for loop is working on the target folders, the second pass is just for our own benefit but shows that you can add as many different file pattern sections as you like.

Hope this helps..



No comments: