Search in sources :

Example 91 with BuildException

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());
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) Properties(java.util.Properties)

Example 92 with BuildException

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());
    }
}
Also used : Commandline(org.apache.tools.ant.types.Commandline) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException)

Example 93 with BuildException

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;
    }
}
Also used : ArrayList(java.util.ArrayList) Resource(org.apache.tools.ant.types.Resource) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) Union(org.apache.tools.ant.types.resources.Union) StringTokenizer(java.util.StringTokenizer) IdentityMapper(org.apache.tools.ant.util.IdentityMapper) Resources(org.apache.tools.ant.types.resources.Resources) BuildException(org.apache.tools.ant.BuildException) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 94 with BuildException

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());
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) Properties(java.util.Properties)

Example 95 with BuildException

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);
    }
}
Also used : PropertyHelper(org.apache.tools.ant.PropertyHelper) BuildException(org.apache.tools.ant.BuildException)

Aggregations

BuildException (org.apache.tools.ant.BuildException)930 IOException (java.io.IOException)390 File (java.io.File)365 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)75 ArrayList (java.util.ArrayList)65 InputStream (java.io.InputStream)62 Project (org.apache.tools.ant.Project)61 Resource (org.apache.tools.ant.types.Resource)58 FileSet (org.apache.tools.ant.types.FileSet)52 Path (org.apache.tools.ant.types.Path)52 Commandline (org.apache.tools.ant.types.Commandline)51 Properties (java.util.Properties)50 OutputStream (java.io.OutputStream)44 FileOutputStream (java.io.FileOutputStream)42 FileResource (org.apache.tools.ant.types.resources.FileResource)42 FileInputStream (java.io.FileInputStream)41 URL (java.net.URL)40 BufferedReader (java.io.BufferedReader)37 Writer (java.io.Writer)37 MalformedURLException (java.net.MalformedURLException)37