I'm looking for a way to compile a few flavors of my Java software using Ant. By flavors I mean, for instance, trial and full version.
In the code I have a
public static final boolean TRIAL
variable which specifies whether it is trial or full version. It would be very nice to be able to set it from Ant and to compile both versions automatically.
I could not find a good wa开发者_如何学Goy to do this, while I do believe that I'm not the first one to face this problem.
In C I would simple use "ifdef" and set the define from Makefile....
Look at this: http://code.google.com/p/preprocessor/
Allows you to do stuff like this:
//#ifdef CUSTOMER1
private String abc = "abc";
//#else
private String abc = "cba";
//#endif
and set it from ant
I'd create a template file and use ANT's copy filters to change the value before compiling. I've never done it with source code, but I've used it quite a bit for configuration files.
In a source template file, you could do:
public static final boolean TRIAL = @TRIAL_VALUE@
Then in the ant build.xml, you could do this:
<filter token="TRIAL_VALUE" value="true" />
<copy tofile="${your.target.file.name.here}" filtering="true">
<fileset dir="${location.of.your.template}">
<include name="${template.file}" />
</fileset>
</copy>
I'm not sure I like the idea of doing this with a real source file (I think there's a good chance it would make the IDE angry). You might want to consider using a configuration file embedded in the jar (and use the copy filter technique on that instead)
If you have several versions of your software to avoid a easy hacking (java decompiler) I strong suggest you the use of parallel set of sources where the trial are just mocks, and so on.
With ANT you could choose the source of exactly what version will use.
Of course that this duplicate the sources, but is one possible solution without use of external software (like previous preprocessor)
精彩评论