Is there any way to define a constant value to Java at compile time

后端 未结 7 996
不知归路
不知归路 2020-12-18 22:59

When I used to write libraries in C/C++ I got into the habit of having a method to return the compile date/time. This was always a compiled into the library so would differe

7条回答
  •  [愿得一人]
    2020-12-18 23:28

    Personally I'd go for a separate properties file in your jar that you'd load at runtime... The classloader has a defined order for searching for files - I can't remember how it works exactly off hand, but I don't think another file with the same name somewhere on the classpath would be likely to cause issues.

    But another way you could do it would be to use Ant to copy your .java files into a different directory before compiling them, filtering in String constants as appropriate. You could use something like:

    public String getBuildDateTime() {
        return "@BUILD_DATE_TIME@";
    }
    

    and write a filter in your Ant file to replace that with a build property.

提交回复
热议问题