Creating a program that will run before and after compilation in Eclipse

931 views Asked by At

I am currently working on a small program that should comment out some code used for testing. I want it to auto run before the compiler while compiling the release version and another program that will comment the code back in after compilation was over.

The program works the only thing I am missing is to add it to the build process. Thanks to all helpers!

2

There are 2 answers

2
JB Nizet On BEST ANSWER

Don't use Eclipse to build the release version of your app. Use Ant, Maven, or any other build tool that is much more flexible than Eclipse, doesn't need a GUI, can be scripted and used by a continuous integration server.

All of these tools should easily be used to include your pre-compilation and post-compilation tasks in the build process.

That said, you could just use a public static final boolean constant FOR_TEST, and include all your testing code in

if (TestUtil.FOR_TEST) {
}

You would then have just a single place to change in the code to have all the test code removed from the compiled version. No need for a complex Java program to do that.

Or you could let all the testing code in the released version, and activate it when testing using a system property, for example. This wouldn't even need any precompilation/postcompilation phase, and would probably have a negligible cost.

0
Eli Acherkan On

In Eclipse, right-click a project, choose Properties → Builders and click New. You can add an Ant script or a command line that Eclipse will trigger when building the project. You can also control the order of builders in the same dialog.

However, I agree with JB Nizet – there are many advantages to having Ant or Maven build your project.