Hi,

One of the managers here was looking at the way the description attribute is used within ant. He thought that it is used for two purposes:

1 - describe a task so that -projecthelp can provide output for the user of a build file 2 - to provide some kind of 'scope' to the targets ie targets without a description are considered 'private' and really shouldn't be called directly

He asked if I could look into a way of enforcing the second of these in code, so that it is impossible to call a 'private' target directly from the cmd line.

It's a relatively straightfoward change involving only minor additions to the code, and it's bwc (as far as I can tell).

Attached patch file of implementation for your consideration.

Pros:
- bwc (AFAIK) - by default targets are 'public', so old build files which don't have target scope continue to run as normal - could be used by IDE developers to filter out executable targets more effectively - could be used by other tools (AntGraph etc) to provide more semantic information to the user - allows a description to be added to complex internal targets, whilst still keeping them 'private'

Cons:
- change to the core classes

Current patch doesn't alter the -projecthelp to separate public/private targets, it simply enforces the new scope rules for execution.

Notes:
- only tested manually, no unit tests

Thanks
Kev


Index: 
D:/java_projects/ant-core-trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java
===================================================================
--- 
D:/java_projects/ant-core-trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java
    (revision 396371)
+++ 
D:/java_projects/ant-core-trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java
    (working copy)
@@ -800,6 +800,8 @@
                     }
                 } else if (key.equals("description")) {
                     target.setDescription(value);
+                } else if (key.equals("scope")) {
+                    target.setScope(value);
                 } else {
                     throw new SAXParseException("Unexpected attribute \""
                         + key + "\"", context.getLocator());
Index: 
D:/java_projects/ant-core-trunk/src/main/org/apache/tools/ant/Project.java
===================================================================
--- D:/java_projects/ant-core-trunk/src/main/org/apache/tools/ant/Project.java  
(revision 396371)
+++ D:/java_projects/ant-core-trunk/src/main/org/apache/tools/ant/Project.java  
(working copy)
@@ -1228,7 +1228,7 @@
         if (targetName == null) {
             String msg = "No target specified";
             throw new BuildException(msg);
-        }
+        } 
 
         // Sort and run the dependency tree.
         // Sorting checks if all the targets (and dependencies)
@@ -1246,6 +1246,12 @@
         throws BuildException {
         Set succeededTargets = new HashSet();
         BuildException buildException = null; // first build exception
+        // check scope of first target
+        Target t = (Target)sortedTargets.get(0);
+        if (t.getScope().equals(Target.PRIVATE_TARGET)) {
+            log(t, "Cannot execute '" + t.getName() + "' directly - declared 
as private", MSG_ERR);
+            System.exit(0);
+        }
         for (Enumeration iter = sortedTargets.elements();
              iter.hasMoreElements();) {
             Target curtarget = (Target) iter.nextElement();
Index: D:/java_projects/ant-core-trunk/src/main/org/apache/tools/ant/Target.java
===================================================================
--- D:/java_projects/ant-core-trunk/src/main/org/apache/tools/ant/Target.java   
(revision 396371)
+++ D:/java_projects/ant-core-trunk/src/main/org/apache/tools/ant/Target.java   
(working copy)
@@ -33,6 +33,9 @@
  */
 public class Target implements TaskContainer {
 
+    protected static final String PUBLIC_TARGET = "public";
+    protected static final String PRIVATE_TARGET = "private"; 
+    
     /** Name of this target. */
     private String name;
     /** The "if" condition to test on execution. */
@@ -45,7 +48,9 @@
     private List children = new ArrayList();
     /** Since Ant 1.6.2 */
     private Location location = Location.UNKNOWN_LOCATION;
-
+    /** The scope of this target */
+    private String scope = PUBLIC_TARGET;
+    
     /** Project this target belongs to. */
     private Project project;
 
@@ -478,4 +483,20 @@
     public int hashCode() {
         return (name != null ? name.hashCode() : 0);
     }
+
+    /**
+     * Gets the targets scope
+     * @return scope
+     */
+    public String getScope() {
+        return scope;
+    }
+
+    /**
+     * Sets the targets scope
+     * @param s
+     */
+    public void setScope(String s) {
+        this.scope = s;
+    }
 }

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to