1

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!

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
Mashiah
  • 111
  • 1
  • 6

2 Answers2

4

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.

Community
  • 1
  • 1
Eli Acherkan
  • 6,401
  • 2
  • 27
  • 34
3

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • But will it compile the code above? I know that in the C/C++ compiler some stuff are been ignored by the compiler, but I am not so sure about the Java one. I have a class that adds logs to the code and it loos like: Log.l("stuff"); and I want to have no calls in the production so the question is will adding this in the Log class will prevent some redundent calculations when the users will use my program? - it is Android related so I want to make is as simple as I can for the device. Again thanks! – Mashiah Feb 21 '12 at 08:29
  • If TestUtil.FOR_TEST is a boolean constant evaluating to false, then the code in the if block, as I said, will be removed from the compiled class. It must be syntactically correct and compile, but the compiler won't include the if block in the byte code. – JB Nizet Feb 21 '12 at 09:18