Eclipse JDT resource copy exclusion
What a nice article title…. clear as mud
So, what the article is really about relates to my playing with a postfix notation DSL; and specifically, adding IDE support within Eclipse. Since I’m going to be using Java bytecode as the underlying execution mechanism for the DSL it makes sense to try to reuse some of the Eclipse JDT functionality; but the JDT does more than I want. Specifically, any files that the JDT finds that cannot be processed by its builder are merely copied to the output directories (i.e. whilst .java files are compiled into .class files, all other files such as .xml or .postfix are merely copied).
Well, I don’t want that; I want the JDT to ignore my files. So we need to add a copy exclusion filter.
Thankfully, the JDT already supports this through a preference which can be set at a workspace level and overridden at a project level.
![]()
Expecting the users to have to add this filter themselves isn’t really acceptable and so it’s only fair that we add the filter at the same time as adding the relevant eclipse Nature to the project. It’s not difficult, and if you’ve done any plugin development, the following code snippet won’t need much explanation, so I’ll leave it to you to work it out.
/**
* Add an exclusion to the JDT resource copy mechanism
* to prevent Eclipse's Java compiler from copying the
* postfix files to the bin directory
* @param project Project having the exclusion added
*/
private static void
addPostFixResourceCopyExclusion(IJavaProject project) {
final String JDTPREF_EXCLUSIONFILTER =
"org.eclipse.jdt.core.builder.resourceCopyExclusionFilter";
//$NON-NLS-1$
final String POSTFIX_SUFFIX = "postfix"; //$NON-NLS-1$
Map optionsMap = project.getOptions(true);
String exclusions = (String)
optionsMap.get( JDTPREF_EXCLUSIONFILTER );
final String EXTENSION_MASK = "*."+POSTFIX_SUFFIX; //$NON-NLS-1$
// check to see if the exclusion is already there
if( exclusions.indexOf(EXTENSION_MASK) < 0 ) {
if( exclusions.length() == 0 ) {
exclusions = EXTENSION_MASK;
} else {
exclusions += "," + EXTENSION_MASK; //$NON-NLS-1$
}
project.setOption(JDTPREF_EXCLUSIONFILTER, exclusions);
}
}
For info, as with many things like this, I didn’t try to work it all out for myself, I looked for other examples and borrowed them. For example, this mechanism is already used by the Eclipse LaunchingPlugin to prevent the copying of .launch files (See org.eclipse.jdt.internal.launching.LaunchingPlugin).