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

后端 未结 7 1003
不知归路
不知归路 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:11

    Unless you want to run your Java source through a C/C++ Preprocessor (which is a BIG NO-NO), use the jar method. There are other ways to get the correct resources out of a jar to make sure someone didn't put a duplicate resource on the classpath. You could also consider using the Jar manifest for this. My project does exactly what you're trying to do (with build dates, revisions, author, etc) using the manifest.

    You'll want to use this:

    Enumeration resources = Thread.currentThread().getContextClassLoader().getResources("META-INF/MANIFEST.MF");
    

    This will get you ALL of the manifests on the classpath. You can figure out which jar they can from by parsing the URL.

提交回复
热议问题