如何从Ant文件(build.xml)中提取属性到Java代码中



一旦build.xml文件被执行,我想检查build.xml中的一些属性来处理我的Java代码中的一些错误。

我的Ant文件(build.xml)是这样执行的(不是所有的代码都存在):
ILaunchConfigurationWorkingCopy workingCopy = type.newInstance(null,
                ITaskLauncherConstants.LAUNCHER_NAME);
/* Configure the ILaunchConfiguration (not all setAttribute calls are presents) :*/
workingCopy.setAttribute(IExternalToolConstants.ATTR_LOCATION, "build.xml");

/* Launch the task, where monitor is a IProgressMonitor */
ILaunch lc = workingCopy.launch(ILaunchManager.RUN_MODE, monitor);

在我的build。xml文件中我有一些属性:

<!-- In Ant File -->
<property name="fail.message" value="true" />

可以在Java代码中检查属性的内容吗?

/* In Java code */
lc.getProperty("fail.message");

当然,这意味着类将"记录"build.xml属性。

同样的问题已经问了6个月前,但答案是不工作(我试过):

如何在java代码中使用build.xml设置属性

谢谢你的建议

一种简单的方法是在ant构建脚本中使用echoproperties任务,这意味着将所有或所需的(通过echoproperties属性前缀)属性写入文件,然后在java中加载该文件。
从ant manual echoproperties, attribute destfile:

如果指定了该值,则表示要发送消息的文件名语句的输出。生成的输出文件是兼容的用于任何Java应用程序作为属性文件加载。如果不是

<echoproperties prefix="fail" destfile="what/ever/foo.properties"/>

最新更新