Browse Source

Add target-group, a special target that must be empty and whose dependency list may be extended by other target(-group)s using their (new) target-group attribute

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@718943 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 16 years ago
parent
commit
2077f6716f
4 changed files with 177 additions and 7 deletions
  1. +47
    -0
      src/main/org/apache/tools/ant/TargetGroup.java
  2. +31
    -5
      src/main/org/apache/tools/ant/helper/ProjectHelper2.java
  3. +13
    -2
      src/main/org/apache/tools/ant/taskdefs/AntStructure.java
  4. +86
    -0
      src/tests/antunit/core/target-group-test.xml

+ 47
- 0
src/main/org/apache/tools/ant/TargetGroup.java View File

@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.tools.ant;

/**
* A special kind of target that must be empty.
*
* @since Ant 1.8.0
*/
public class TargetGroup extends Target {

// no "clone" constructor since I'm not really sure where it is
// used

private static final String NO_CHILDREN_ALLOWED
= "you must not nest child elements into a target-group";

/**
* Throws an exception.
*/
public final void addTask(Task task) {
throw new BuildException(NO_CHILDREN_ALLOWED);
}

/**
* Throws an exception.
*/
public final void addDataType(RuntimeConfigurable r) {
throw new BuildException(NO_CHILDREN_ALLOWED);
}
}

+ 31
- 5
src/main/org/apache/tools/ant/helper/ProjectHelper2.java View File

@@ -24,6 +24,7 @@ import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import org.apache.tools.ant.RuntimeConfigurable;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.TargetGroup;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.UnknownElement;
import org.apache.tools.ant.util.FileUtils;
@@ -772,18 +773,20 @@ public class ProjectHelper2 extends ProjectHelper {
*
* @exception org.xml.sax.SAXParseException if the tag given is not
* <code>"taskdef"</code>, <code>"typedef"</code>,
* <code>"property"</code>, <code>"target"</code>
* <code>"property"</code>, <code>"target"</code>,
* <code>"target-group"</code>
* or a data type definition
*/
public AntHandler onStartChild(String uri, String name, String qname, Attributes attrs,
AntXMLContext context) throws SAXParseException {
return name.equals("target") && (uri.equals("") || uri.equals(ANT_CORE_URI))
? ProjectHelper2.targetHandler : ProjectHelper2.elementHandler;
return (name.equals("target") || name.equals("target-group"))
&& (uri.equals("") || uri.equals(ANT_CORE_URI))
? ProjectHelper2.targetHandler : ProjectHelper2.elementHandler;
}
}

/**
* Handler for "target" elements.
* Handler for "target" and "target-group" elements.
*/
public static class TargetHandler extends AntHandler {

@@ -811,9 +814,11 @@ public class ProjectHelper2 extends ProjectHelper {
AntXMLContext context) throws SAXParseException {
String name = null;
String depends = "";
String targetGroup = null;

Project project = context.getProject();
Target target = new Target();
Target target = "target".equals(tag)
? new Target() : new TargetGroup();
target.setProject(project);
target.setLocation(new Location(context.getLocator()));
context.addTarget(target);
@@ -843,6 +848,8 @@ public class ProjectHelper2 extends ProjectHelper {
}
} else if (key.equals("description")) {
target.setDescription(value);
} else if (key.equals("target-group")) {
targetGroup = value;
} else {
throw new SAXParseException("Unexpected attribute \"" + key + "\"", context
.getLocator());
@@ -869,6 +876,9 @@ public class ProjectHelper2 extends ProjectHelper {
+ " specify a name attribute");
}
name = prefix + sep + name;
if (targetGroup != null) {
targetGroup = prefix + sep + targetGroup;
}
}

// Check if this target is in the current build file
@@ -910,6 +920,22 @@ public class ProjectHelper2 extends ProjectHelper {
context.getCurrentTargets().put(newName, newTarget);
project.addOrReplaceTarget(newName, newTarget);
}
if (targetGroup != null) {
if (!projectTargets.containsKey(targetGroup)) {
throw new BuildException("can't add target "
+ name + " to target-group "
+ targetGroup
+ " because the target-group"
+ " is unknown.");
}
Target t = (Target) projectTargets.get(targetGroup);
if (!(t instanceof TargetGroup)) {
throw new BuildException("referenced target "
+ targetGroup
+ " is not a target-group");
}
t.addDependency(name);
}
}

private String getTargetPrefix(AntXMLContext context) {


+ 13
- 2
src/main/org/apache/tools/ant/taskdefs/AntStructure.java View File

@@ -225,7 +225,7 @@ public class AntStructure extends Task {

out.println("");

out.print("<!ELEMENT project (target | ");
out.print("<!ELEMENT project (target | target-group | ");
out.print(TASKS);
out.print(" | ");
out.print(TYPES);
@@ -247,13 +247,24 @@ public class AntStructure extends Task {
out.print(TYPES);
out.println(")*>");
out.println("");
printTargetAttrs(out, "target");
out.println("<!ELEMENT target-group EMPTY>");
out.println("");
printTargetAttrs(out, "target-group");
}

out.println("<!ATTLIST target");
/**
* Prints the definition for the target element.
*/
private void printTargetAttrs(PrintWriter out, String tag) {
out.print("<!ATTLIST ");
out.println(tag);
out.println(" id ID #IMPLIED");
out.println(" name CDATA #REQUIRED");
out.println(" if CDATA #IMPLIED");
out.println(" unless CDATA #IMPLIED");
out.println(" depends CDATA #IMPLIED");
out.println(" target-group CDATA #IMPLIED");
out.println(" description CDATA #IMPLIED>");
out.println("");
}


+ 86
- 0
src/tests/antunit/core/target-group-test.xml View File

@@ -0,0 +1,86 @@
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns:au="antlib:org.apache.ant.antunit" default="antunit">

<import file="../antunit-base.xml"/>

<target-group name="testTargetGroupWorksLikeTarget"
depends="setProperty, assertProperty"/>

<target name="setProperty">
<property name="foo" value="bar"/>
</target>

<target name="assertProperty">
<au:assertPropertyEquals name="foo" value="bar"/>
</target>

<target name="testTargetGroupMustBeEmpty">
<mkdir dir="${output}"/>
<echo file="${output}/build.xml"><![CDATA[
<project>
<target-group name="foo">
<echo>bar</echo>
</target-group>
</project>]]></echo>
<au:expectfailure
expectedMessage="you must not nest child elements into a target-group">
<ant dir="${output}"/>
</au:expectfailure>
</target>

<target name="testAddToTargetGroup">
<mkdir dir="${output}"/>
<echo file="${output}/build.xml"><![CDATA[
<project default="foo">
<target-group name="foo"/>
<target name="bar" target-group="foo">
<echo>In target bar</echo>
</target>
</project>]]></echo>
<ant dir="${output}"/>
<au:assertLogContains text="In target bar"/>
</target>

<target name="testTargetGroupMustBeKnown">
<mkdir dir="${output}"/>
<echo file="${output}/build.xml"><![CDATA[
<project default="foo">
<target-group name="bar" target-group="foo"/>
<target-group name="foo"/>
</project>]]></echo>
<au:expectfailure
expectedMessage="can't add target bar to target-group foo because the target-group is unknown">
<ant dir="${output}"/>
</au:expectfailure>
</target>

<target name="testCantAddToPlainTarget">
<mkdir dir="${output}"/>
<echo file="${output}/build.xml"><![CDATA[
<project default="foo">
<target name="foo"/>
<target name="bar" target-group="foo"/>
</project>]]></echo>
<au:expectfailure
expectedMessage="referenced target foo is not a target-group">
<ant dir="${output}"/>
</au:expectfailure>
</target>

</project>

Loading…
Cancel
Save