use of org.apache.tools.ant.BuildException in project ant by apache.
the class Definer method loadProperties.
/**
* Load type definitions as properties from a URL.
*
* @param al the classloader to use
* @param url the url to get the definitions from
*/
protected void loadProperties(ClassLoader al, URL url) {
try (InputStream is = url.openStream()) {
if (is == null) {
log("Could not load definitions from " + url, Project.MSG_WARN);
return;
}
Properties props = new Properties();
props.load(is);
for (String key : props.stringPropertyNames()) {
name = key;
classname = props.getProperty(name);
addDefinition(al, name, classname);
}
} catch (IOException ex) {
throw new BuildException(ex, getLocation());
}
}
use of org.apache.tools.ant.BuildException in project ant by apache.
the class Patch method execute.
/**
* execute patch
* @throws BuildException when it all goes a bit pear shaped
*/
@Override
public void execute() throws BuildException {
if (!havePatchfile) {
throw new BuildException("patchfile argument is required", getLocation());
}
Commandline toExecute = (Commandline) cmd.clone();
toExecute.setExecutable(PATCH);
if (originalFile != null) {
toExecute.createArgument().setFile(originalFile);
}
Execute exe = new Execute(new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN), null);
exe.setCommandline(toExecute.getCommandline());
if (directory == null) {
exe.setWorkingDirectory(getProject().getBaseDir());
} else {
if (!directory.isDirectory()) {
throw new BuildException(directory + " is not a directory.", getLocation());
}
exe.setWorkingDirectory(directory);
}
log(toExecute.describeCommand(), Project.MSG_VERBOSE);
try {
int returncode = exe.execute();
if (Execute.isFailure(returncode)) {
String msg = "'" + PATCH + "' failed with exit code " + returncode;
if (failOnError) {
throw new BuildException(msg);
}
log(msg, Project.MSG_ERR);
}
} catch (IOException e) {
throw new BuildException(e, getLocation());
}
}
use of org.apache.tools.ant.BuildException in project ant by apache.
the class PathConvert method execute.
/**
* Do the execution.
* @throws BuildException if something is invalid.
*/
@Override
public void execute() throws BuildException {
Resources savedPath = path;
// may be altered in validateSetup
String savedPathSep = pathSep;
// may be altered in validateSetup
String savedDirSep = dirSep;
try {
// If we are a reference, create a Path from the reference
if (isReference()) {
Object o = refid.getReferencedObject(getProject());
if (!(o instanceof ResourceCollection)) {
throw new BuildException("refid '%s' does not refer to a resource collection.", refid.getRefId());
}
getPath().add((ResourceCollection) o);
}
// validate our setup
validateSetup();
// Currently, we deal with only two path formats: Unix and Windows
// And Unix is everything that is not Windows
// (with the exception for NetWare and OS/2 below)
// for NetWare and OS/2, piggy-back on Windows, since here and
// in the apply code, the same assumptions can be made as with
// windows - that \\ is an OK separator, and do comparisons
// case-insensitive.
String fromDirSep = onWindows ? "\\" : "/";
StringBuilder rslt = new StringBuilder();
ResourceCollection resources = isPreserveDuplicates() ? path : new Union(path);
List<String> ret = new ArrayList<>();
FileNameMapper mapperImpl = mapper == null ? new IdentityMapper() : mapper.getImplementation();
for (Resource r : resources) {
String[] mapped = mapperImpl.mapFileName(String.valueOf(r));
for (int m = 0; mapped != null && m < mapped.length; ++m) {
ret.add(mapped[m]);
}
}
boolean first = true;
for (String string : ret) {
// Apply the path prefix map
String elem = mapElement(string);
if (!first) {
rslt.append(pathSep);
}
first = false;
StringTokenizer stDirectory = new StringTokenizer(elem, fromDirSep, true);
while (stDirectory.hasMoreTokens()) {
String token = stDirectory.nextToken();
rslt.append(fromDirSep.equals(token) ? dirSep : token);
}
}
// unless setonempty == false
if (setonempty || rslt.length() > 0) {
String value = rslt.toString();
if (property == null) {
log(value);
} else {
log("Set property " + property + " = " + value, Project.MSG_VERBOSE);
getProject().setNewProperty(property, value);
}
}
} finally {
path = savedPath;
dirSep = savedDirSep;
pathSep = savedPathSep;
}
}
use of org.apache.tools.ant.BuildException in project ant by apache.
the class Property method loadFile.
/**
* load properties from a file
* @param file file to load
* @throws BuildException on error
*/
protected void loadFile(File file) throws BuildException {
Properties props = new Properties();
log("Loading " + file.getAbsolutePath(), Project.MSG_VERBOSE);
try {
if (file.exists()) {
try (InputStream fis = Files.newInputStream(file.toPath())) {
loadProperties(props, fis, file.getName().endsWith(".xml"));
}
addProperties(props);
} else {
log("Unable to find property file: " + file.getAbsolutePath(), Project.MSG_VERBOSE);
}
} catch (IOException ex) {
throw new BuildException(ex, getLocation());
}
}
use of org.apache.tools.ant.BuildException in project ant by apache.
the class PropertyHelperTask method execute.
/**
* Execute the task.
* @throws BuildException on error.
*/
@Override
public void execute() throws BuildException {
if (getProject() == null) {
throw new BuildException("Project instance not set");
}
if (propertyHelper == null && delegates == null) {
throw new BuildException("Either a new PropertyHelper or one or more PropertyHelper delegates are required");
}
PropertyHelper ph = propertyHelper;
if (ph == null) {
ph = PropertyHelper.getPropertyHelper(getProject());
} else {
ph = propertyHelper;
}
synchronized (ph) {
if (delegates != null) {
for (Object o : delegates) {
PropertyHelper.Delegate delegate = o instanceof DelegateElement ? ((DelegateElement) o).resolve() : (PropertyHelper.Delegate) o;
log("Adding PropertyHelper delegate " + delegate, Project.MSG_DEBUG);
ph.add(delegate);
}
}
}
if (propertyHelper != null) {
log("Installing PropertyHelper " + propertyHelper, Project.MSG_DEBUG);
// TODO copy existing properties to new PH?
getProject().addReference(MagicNames.REFID_PROPERTY_HELPER, propertyHelper);
}
}
Aggregations