简体   繁体   中英

How to extract properties from Ant file (build.xml) into Java code

Once the build.xml file is executed, I want to check some properties in the build.xml to handle some errors in my Java code.

For infos, my Ant file (build.xml) is executed this way (not all the code is present):

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);

And in my build.xml file I have some properties :

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

Is it possible to check the content of the property in Java code ? Something like :

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

Of course, this would mean that the class would "record" the build.xml properties.

Same question has been asked 6 months ago, but the answer is not working (I tried):

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

Thanks for any suggestion.

A simple approach would be using echoproperties task in your ant buildscript, means writing all or desired (via echoproperties attribute prefix) properties to a file and load that file in java afterwards.
From ant manual echoproperties , attribute 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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM