I would like to do conditional compilation in a program of mine. I know that if you declare a public static final boolean the compiler will ignore the branch that is not traversed. Is it possible to have an ant target change a variable before building the program?
For example if I have:
final public static boolean windows = false;
I would like two ant targets: Windows and Mac.
I would like the command
ant windows
to change the boolean to true, while
ant mac
leaves the variable as is.
Thanks.
You can get Ant to modify a properties file, and then you can read this file in your application pretty easily:
new Properties(new FileInputStream("filename" / new File(filename))),and read properties using:
Boolean isWindows = new Boolean(properties.getProperty("windows"))or:
String os = properties.getProperty("os").You can use the Ant
PropertyFiletask for doing this: http://ant.apache.org/manual/Tasks/propertyfile.html.Edit: here’s an alternative using another task if you absolutely must edit the source code file using Ant:
<replaceregexp file="blah.java" match="public static final boolean WINDOWS = \"(.*)\"" replace="public static final boolean WINDOWS = \"" + ${properties.windows} + "\"" />— replace the code as your own as required. See http://ant.apache.org/manual/Tasks/replaceregexp.html for details.