You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by bo...@apache.org on 2008/11/11 10:36:52 UTC

svn commit: r713005 - in /ant/core/trunk: ./ docs/manual/CoreTasks/ src/main/org/apache/tools/ant/ src/main/org/apache/tools/ant/helper/ src/main/org/apache/tools/ant/taskdefs/ src/tests/antunit/taskdefs/ src/tests/antunit/taskdefs/importtests/

Author: bodewig
Date: Tue Nov 11 01:36:51 2008
New Revision: 713005

URL: http://svn.apache.org/viewvc?rev=713005&view=rev
Log:
add 'as' attribute to <import> in order to control the imported target's prefixes

Added:
    ant/core/trunk/src/tests/antunit/taskdefs/import-test.xml   (with props)
    ant/core/trunk/src/tests/antunit/taskdefs/importtests/
    ant/core/trunk/src/tests/antunit/taskdefs/importtests/a.xml   (with props)
    ant/core/trunk/src/tests/antunit/taskdefs/importtests/b.xml   (with props)
Modified:
    ant/core/trunk/WHATSNEW
    ant/core/trunk/docs/manual/CoreTasks/import.html
    ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelper.java
    ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ImportTask.java

Modified: ant/core/trunk/WHATSNEW
URL: http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=713005&r1=713004&r2=713005&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Tue Nov 11 01:36:51 2008
@@ -516,6 +516,9 @@
    STARTTLS.
    Bugzilla Report 46063.
 
+ * <import> has a new attribute "as" that can be used to control the
+   prefix prepended to the imported target's names.
+
 Changes from Ant 1.7.0 TO Ant 1.7.1
 =============================================
 

Modified: ant/core/trunk/docs/manual/CoreTasks/import.html
URL: http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTasks/import.html?rev=713005&r1=713004&r2=713005&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/import.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/import.html Tue Nov 11 01:36:51 2008
@@ -62,6 +62,10 @@
 This enables the new implementation to call the old target, thus
 <i>enhancing</i> it with tasks called before or after it.</p>
 
+<p>If you use the <i>as</i> attribute of the task, its value will be
+  used to prefix the overriden target's name instead of the name
+  attribute of the project tag.</p>
+
 <h4>Special Properties</h4>
 
 <p>Imported files are treated as they are present in the main
@@ -152,6 +156,17 @@
       </td>
       <td valign="top" align="center">No</td>
     </tr>
+    <tr>
+      <td valign="top">
+        as
+      </td>
+      <td valign="top">
+        Specifies the prefix prepended to the target names.  If
+        ommitted, the name attribute of the project tag of the
+        imported file will be used.
+      </td>
+      <td valign="top" align="center">No</td>
+    </tr>
   </tbody>
 </table>
 

Modified: ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelper.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelper.java?rev=713005&r1=713004&r2=713005&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelper.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelper.java Tue Nov 11 01:36:51 2008
@@ -115,6 +115,34 @@
         return importStack;
     }
 
+    private final static ThreadLocal targetPrefix = new ThreadLocal() {
+            protected Object initialValue() {
+                return (String) null;
+            }
+        };
+
+    /**
+     * The prefix to prepend to imported target names.
+     *
+     * <p>May be set by &lt;import&gt;'s as attribute.</p>
+     *
+     * @return the configured prefix or null
+     *
+     * @since ant 1.8.0
+     */
+    public static String getCurrentTargetPrefix() {
+        return (String) targetPrefix.get();
+    }
+
+    /**
+     * Sets the prefix to prepend to imported target names.
+     *
+     * @since ant 1.8.0
+     */
+    public static void setCurrentTargetPrefix(String prefix) {
+        targetPrefix.set(prefix);
+    }
+
     // --------------------  Parse method  --------------------
     /**
      * Parses the project file, configuring the project as it goes.

Modified: ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java?rev=713005&r1=713004&r2=713005&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java Tue Nov 11 01:36:51 2008
@@ -612,11 +612,6 @@
                 && (uri.equals("") || uri.equals(ANT_CORE_URI))) {
                 return ProjectHelper2.projectHandler;
             }
-//            if (context.importlevel > 0) {
-//                // we are in an imported file. Allow top-level <target>.
-//                if (qname.equals( "target" ) )
-//                    return ProjectHelper2.targetHandler;
-//            }
             if (name.equals(qname)) {
                 throw new SAXParseException("Unexpected element \"{" + uri
                     + "}" + name + "\" {" + ANT_CORE_URI + "}" + name, context.getLocator());
@@ -869,11 +864,12 @@
             if (depends.length() > 0) {
                 target.setDepends(depends);
             }
-            if (context.isIgnoringProjectTag() && context.getCurrentProjectName() != null
-                    && context.getCurrentProjectName().length() != 0) {
+            String prefix = null;
+            if (context.isIgnoringProjectTag()
+                && (prefix = getTargetPrefix(context)) != null) {
                 // In an impored file (and not completely
-                // ignoring the project tag)
-                String newName = context.getCurrentProjectName() + "." + name;
+                // ignoring the project tag or having a preconfigured prefix)
+                String newName = prefix + "." + name;
                 Target newTarget = usedTarget ? new Target(target) : target;
                 newTarget.setName(newName);
                 context.getCurrentTargets().put(newName, newTarget);
@@ -881,6 +877,21 @@
             }
         }
 
+        private String getTargetPrefix(AntXMLContext context) {
+            String configuredValue = getCurrentTargetPrefix();
+            if (configuredValue != null && configuredValue.length() == 0) {
+                configuredValue = null;
+            }
+            if (configuredValue != null) {
+                return configuredValue;
+            }
+            String projectName = context.getCurrentProjectName();
+            if (projectName != null && projectName.length() == 0) {
+                projectName = null;
+            }
+            return projectName;
+        }
+
         /**
          * Handles the start of an element within a target.
          *

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ImportTask.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ImportTask.java?rev=713005&r1=713004&r2=713005&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ImportTask.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ImportTask.java Tue Nov 11 01:36:51 2008
@@ -34,9 +34,9 @@
  * into the same Project.
  * </p>
  * <p>
- * <b>Important</b>: we have not finalized how relative file references
- * will be resolved in deep/complex build hierarchies - such as what happens
- * when an imported file imports another file. Use absolute references for
+ * <b>Important</b>: Trying to understand how relative file references
+ * resolved in deep/complex build hierarchies - such as what happens
+ * when an imported file imports another file can be difficult. Use absolute references for
  * enhanced build file stability, especially in the imported files.
  * </p>
  * <p>Examples:</p>
@@ -55,6 +55,7 @@
 public class ImportTask extends Task {
     private String file;
     private boolean optional;
+    private String targetPrefix;
     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
 
     /**
@@ -79,6 +80,15 @@
     }
 
     /**
+     * The prefix to use when prefixing the imported target names.
+     *
+     * @since Ant 1.8.0
+     */
+    public void setAs(String prefix) {
+        targetPrefix = prefix;
+    }
+
+    /**
      *  This relies on the task order model.
      *
      */
@@ -143,10 +153,13 @@
         }
 
         try {
+            ProjectHelper.setCurrentTargetPrefix(targetPrefix);
             helper.parse(getProject(), importedFile);
         } catch (BuildException ex) {
             throw ProjectHelper.addLocationToBuildException(
                 ex, getLocation());
+        } finally {
+            ProjectHelper.setCurrentTargetPrefix(null);
         }
     }
 

Added: ant/core/trunk/src/tests/antunit/taskdefs/import-test.xml
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/import-test.xml?rev=713005&view=auto
==============================================================================
--- ant/core/trunk/src/tests/antunit/taskdefs/import-test.xml (added)
+++ ant/core/trunk/src/tests/antunit/taskdefs/import-test.xml Tue Nov 11 01:36:51 2008
@@ -0,0 +1,31 @@
+<?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 default="antunit" xmlns:au="antlib:org.apache.ant.antunit">
+  <import file="../antunit-base.xml" />
+
+  <import file="importtests/a.xml"/>
+  <import file="importtests/b.xml" as="c"/>
+
+  <target name="testNoExplicitPrefix" depends="a.a">
+    <au:assertEquals expected="bar" actual="${foo}"/>
+  </target>
+
+  <target name="testExplicitPrefix" depends="c.b">
+    <au:assertEquals expected="baz" actual="${foo}"/>
+  </target>
+</project>

Propchange: ant/core/trunk/src/tests/antunit/taskdefs/import-test.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ant/core/trunk/src/tests/antunit/taskdefs/importtests/a.xml
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/importtests/a.xml?rev=713005&view=auto
==============================================================================
--- ant/core/trunk/src/tests/antunit/taskdefs/importtests/a.xml (added)
+++ ant/core/trunk/src/tests/antunit/taskdefs/importtests/a.xml Tue Nov 11 01:36:51 2008
@@ -0,0 +1,23 @@
+<?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 name="a">
+  <target name="a">
+    <property name="foo" value="bar"/>
+  </target>
+</project>
+

Propchange: ant/core/trunk/src/tests/antunit/taskdefs/importtests/a.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ant/core/trunk/src/tests/antunit/taskdefs/importtests/b.xml
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/importtests/b.xml?rev=713005&view=auto
==============================================================================
--- ant/core/trunk/src/tests/antunit/taskdefs/importtests/b.xml (added)
+++ ant/core/trunk/src/tests/antunit/taskdefs/importtests/b.xml Tue Nov 11 01:36:51 2008
@@ -0,0 +1,23 @@
+<?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 name="b">
+  <target name="b">
+    <property name="foo" value="baz"/>
+  </target>
+</project>
+

Propchange: ant/core/trunk/src/tests/antunit/taskdefs/importtests/b.xml
------------------------------------------------------------------------------
    svn:eol-style = native