Reputation: 32273
I have a Java configuration file with constants.
I'm using ant to replace the values of the constant according to the build. In a precompile target:
<replace
file="Config.java"
value="default"
propertyFile="${build.env}.properties">
<replacefilter
token="@mytoken@"
property="myprop.x"/>
</replace>
Works well. But after I run this my source file is modified. So if I run it again it will not replace anything because @mytoken@ was replaced the first time.
I don't want to put Config.java outside of the project because I want it to work with eclipse and would get lot of compile errors if the file is not where expected.
I was thinking about replacing back in a post build target or something, but not sure if that's secure. If the build fails or the user interrupts the script it will not run and the value will not be set back.
Any help? Thanks in advance.
Upvotes: 0
Views: 1498
Reputation: 17321
When I had to deal with this task, I went about it a different way. Instead of editing a real source file, the ant script always makes a file named Version.java
. Version.java
is never checked into the repository, but the interface Version
implements is. This way, you don't have to be statically dependent on the existence of the generated file.
public String getVersionHelper() {
try {
Class versionClass = Class.forName("Version");
IVersion version = (IVersion) versionClass.newInstance()
return version.getVersion();
} catch (ClassNotFoundException ex) {
return "NO VERSION";
}
}
The key point is that official builds are always done with ant, not eclipse. This allows you to run in eclipse for testing and still compile successfully.
Upvotes: 1