How to suppress Java warnings for specific directories or files such as generated code
Starting with version 3.8 M6, Eclipse (to be exact: the JDT) has built-in functionality for this. It is configurable through a project's build path: Project properties > Java Build Path > Compiler > Source
Announced here: Eclipse 3.8 and 4.2 M6 - New and Noteworthy, called Selectively ignore errors/warnings from source folders. That's also where the screenshot is from. This is the new feature developed on the previously linked Bug 220928.
There is a ticket for this, Bug 220928, that has since been completed for Eclipse 3.8. Please see this answer for details.
If you're stuck with Eclipse 3.7 or lower: The user "Marc" commenting on that ticket created (or at least links to) a plugin called 'warningcleaner' in comment 35. I'm using that with a lot of success while waiting for this feature to be integrated into Eclipse.
It's really quite simple:
- Install plugin.
- Right-click project and select "Add/remove generated code nature".
- Open the project settings (right-click and select "properties").
- Open the tab 'Warning Cleaner'.
- Select the source folders you want to ignore the warnings from.
I solved this by using the maven regexp replace plugin - it does not solve the cause, but heals the pain:
<plugin> <groupId>com.google.code.maven-replacer-plugin</groupId> <artifactId>maven-replacer-plugin</artifactId> <version>1.3.2</version> <executions><execution> <phase>prepare-package</phase> <goals> <goal>replace</goal> </goals></execution> </executions> <configuration><includes> <include>target/generated-sources/antlr/**/*.java</include></includes><regex>true</regex><regexFlags> <regexFlag>MULTILINE</regexFlag></regexFlags><replacements> <replacement> <token>^public class</token> <value>@SuppressWarnings("all") public class</value> </replacement></replacements> </configuration></plugin>
Note that I did not manage to get the ** notation to work, so you might have to specify path exactly.
See comment below for an improvement on how not to generate duplicate @SupressWarnings