Search in sources :

Example 41 with FileResource

use of org.apache.tools.ant.types.resources.FileResource in project groovy by apache.

the class Groovy method execute.

/**
 * Load the file and then execute it
 */
@Override
public void execute() throws BuildException {
    log.debug("execute()");
    command = command.trim();
    // process filesets
    for (FileSet next : filesets) {
        for (Resource res : next) {
            if (src == null) {
                src = res;
            } else {
                throw new BuildException("A single source resource must be provided!", getLocation());
            }
        }
    }
    if (src == null && command.length() == 0) {
        throw new BuildException("Source does not exist!", getLocation());
    }
    if (src != null && !src.isExists()) {
        throw new BuildException("Source resource does not exist!", getLocation());
    }
    if (outputEncoding == null || outputEncoding.isEmpty()) {
        outputEncoding = Charset.defaultCharset().name();
    }
    try {
        PrintStream out = System.out;
        try {
            if (output != null) {
                log.verbose("Opening PrintStream to output file " + output);
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output.getAbsolutePath(), append));
                out = new PrintStream(bos, false, outputEncoding);
            }
            // then read groovy statements in from a resource using the src attribute
            if (command == null || command.trim().length() == 0) {
                Reader reader;
                if (src instanceof FileResource) {
                    File file = ((FileResource) src).getFile();
                    createClasspath().add(new Path(getProject(), file.getParentFile().getCanonicalPath()));
                    if (encoding != null && !encoding.isEmpty()) {
                        reader = new LineNumberReader(new InputStreamReader(new FileInputStream(file), encoding));
                    } else {
                        reader = new CharsetToolkit(file).getReader();
                    }
                } else {
                    if (encoding != null && !encoding.isEmpty()) {
                        reader = new InputStreamReader(new BufferedInputStream(src.getInputStream()), encoding);
                    } else {
                        reader = new InputStreamReader(new BufferedInputStream(src.getInputStream()), Charset.defaultCharset());
                    }
                }
                readCommandFromReader(reader);
            } else {
                if (src != null) {
                    log.info("Ignoring supplied resource as direct script text found");
                }
            }
            if (command != null) {
                execGroovy(command, out);
            }
        } finally {
            if (out != null && out != System.out) {
                out.close();
            }
        }
    } catch (IOException e) {
        throw new BuildException(e, getLocation());
    }
    log.verbose("Statements executed successfully");
}
Also used : Path(org.apache.tools.ant.types.Path) PrintStream(java.io.PrintStream) FileSet(org.apache.tools.ant.types.FileSet) InputStreamReader(java.io.InputStreamReader) CharsetToolkit(groovy.util.CharsetToolkit) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) LineNumberReader(java.io.LineNumberReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) LineNumberReader(java.io.LineNumberReader) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) BuildException(org.apache.tools.ant.BuildException) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File)

Example 42 with FileResource

use of org.apache.tools.ant.types.resources.FileResource in project BroadleafCommerce by BroadleafCommerce.

the class DependencyLicenseCopy method execute.

@SuppressWarnings("unchecked")
public void execute() throws BuildException {
    super.execute();
    try {
        for (int i = 0; i < rcs.size(); i++) {
            ResourceCollection rc = (ResourceCollection) rcs.elementAt(i);
            Iterator<Resource> resources = rc.iterator();
            while (resources.hasNext()) {
                Resource r = (Resource) resources.next();
                if (!r.isExists()) {
                    continue;
                }
                if (r instanceof FileResource) {
                    FileResource fr = (FileResource) r;
                    String baseDir = fr.getBaseDir().getAbsolutePath();
                    String file = fr.getFile().getAbsolutePath();
                    file = file.substring(baseDir.length(), file.length());
                    String[] parts = file.split("/");
                    if (parts.length <= 1) {
                        parts = file.split("\\\\");
                    }
                    if (parts.length <= 1) {
                        throw new BuildException("Unable to recognize the path separator for src file: " + file);
                    }
                    String[] specificParts = new String[parts.length - 1];
                    System.arraycopy(parts, 0, specificParts, 0, specificParts.length);
                    String specificFilePart = StringUtils.join(specificParts, '/') + "/license.txt";
                    File specificFile = new File(licenseDir, specificFilePart);
                    File specificDestinationFile = new File(destDir, specificFilePart);
                    if (specificFile.exists()) {
                        fileUtils.copyFile(specificFile, specificDestinationFile);
                        continue;
                    }
                    String[] generalParts = new String[3];
                    System.arraycopy(parts, 0, generalParts, 0, 3);
                    String generalFilePart = StringUtils.join(generalParts, '/') + "/license.txt";
                    File generalFile = new File(licenseDir, generalFilePart);
                    if (generalFile.exists()) {
                        fileUtils.copyFile(generalFile, specificDestinationFile);
                        continue;
                    }
                    String[] moreGeneralParts = new String[2];
                    System.arraycopy(parts, 0, moreGeneralParts, 0, 2);
                    String moreGeneralFilePart = StringUtils.join(moreGeneralParts, '/') + "/license.txt";
                    File moreGeneralFile = new File(licenseDir, moreGeneralFilePart);
                    if (moreGeneralFile.exists()) {
                        fileUtils.copyFile(moreGeneralFile, specificDestinationFile);
                    }
                }
            }
        }
    } catch (IOException e) {
        throw new BuildException(e);
    }
}
Also used : Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) FileResource(org.apache.tools.ant.types.resources.FileResource) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) File(java.io.File) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 43 with FileResource

use of org.apache.tools.ant.types.resources.FileResource in project checkstyle by checkstyle.

the class CheckstyleAntTaskTest method testPathsDirectoryWithNestedFile.

@Test
public final void testPathsDirectoryWithNestedFile() throws IOException {
    // given
    TestRootModuleChecker.reset();
    final CheckstyleAntTaskLogStub antTask = new CheckstyleAntTaskLogStub();
    antTask.setConfig(getPath(CUSTOM_ROOT_CONFIG_FILE));
    antTask.setProject(new Project());
    final FileResource fileResource = new FileResource(antTask.getProject(), getPath(""));
    final Path sourcePath = new Path(antTask.getProject());
    sourcePath.add(fileResource);
    antTask.addPath(sourcePath);
    // when
    antTask.execute();
    // then
    assertWithMessage("Checker is not processed").that(TestRootModuleChecker.isProcessed()).isTrue();
    final List<File> filesToCheck = TestRootModuleChecker.getFilesToCheck();
    assertWithMessage("There are more files to check than expected").that(filesToCheck).hasSize(8);
    assertWithMessage("The path of file differs from expected").that(filesToCheck.get(5).getAbsolutePath()).isEqualTo(getPath(FLAWLESS_INPUT));
    assertWithMessage("Amount of logged messages in unexpected").that(antTask.getLoggedMessages()).hasSize(8);
}
Also used : Path(org.apache.tools.ant.types.Path) Project(org.apache.tools.ant.Project) FileResource(org.apache.tools.ant.types.resources.FileResource) CheckstyleAntTaskLogStub(com.puppycrawl.tools.checkstyle.internal.testmodules.CheckstyleAntTaskLogStub) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 44 with FileResource

use of org.apache.tools.ant.types.resources.FileResource in project liquibase by liquibase.

the class ChangeLogSyncTask method executeWithLiquibaseClassloader.

@Override
public void executeWithLiquibaseClassloader() throws BuildException {
    Liquibase liquibase = getLiquibase();
    OutputStreamWriter writer = null;
    try {
        FileResource outputFile = getOutputFile();
        if (outputFile != null) {
            writer = new OutputStreamWriter(outputFile.getOutputStream(), getOutputEncoding());
            liquibase.changeLogSync(toTag, new Contexts(getContexts()), getLabels(), writer);
        } else {
            liquibase.changeLogSync(toTag, new Contexts(getContexts()), getLabels());
        }
    } catch (UnsupportedEncodingException e) {
        throw new BuildException("Unable to generate sync SQL. Encoding [" + getOutputEncoding() + "] is not supported.", e);
    } catch (IOException e) {
        throw new BuildException("Unable to generate sync SQL. Error creating output writer.", e);
    } catch (LiquibaseException e) {
        throw new BuildException("Unable to sync change log: " + e.getMessage(), e);
    } finally {
        FileUtils.close(writer);
    }
}
Also used : Liquibase(liquibase.Liquibase) FileResource(org.apache.tools.ant.types.resources.FileResource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OutputStreamWriter(java.io.OutputStreamWriter) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) LiquibaseException(liquibase.exception.LiquibaseException) Contexts(liquibase.Contexts)

Example 45 with FileResource

use of org.apache.tools.ant.types.resources.FileResource in project liquibase by liquibase.

the class MarkNextChangeSetRanTask method executeWithLiquibaseClassloader.

@Override
public void executeWithLiquibaseClassloader() throws BuildException {
    Liquibase liquibase = getLiquibase();
    Writer writer = null;
    try {
        FileResource outputFile = getOutputFile();
        if (outputFile != null) {
            writer = getOutputFileWriter();
            liquibase.markNextChangeSetRan(new Contexts(getContexts()), getLabels(), writer);
        } else {
            liquibase.markNextChangeSetRan(new Contexts(getContexts()), getLabels());
        }
    } catch (LiquibaseException e) {
        throw new BuildException("Unable to mark next changeset as ran: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new BuildException("Unable to mark next changeset as ran. Error creating output writer.", e);
    } finally {
        FileUtils.close(writer);
    }
}
Also used : Liquibase(liquibase.Liquibase) FileResource(org.apache.tools.ant.types.resources.FileResource) LiquibaseException(liquibase.exception.LiquibaseException) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) Contexts(liquibase.Contexts) Writer(java.io.Writer)

Aggregations

FileResource (org.apache.tools.ant.types.resources.FileResource)48 File (java.io.File)29 BuildException (org.apache.tools.ant.BuildException)25 Resource (org.apache.tools.ant.types.Resource)23 IOException (java.io.IOException)15 FileProvider (org.apache.tools.ant.types.resources.FileProvider)10 Test (org.junit.Test)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Liquibase (liquibase.Liquibase)6 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)6 FileSet (org.apache.tools.ant.types.FileSet)6 Contexts (liquibase.Contexts)5 LiquibaseException (liquibase.exception.LiquibaseException)5 BufferedReader (java.io.BufferedReader)4 Reader (java.io.Reader)4 Writer (java.io.Writer)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Iterator (java.util.Iterator)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4