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");
}
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);
}
}
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);
}
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);
}
}
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);
}
}
Aggregations