Here the <targetIsOverwritten/> condition and a buildfile compiling and
testing the two conditions.
Test testAAndMainIsOverwritten is failing and therefore the algorithm
not fully right ...
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.taskdefs.condition.Condition;
import java.util.List;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
// inherited getProject(), getLocation() + log()
public class IsTargetOverwrittenCondition extends ProjectComponent
implements Condition {
// target to check
String target;
public void setTarget(String t) {
target = t;
}
// specific buildfile which should be overwritten?
String file;
public void setFile(String f) {
file = f;
}
public boolean eval() throws BuildException {
if (target == null) {
throw new BuildException("Must specify 'target' to test.",
getLocation());
}
if (!projectHasTarget(target)) {
// The target is not present, therefore it is also not
overwritten.
return false;
} else {
// imported targets are in BOTH namespace: <target> +
<buildfile>.<target>
List importedTargets = findTargetsByName(target);
if (importedTargets.isEmpty()) {
// only in the main buildfile, means 'not overwritten'
return false;
} else {
if (file == null) {
return checkTargetIdentity(target, importedTargets);
} else {
if (filterListByPrefix(importedTargets,
file).isEmpty()) {
return false;
} else {
return true;
}
}
}
}
}
private boolean projectHasTarget(String name) {
return getProject().getTargets().containsKey(target);
}
private List findTargetsByName(String name) {
ArrayList rv = new ArrayList();
for (Enumeration en = getProject().getTargets().keys();
en.hasMoreElements(); ) {
String tName = (String)en.nextElement();
if (tName.endsWith("." + name)) {
rv.add(tName);
}
}
return rv;
}
private List filterListByPrefix(List list, String prefix) {
ArrayList rv = new ArrayList();
for (Iterator it=list.iterator(); it.hasNext();) {
String name = (String)it.next();
if (name.startsWith(prefix)) {
rv.add(name);
}
}
return rv;
}
private boolean checkTargetIdentity(String target, List
otherTargetNames) {
Object original = getProject().getTargets().get(target);
for (Iterator it=otherTargetNames.iterator(); it.hasNext();) {
Object next = it.next();
if (original.equals(next)) return true;
}
return false;
}
}
<project xmlns:au="antlib:org.apache.ant.antunit" default="suite">
<import file="a.xml"/>
<import file="b.xml"/>
<target name="onlyMain"/>
<target name="AandMain"/>
<target name="BandMain"/>
<target name="all"/>
<target name="prepare">
<javac srcdir="." destdir="."/>
<typedef name="hastarget" classname="HasTargetCondition"
classpath="."/>
<typedef name="targetIsOverwritten"
classname="IsTargetOverwrittenCondition" classpath="."/>
</target>
<target name="suite" depends="prepare">
<au:antunit>
<fileset file="${ant.file}"/>
<au:plainlistener/>
</au:antunit>
</target>
<target name="testOnlyAExists">
<au:assertTrue>
<hastarget target="onlyA"/>
</au:assertTrue>
</target>
<target name="testXxxDoesNotExist">
<au:assertFalse>
<hastarget target="xxx"/>
</au:assertFalse>
</target>
<target name="testHasTargetFailsWithoutTarget">
<au:expectfailure expectedMessage="Must specify 'target' to
test.">
<condition property="p">
<hastarget/>
</condition>
</au:expectfailure>
</target>
<target name="testTargetIsOverwrittenFailsWithoutTarget">
<au:expectfailure expectedMessage="Must specify 'target' to
test.">
<condition property="p">
<targetIsOverwritten/>
</condition>
</au:expectfailure>
</target>
<target name="testOnlyAIsNotOverwritten">
<au:assertFalse>
<targetIsOverwritten target="onlyA"/>
</au:assertFalse>
</target>
<target name="testOnlyMainIsNotOverwritten">
<au:assertFalse>
<targetIsOverwritten target="onlyMain"/>
</au:assertFalse>
</target>
<target name="testAAndMainIsOverwritten">
<au:assertTrue>
<targetIsOverwritten target="AandMain"/>
</au:assertTrue>
</target>
<target name="testAAndMainIsOverwrittenFromA">
<au:assertTrue>
<targetIsOverwritten target="AandMain" file="a"/>
</au:assertTrue>
</target>
<target name="testAAndMainIsNotOverwrittenFromB">
<au:assertFalse>
<targetIsOverwritten target="AandMain" file="b"/>
</au:assertFalse>
</target>
</project>
<project name="a">
<target name="onlyA"/>
<target name="AandMain"/>
<target name="AandB"/>
<target name="all"/>
</project>
<project name="b">
<target name="onlyB"/>
<target name="BandMain"/>
<target name="AandB"/>
<target name="all"/>
</project>
Jan
>A quick implementation of <hastarget> would be:
>
>
>
>
>
>import org.apache.tools.ant.BuildException;
>import org.apache.tools.ant.Project;
>import org.apache.tools.ant.taskdefs.condition.Condition;
>
>public class HasTargetCondition implements Condition {
>
> // dynamically set by Ant runtime
> Project project;
> public void setProject(Project p) {
> project = p;
> }
> public Project getProject() {
> return project;
> }
>
> // target to check
> String target;
> public void setTarget(String t) {
> target = t;
> }
>
> //implements Condition
> public boolean eval() throws BuildException {
> if (target == null) {
> throw new BuildException("Must specify 'target' to test.");
> }
> return getProject().getTargets().containsKey(target);
> }
>}
>
>
>
> <typedef name="hastarget" classname="HasTargetCondition"
>classpath="."/>
> <condition property="x.onlyA.exists">
> <hastarget target="onlyA"/>
> </condition>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]