use of org.apache.tools.ant.taskdefs.LogOutputStream in project ant by apache.
the class Pvcs method execute.
/**
* @throws BuildException Something is stopping the build...
*/
@Override
public void execute() throws BuildException {
int result = 0;
if (repository == null || repository.trim().isEmpty()) {
throw new BuildException("Required argument repository not specified");
}
// Check workspace exists
// Launch PCLI listversionedfiles -z -aw
// Capture output
// build the command line from what we got the format is
Commandline commandLine = new Commandline();
commandLine.setExecutable(getExecutable(PCLI_EXE));
commandLine.createArgument().setValue("lvf");
commandLine.createArgument().setValue("-z");
commandLine.createArgument().setValue("-aw");
if (getWorkspace() != null) {
commandLine.createArgument().setValue("-sp" + getWorkspace());
}
commandLine.createArgument().setValue("-pr" + getRepository());
String uid = getUserId();
if (uid != null) {
commandLine.createArgument().setValue("-id" + uid);
}
// default pvcs project is "/"
if (getPvcsproject() == null && getPvcsprojects().isEmpty()) {
pvcsProject = "/";
}
if (getPvcsproject() != null) {
commandLine.createArgument().setValue(getPvcsproject());
}
if (!getPvcsprojects().isEmpty()) {
for (PvcsProject pvcsProject : getPvcsprojects()) {
String projectName = pvcsProject.getName();
if (projectName == null || projectName.trim().isEmpty()) {
throw new BuildException("name is a required attribute of pvcsproject");
}
commandLine.createArgument().setValue(projectName);
}
}
File tmp = null;
File tmp2 = null;
try {
Random rand = new Random(System.currentTimeMillis());
tmp = new File("pvcs_ant_" + rand.nextLong() + ".log");
OutputStream fos = Files.newOutputStream(tmp.toPath());
tmp2 = new File("pvcs_ant_" + rand.nextLong() + ".log");
log(commandLine.describeCommand(), Project.MSG_VERBOSE);
try {
result = runCmd(commandLine, new PumpStreamHandler(fos, new LogOutputStream(this, Project.MSG_WARN)));
} finally {
FileUtils.close(fos);
}
if (Execute.isFailure(result) && !ignorerc) {
String msg = "Failed executing: " + commandLine.toString();
throw new BuildException(msg, getLocation());
}
if (!tmp.exists()) {
throw new BuildException("Communication between ant and pvcs " + "failed. No output generated from executing PVCS " + "commandline interface \"pcli\" and \"get\"");
}
// Create folders in workspace
log("Creating folders", Project.MSG_INFO);
createFolders(tmp);
// Massage PCLI lvf output transforming '\' to '/' so get command works appropriately
massagePCLI(tmp, tmp2);
// Launch get on output captured from PCLI lvf
commandLine.clearArgs();
commandLine.setExecutable(getExecutable(GET_EXE));
if (getConfig() != null && getConfig().length() > 0) {
commandLine.createArgument().setValue("-c" + getConfig());
}
if (getForce() != null && getForce().equals("yes")) {
commandLine.createArgument().setValue("-Y");
} else {
commandLine.createArgument().setValue("-N");
}
if (getPromotiongroup() != null) {
commandLine.createArgument().setValue("-G" + getPromotiongroup());
} else {
if (getLabel() != null) {
commandLine.createArgument().setValue("-v" + getLabel());
} else {
if (getRevision() != null) {
commandLine.createArgument().setValue("-r" + getRevision());
}
}
}
if (updateOnly) {
commandLine.createArgument().setValue("-U");
}
commandLine.createArgument().setValue("@" + tmp2.getAbsolutePath());
log("Getting files", Project.MSG_INFO);
log("Executing " + commandLine.toString(), Project.MSG_VERBOSE);
result = runCmd(commandLine, new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN));
if (result != 0 && !ignorerc) {
String msg = "Failed executing: " + commandLine.toString() + ". Return code was " + result;
throw new BuildException(msg, getLocation());
}
} catch (FileNotFoundException e) {
String msg = "Failed executing: " + commandLine.toString() + ". Exception: " + e.getMessage();
throw new BuildException(msg, getLocation());
} catch (IOException e) {
String msg = "Failed executing: " + commandLine.toString() + ". Exception: " + e.getMessage();
throw new BuildException(msg, getLocation());
} catch (ParseException e) {
String msg = "Failed executing: " + commandLine.toString() + ". Exception: " + e.getMessage();
throw new BuildException(msg, getLocation());
} finally {
if (tmp != null) {
tmp.delete();
}
if (tmp2 != null) {
tmp2.delete();
}
}
}
use of org.apache.tools.ant.taskdefs.LogOutputStream in project ant by apache.
the class Symlink method loadLinks.
/**
* Load links from properties files included in one or more FileSets.
*
* <p>This method is only invoked when the action attribute is set to
* "recreate". The filesets passed in are assumed to specify the
* names of the property files with the link information and the
* subdirectories in which to look for them.</p>
*
* @param fileSets The <code>FileSet</code>s for this task.
* @return The links to be made.
*/
private Properties loadLinks(List<FileSet> fileSets) {
Properties finalList = new Properties();
// loop through the supplied file sets:
for (FileSet fs : fileSets) {
DirectoryScanner ds = new DirectoryScanner();
fs.setupDirectoryScanner(ds, getProject());
ds.setFollowSymlinks(false);
ds.scan();
File dir = fs.getDir(getProject());
// load included files as properties files:
for (String name : ds.getIncludedFiles()) {
File inc = new File(dir, name);
File pf = inc.getParentFile();
Properties lnks = new Properties();
try (InputStream is = new BufferedInputStream(Files.newInputStream(inc.toPath()))) {
lnks.load(is);
pf = pf.getCanonicalFile();
} catch (FileNotFoundException fnfe) {
handleError("Unable to find " + name + "; skipping it.");
continue;
} catch (IOException ioe) {
handleError("Unable to open " + name + " or its parent dir; skipping it.");
continue;
}
try {
lnks.store(new PrintStream(new LogOutputStream(this, Project.MSG_INFO)), "listing properties");
} catch (IOException ex) {
log("failed to log unshortened properties");
lnks.list(new PrintStream(new LogOutputStream(this, Project.MSG_INFO)));
}
// working directory:
for (String key : lnks.stringPropertyNames()) {
finalList.put(new File(pf, key).getAbsolutePath(), lnks.getProperty(key));
}
}
}
return finalList;
}
use of org.apache.tools.ant.taskdefs.LogOutputStream in project ant by apache.
the class JUnitTask method execute.
/**
* Execute a list of tests in a single forked Java VM.
* @param testList the list of tests to execute.
* @param thread Identifies which thread is test running in (0 for single-threaded runs)
* @throws BuildException on error.
*/
protected void execute(final List<JUnitTest> testList, final int thread) throws BuildException {
// Create a temporary file to pass the test cases to run to
// the runner (one test case per line)
final File casesFile = createTempPropertiesFile("junittestcases");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(casesFile))) {
log("Creating casesfile '" + casesFile.getAbsolutePath() + "' with content: ", Project.MSG_VERBOSE);
final PrintStream logWriter = new PrintStream(new LogOutputStream(this, Project.MSG_VERBOSE));
JUnitTest test = null;
for (JUnitTest t : testList) {
test = t;
test.setThread(thread);
printDual(writer, logWriter, test.getName());
if (test.getMethods() != null) {
printDual(writer, logWriter, ":" + test.getMethodsString().replace(',', '+'));
}
if (test.getTodir() == null) {
printDual(writer, logWriter, "," + getProject().resolveFile("."));
} else {
printDual(writer, logWriter, "," + test.getTodir());
}
if (test.getOutfile() == null) {
printlnDual(writer, logWriter, "," + "TEST-" + test.getName());
} else {
printlnDual(writer, logWriter, "," + test.getOutfile());
}
}
writer.flush();
// execute the test and get the return code
final ExecuteWatchdog watchdog = createWatchdog();
final TestResultHolder result = executeAsForked(test, watchdog, casesFile);
actOnTestResult(result, test, "Tests");
} catch (final IOException e) {
log(e.toString(), Project.MSG_ERR);
throw new BuildException(e);
} finally {
try {
FILE_UTILS.tryHardToDelete(casesFile);
} catch (final Exception e) {
log(e.toString(), Project.MSG_ERR);
}
}
}
use of org.apache.tools.ant.taskdefs.LogOutputStream in project ant by apache.
the class Javac12 method execute.
/**
* Run the compilation.
* @return true if the compiler ran with a zero exit result (ok)
* @exception BuildException if the compilation has problems.
*/
public boolean execute() throws BuildException {
attributes.log("Using classic compiler", Project.MSG_VERBOSE);
Commandline cmd = setupJavacCommand(true);
OutputStream logstr = new LogOutputStream(attributes, Project.MSG_WARN);
try {
// Create an instance of the compiler, redirecting output to
// the project log
Class<?> c = Class.forName(CLASSIC_COMPILER_CLASSNAME);
Constructor cons = c.getConstructor(OutputStream.class, String.class);
Object compiler = cons.newInstance(logstr, "javac");
// Call the compile() method
Method compile = c.getMethod("compile", String[].class);
return (Boolean) compile.invoke(compiler, new Object[] { cmd.getArguments() });
} catch (ClassNotFoundException ex) {
throw new BuildException("Cannot use classic compiler, as it is " + "not available. \n" + " A common solution is " + "to set the environment variable" + " JAVA_HOME to your jdk directory.\n" + "It is currently set to \"" + JavaEnvUtils.getJavaHome() + "\"", location);
} catch (Exception ex) {
if (ex instanceof BuildException) {
throw (BuildException) ex;
} else {
throw new BuildException("Error starting classic compiler: ", ex, location);
}
} finally {
FileUtils.close(logstr);
}
}
use of org.apache.tools.ant.taskdefs.LogOutputStream in project ant by apache.
the class Rpm method execute.
/**
* Execute the task
*
* @throws BuildException is there is a problem in the task execution.
*/
@Override
public void execute() throws BuildException {
Commandline toExecute = new Commandline();
toExecute.setExecutable(rpmBuildCommand == null ? guessRpmBuildCommand() : rpmBuildCommand);
if (topDir != null) {
toExecute.createArgument().setValue("--define");
toExecute.createArgument().setValue("_topdir " + topDir);
}
toExecute.createArgument().setLine(command);
if (cleanBuildDir) {
toExecute.createArgument().setValue("--clean");
}
if (removeSpec) {
toExecute.createArgument().setValue("--rmspec");
}
if (removeSource) {
toExecute.createArgument().setValue("--rmsource");
}
toExecute.createArgument().setValue("SPECS/" + specFile);
ExecuteStreamHandler streamhandler = null;
OutputStream outputstream = null;
OutputStream errorstream = null;
if (error == null && output == null) {
if (!quiet) {
streamhandler = new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN);
} else {
streamhandler = new LogStreamHandler(this, Project.MSG_DEBUG, Project.MSG_DEBUG);
}
} else {
if (output != null) {
OutputStream fos = null;
try {
// NOSONAR
fos = Files.newOutputStream(output.toPath());
BufferedOutputStream bos = new BufferedOutputStream(fos);
outputstream = new PrintStream(bos);
} catch (IOException e) {
FileUtils.close(fos);
throw new BuildException(e, getLocation());
}
} else if (!quiet) {
outputstream = new LogOutputStream(this, Project.MSG_INFO);
} else {
outputstream = new LogOutputStream(this, Project.MSG_DEBUG);
}
if (error != null) {
OutputStream fos = null;
try {
fos = Files.newOutputStream(error.toPath());
BufferedOutputStream bos = new BufferedOutputStream(fos);
errorstream = new PrintStream(bos);
} catch (IOException e) {
FileUtils.close(fos);
throw new BuildException(e, getLocation());
}
} else if (!quiet) {
errorstream = new LogOutputStream(this, Project.MSG_WARN);
} else {
errorstream = new LogOutputStream(this, Project.MSG_DEBUG);
}
streamhandler = new PumpStreamHandler(outputstream, errorstream);
}
Execute exe = getExecute(toExecute, streamhandler);
try {
log("Building the RPM based on the " + specFile + " file");
int returncode = exe.execute();
if (Execute.isFailure(returncode)) {
String msg = "'" + toExecute.getExecutable() + "' failed with exit code " + returncode;
if (failOnError) {
throw new BuildException(msg);
}
log(msg, Project.MSG_ERR);
}
} catch (IOException e) {
throw new BuildException(e, getLocation());
} finally {
FileUtils.close(outputstream);
FileUtils.close(errorstream);
}
}
Aggregations