假设我们有一个 PrintTarget 类,其中包含 main()。
我们可以这样构建/编译吗,我们应该能够以某种方式在 main.js 中发送构建目标名称。当调用 main() 时,它将打印构建目标名称。
我不想在构建时使用目标名称。我们都知道该怎么做。 我希望生成的 JAR 记住目标名称。 假设我们创建了 jar printTarget.jar 并执行 jar #>java printTarget.jar 我希望此调用打印目标名称。
最佳答案
要将数据从构建时传递到运行时,您需要将数据写入存储在 JAR 中的文件中,例如
<jar destfile="printTarget.jar">
<fileset dir="${classes.dir}" />
<manifest>
<attribute name="Main-Class" value="com.example.Main" />
</manifest>
<!-- This is just a way to create a zip entry from inline text in the build
file without having to <echo> it to a real file on disk first -->
<mappedresources>
<mergemapper to="com/example/buildinfo.properties"/>
<string encoding="ISO-8859-1"># this is a generated file, do not edit
targetname=custom-build-1
</string>
</mappedresources>
</jar>
然后你可以在你的主要方法中阅读
package com.example;
import java.io.*;
import java.util.Properties;
public class Main {
public static void main(String[] args) throws Exception {
Properties buildInfo = new Properties();
InputStream is = Main.class.getResourceAsStream("buildinfo.properties");
try {
buildInfo.load(is);
} finally {
is.close();
}
System.out.println("Build target was " +
buildInfo.getProperty("targetname", "<unknown>"));
}
}
这应该打印
Build target was custom-build-1
关于java - 如何在 Java 中打印构建目标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13654299/