0

I am building a Java project into WAR file with different Ant scripts, e.g. buildDev.xml, buildUAT.xml, buildProduction.xml, etc...

Inside each script, I can use property to set different properties for the different builds, e.g.

<property name="myDir" value="foo" />

However, outside of the Ant script, I can't think of any ways to have certain blocks of Java code being included or excluded depending on different builds...

In C++, C#, or some other languages, I can use preprocessor like this:

#if defined(DEV_BUILD)
    runThisForDevBuild();
#elif defined(PRODUCTION_BUILD)
    runThisForProductionBuild();
#else
    printError();
#endif

I understand that preprocessor is not a thing in Java, but I wonder if Ant, using different means, would allow me to achieve something like the above in Java?

user2526586
  • 972
  • 2
  • 12
  • 27
  • Take a look at [this answer](https://stackoverflow.com/a/1922639/133203) – Federico klez Culloca May 12 '22 at 08:35
  • 1
    It's probably done that way on purpose in Java. And to be honest maintaining compiler switches can be a real nightmare. E.g. you can use interfaces and then different implementations for dev and production. And at runtime use a configuration file to create a new instance. A DI framework like Spring would help. So you can control the behaviour only by configuration. – vanje May 12 '22 at 13:46
  • Normally [ServiceLoader](https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/ServiceLoader.html) is used for this. You can include only the provider appropriate for a particular build in the application. – VGR May 12 '22 at 16:44

0 Answers0