有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

对于infos,我的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);

以我的身材。xml文件我有一些属性:

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

是否可以在Java代码中检查属性的内容类似于:

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

当然,这意味着类将“记录”构建。xml属性

6个月前也有人问过同样的问题,但答案不起作用(我试过):

How to set a property in java code using build.xml

谢谢你的建议


共 (1) 个答案

  1. # 1 楼答案

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

    If specified, the value indicates the name of the file to send the output of the statement to. The generated output file is compatible for loading by any Java application as a property file. If not specified, then the output will go to the Apache Ant log.

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