Search in sources :

Example 76 with BuildException

use of org.apache.tools.ant.BuildException in project ant by apache.

the class ManifestClassPath method setJarFile.

/**
 * The JAR file to contain the classpath attribute in its manifest.
 *
 * @param  jarfile the JAR file. Need not exist yet, but its parent
 *         directory must exist on the other hand.
 */
public void setJarFile(File jarfile) {
    File parent = jarfile.getParentFile();
    if (!parent.isDirectory()) {
        throw new BuildException("Jar's directory not found: %s", parent);
    }
    this.dir = parent;
}
Also used : BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 77 with BuildException

use of org.apache.tools.ant.BuildException in project ant by apache.

the class ManifestTask method execute.

/**
 * Create or update the Manifest when used as a task.
 *
 * @throws BuildException if the manifest cannot be written.
 */
@Override
public void execute() throws BuildException {
    if (manifestFile == null) {
        throw new BuildException("the file attribute is required");
    }
    Manifest toWrite = Manifest.getDefaultManifest();
    Manifest current = null;
    BuildException error = null;
    if (manifestFile.exists()) {
        Charset charset = Charset.forName(encoding == null ? "UTF-8" : encoding);
        try (InputStreamReader isr = new InputStreamReader(Files.newInputStream(manifestFile.toPath()), charset)) {
            current = new Manifest(isr);
        } catch (ManifestException m) {
            error = new BuildException("Existing manifest " + manifestFile + " is invalid", m, getLocation());
        } catch (IOException e) {
            error = new BuildException("Failed to read " + manifestFile, e, getLocation());
        }
    }
    // look for and print warnings
    for (Enumeration<String> e = nestedManifest.getWarnings(); e.hasMoreElements(); ) {
        log("Manifest warning: " + e.nextElement(), Project.MSG_WARN);
    }
    try {
        if ("update".equals(mode.getValue()) && manifestFile.exists()) {
            if (current != null) {
                toWrite.merge(current, false, mergeClassPaths);
            } else if (error != null) {
                throw error;
            }
        }
        toWrite.merge(nestedManifest, false, mergeClassPaths);
    } catch (ManifestException m) {
        throw new BuildException("Manifest is invalid", m, getLocation());
    }
    if (toWrite.equals(current)) {
        log("Manifest has not changed, do not recreate", Project.MSG_VERBOSE);
        return;
    }
    try (PrintWriter w = new PrintWriter(new OutputStreamWriter(Files.newOutputStream(manifestFile.toPath()), Manifest.JAR_ENCODING))) {
        toWrite.write(w, flattenClassPaths);
        if (w.checkError()) {
            throw new IOException("Encountered an error writing manifest");
        }
    } catch (IOException e) {
        throw new BuildException("Failed to write " + manifestFile, e, getLocation());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 78 with BuildException

use of org.apache.tools.ant.BuildException in project ant by apache.

the class Move method doFileOperations.

// ************************************************************************
// protected and private methods
// ************************************************************************
/**
 * Override copy's doFileOperations to move the files instead of copying them.
 */
@Override
protected void doFileOperations() {
    // Attempt complete directory renames, if any, first.
    if (completeDirMap.size() > 0) {
        for (Map.Entry<File, File> entry : completeDirMap.entrySet()) {
            File fromDir = entry.getKey();
            File toDir = entry.getValue();
            boolean renamed = false;
            try {
                log("Attempting to rename dir: " + fromDir + " to " + toDir, verbosity);
                renamed = renameFile(fromDir, toDir, filtering, forceOverwrite);
            } catch (IOException ioe) {
                String msg = "Failed to rename dir " + fromDir + " to " + toDir + " due to " + ioe.getMessage();
                throw new BuildException(msg, ioe, getLocation());
            }
            if (!renamed) {
                FileSet fs = new FileSet();
                fs.setProject(getProject());
                fs.setDir(fromDir);
                addFileset(fs);
                DirectoryScanner ds = fs.getDirectoryScanner(getProject());
                String[] files = ds.getIncludedFiles();
                String[] dirs = ds.getIncludedDirectories();
                scan(fromDir, toDir, files, dirs);
            }
        }
    }
    int moveCount = fileCopyMap.size();
    if (moveCount > 0) {
        // files to move
        log("Moving " + moveCount + " file" + ((moveCount == 1) ? "" : "s") + " to " + destDir.getAbsolutePath());
        for (Map.Entry<String, String[]> entry : fileCopyMap.entrySet()) {
            String fromFile = entry.getKey();
            File f = new File(fromFile);
            boolean selfMove = false;
            if (f.exists()) {
                // Is this file still available to be moved?
                String[] toFiles = entry.getValue();
                for (int i = 0; i < toFiles.length; i++) {
                    String toFile = toFiles[i];
                    if (fromFile.equals(toFile)) {
                        log("Skipping self-move of " + fromFile, verbosity);
                        selfMove = true;
                        // move will not occur, but that's what we want
                        continue;
                    }
                    File d = new File(toFile);
                    if ((i + 1) == toFiles.length && !selfMove) {
                        // Only try to move if this is the last mapped file
                        // and one of the mappings isn't to itself
                        moveFile(f, d, filtering, forceOverwrite);
                    } else {
                        copyFile(f, d, filtering, forceOverwrite);
                    }
                }
            }
        }
    }
    if (includeEmpty) {
        int createCount = 0;
        for (Map.Entry<String, String[]> entry : dirCopyMap.entrySet()) {
            String fromDirName = entry.getKey();
            boolean selfMove = false;
            for (String toDirName : entry.getValue()) {
                if (fromDirName.equals(toDirName)) {
                    log("Skipping self-move of " + fromDirName, verbosity);
                    selfMove = true;
                    continue;
                }
                File d = new File(toDirName);
                if (!d.exists()) {
                    if (!d.mkdirs() && !d.exists()) {
                        log("Unable to create directory " + d.getAbsolutePath(), Project.MSG_ERR);
                    } else {
                        createCount++;
                    }
                }
            }
            File fromDir = new File(fromDirName);
            if (!selfMove && okToDelete(fromDir)) {
                deleteDir(fromDir);
            }
        }
        if (createCount > 0) {
            log("Moved " + dirCopyMap.size() + " empty director" + (dirCopyMap.size() == 1 ? "y" : "ies") + " to " + createCount + " empty director" + (createCount == 1 ? "y" : "ies") + " under " + destDir.getAbsolutePath());
        }
    }
}
Also used : FileSet(org.apache.tools.ant.types.FileSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) Map(java.util.Map) File(java.io.File)

Example 79 with BuildException

use of org.apache.tools.ant.BuildException in project ant by apache.

the class Move method copyFile.

/**
 * Copy fromFile to toFile.
 * @param fromFile File
 * @param toFile File
 * @param filtering boolean
 * @param overwrite boolean
 */
private void copyFile(File fromFile, File toFile, boolean filtering, boolean overwrite) {
    try {
        log("Copying " + fromFile + " to " + toFile, verbosity);
        FilterSetCollection executionFilters = new FilterSetCollection();
        if (filtering) {
            executionFilters.addFilterSet(getProject().getGlobalFilterSet());
        }
        getFilterSets().forEach(executionFilters::addFilterSet);
        getFileUtils().copyFile(fromFile, toFile, executionFilters, getFilterChains(), forceOverwrite, getPreserveLastModified(), /* append: */
        false, getEncoding(), getOutputEncoding(), getProject(), getForce());
    } catch (IOException ioe) {
        throw new BuildException("Failed to copy " + fromFile + " to " + toFile + " due to " + ioe.getMessage(), ioe, getLocation());
    }
}
Also used : FilterSetCollection(org.apache.tools.ant.types.FilterSetCollection) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException)

Example 80 with BuildException

use of org.apache.tools.ant.BuildException in project ant by apache.

the class Nice method execute.

/**
 * Execute the task
 * @exception BuildException if something goes wrong with the build
 */
@Override
public void execute() throws BuildException {
    Thread self = Thread.currentThread();
    int priority = self.getPriority();
    if (currentPriority != null) {
        String current = Integer.toString(priority);
        getProject().setNewProperty(currentPriority, current);
    }
    // if there is a new priority, and it is different, change it
    if (newPriority != null && priority != newPriority) {
        try {
            self.setPriority(newPriority);
        } catch (SecurityException e) {
            // catch permissions denial and keep going
            log("Unable to set new priority -a security manager is in the way", Project.MSG_WARN);
        } catch (IllegalArgumentException iae) {
            throw new BuildException("Priority out of range", iae);
        }
    }
}
Also used : 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