Browse Source

Bug 7552 auditing of <ant> - antFile, dir and target need to be reset

at the end of execute to get back to the state before execute has been
called.

Add some documentation while I was at it.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272332 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 23 years ago
parent
commit
55adbd06c9
1 changed files with 53 additions and 8 deletions
  1. +53
    -8
      src/main/org/apache/tools/ant/taskdefs/Ant.java

+ 53
- 8
src/main/org/apache/tools/ant/taskdefs/Ant.java View File

@@ -97,7 +97,10 @@ public class Ant extends Task {
/** the basedir where is executed the build file */ /** the basedir where is executed the build file */
private File dir = null; private File dir = null;


/** the build.xml file (can be absolute) in this case dir will be ignored */
/**
* the build.xml file (can be absolute) in this case dir will be
* ignored
*/
private String antFile = null; private String antFile = null;


/** the target to call if any */ /** the target to call if any */
@@ -142,13 +145,27 @@ public class Ant extends Task {
inheritRefs = value; inheritRefs = value;
} }


/**
* Creates a Project instance for the project to call.
*/
public void init() { public void init() {
newProject = new Project(); newProject = new Project();
newProject.setJavaVersionProperty(); newProject.setJavaVersionProperty();
newProject.addTaskDefinition("property", newProject.addTaskDefinition("property",
(Class)project.getTaskDefinitions().get("property"));
(Class) project.getTaskDefinitions()
.get("property"));
} }


/**
* Called in execute or createProperty of newProject is null.
*
* <p>This can happen if the same instance of this task is run
* twice as newProject is set to null at the end of execute (to
* save memory and help the GC).</p>
*
* <p>Sets all properties that have been defined as nested
* property elements.</p>
*/
private void reinit() { private void reinit() {
init(); init();
final int count = properties.size(); final int count = properties.size();
@@ -169,6 +186,13 @@ public class Ant extends Task {
} }
} }


/**
* Attaches the build listeners of the current project to the new
* project, configures a possible logfile, transfers task and
* data-type definitions, transfers properties (either all or just
* the ones specified as user properties to the current project,
* depending on inheritall).
*/
private void initializeProject() { private void initializeProject() {
Vector listeners = project.getBuildListeners(); Vector listeners = project.getBuildListeners();
final int count = listeners.size(); final int count = listeners.size();
@@ -245,6 +269,11 @@ public class Ant extends Task {
} }
} }


/**
* Pass output sent to System.out to the new project.
*
* @since Ant 1.5
*/
protected void handleOutput(String line) { protected void handleOutput(String line) {
if (newProject != null) { if (newProject != null) {
newProject.demuxOutput(line, false); newProject.demuxOutput(line, false);
@@ -253,6 +282,11 @@ public class Ant extends Task {
} }
} }


/**
* Pass output sent to System.err to the new project.
*
* @since Ant 1.5
*/
protected void handleErrorOutput(String line) { protected void handleErrorOutput(String line) {
if (newProject != null) { if (newProject != null) {
newProject.demuxOutput(line, true); newProject.demuxOutput(line, true);
@@ -265,6 +299,9 @@ public class Ant extends Task {
* Do the execution. * Do the execution.
*/ */
public void execute() throws BuildException { public void execute() throws BuildException {
File savedDir = dir;
String savedAntFile = antFile;
String savedTarget = target;
try { try {
if (newProject == null) { if (newProject == null) {
reinit(); reinit();
@@ -292,7 +329,6 @@ public class Ant extends Task {
File file = FileUtils.newFileUtils().resolveFile(dir, antFile); File file = FileUtils.newFileUtils().resolveFile(dir, antFile);
antFile = file.getAbsolutePath(); antFile = file.getAbsolutePath();



log("calling target "+(target!=null?target:"[default]") log("calling target "+(target!=null?target:"[default]")
+ " in build file "+ antFile.toString(), + " in build file "+ antFile.toString(),
Project.MSG_VERBOSE); Project.MSG_VERBOSE);
@@ -325,6 +361,9 @@ public class Ant extends Task {
//ignore //ignore
} }
} }
dir = savedDir;
antFile = savedAntFile;
target = savedTarget;
} }
} }


@@ -344,7 +383,7 @@ public class Ant extends Task {
/** /**
* Add the references explicitly defined as nested elements to the * Add the references explicitly defined as nested elements to the
* new project. Also copy over all references that don't override * new project. Also copy over all references that don't override
* existing references in the new project if inheritall has been
* existing references in the new project if inheritrefs has been
* requested. * requested.
*/ */
private void addReferences() throws BuildException { private void addReferences() throws BuildException {
@@ -356,7 +395,8 @@ public class Ant extends Task {
Reference ref = (Reference)e.nextElement(); Reference ref = (Reference)e.nextElement();
String refid = ref.getRefId(); String refid = ref.getRefId();
if (refid == null) { if (refid == null) {
throw new BuildException("the refid attribute is required for reference elements");
throw new BuildException("the refid attribute is required"
+ " for reference elements");
} }
if (!thisReferences.containsKey(refid)) { if (!thisReferences.containsKey(refid)) {
log("Parent project doesn't contain any reference '" log("Parent project doesn't contain any reference '"
@@ -422,8 +462,8 @@ public class Ant extends Task {
// ignore this if the class being referenced does not have // ignore this if the class being referenced does not have
// a set project method. // a set project method.
} catch(Exception e2) { } catch(Exception e2) {
String msg = "Error setting new project instance for reference with id "
+ oldKey;
String msg = "Error setting new project instance for "
+ "reference with id " + oldKey;
throw new BuildException(msg, e2, location); throw new BuildException(msg, e2, location);
} }
} }
@@ -431,7 +471,7 @@ public class Ant extends Task {
} }


/** /**
* ...
* Set the dir attribute.
*/ */
public void setDir(File d) { public void setDir(File d) {
this.dir = d; this.dir = d;
@@ -457,6 +497,11 @@ public class Ant extends Task {
this.target = s; this.target = s;
} }


/**
* Set the name of a log file. This will be resolved relative to
* the dir attribute if specified, relative to the current
* project's basedir otherwise.
*/
public void setOutput(String s) { public void setOutput(String s) {
this.output = s; this.output = s;
} }


Loading…
Cancel
Save