Java – best practices for code modification during ant build

Admittedly, this doesn't sound like a best practice, but let me explain During the build, we need to paste the build number and system version into a class whose sole purpose is to contain these values and make them accessible

Our first idea is to use system properties, but due to the volatility of the deployment environment (another way is "system administrators are doing strange, evil and creepy things"), we want to hard code them

Basically, I see four possibilities to realize it in ants:

>Use < replace > on a token in the class

The problem with this method is that the file is changed, so you must replace the token with < replacegexp > after compilation... Sooo is ugly. I don't want to touch the source code with regular expressions Add time dependence. > Copy file, copy, compile copy, delete copy

One must remember the sequence - the original class must be compiled before it can be overwritten by the copy Time dependence is also ugly. > Copy the file, replace the token on the original, compile, and replace the stained original with a copy

Unless embedded in the compilation target, the same time dependency is ugly because all our build files use the same import compilation target. > Create files from scratch / store files outside the source path in the build script

Since there is no time dependency, the first three are improved, but the compiler / IDE is very dissatisfied because it ignores classes The red mark is disturbingly ugly

What do you think of alternatives?

Are there any best practices for this?

I certainly hope I missed a perfect way

thank you

Edit our final use list, store the build number and system version in the implementation version attribute, and cancel MyClass class. getPackage(). getImplementationVersion(). I found that this solution is one of the answers to this thread, which was commented by andersoj

Solution

I think the simpler way is to make your version Java class contains simple from jar Read from the properties file and generate this at build time in ant build Properties file For example, simply generate:

build.number = 142
build.timestamp = 5/12/2011 12:31

Build in < build number > task in ant is half finished (see the second example)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>