KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > CleanAll


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.nbbuild;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.Project;
31 import org.apache.tools.ant.Target;
32 import org.apache.tools.ant.Task;
33 import org.apache.tools.ant.taskdefs.Ant;
34
35 /** Runs a clean task (for example) in all submodules.
36  *
37  * @author Jesse Glick
38  */

39 public class CleanAll extends Task {
40
41     private List JavaDoc<String JavaDoc> modules = new ArrayList JavaDoc<String JavaDoc>();
42     private List JavaDoc<String JavaDoc> failedmodules = new ArrayList JavaDoc<String JavaDoc>();
43     private String JavaDoc targetname = "clean";
44     private File JavaDoc topdir = null;
45     private File JavaDoc [] topdirs = null;
46     private boolean resolvedependencies = false; // resolve compile-time dependencies for clean
47
private String JavaDoc deptargetprefix = ""; // target prefix for resolving dependencies
48
private Hashtable JavaDoc targets;
49     private boolean failonerror = true; // fail if particular module build failed?
50

51     /** Comma-separated list of modules to include. */
52     public void setModules (String JavaDoc s) {
53         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc (s, ", ");
54         modules = new ArrayList JavaDoc<String JavaDoc>();
55         while (tok.hasMoreTokens ())
56             modules.add(tok.nextToken());
57     }
58     
59     /** Name of the target to run in each module's build script. */
60     public void setTargetname (String JavaDoc s) {
61         targetname = s;
62     }
63     
64     /** Prefix of compile targets in current build script for
65       * each module.
66       */

67     public void setDepTargetPrefix (String JavaDoc s) {
68         deptargetprefix = s;
69     }
70     
71     /** Enable/Disable resolving compile-time dependencies. */
72     public void setResolveDependencies (boolean b) {
73         resolvedependencies = b;
74     }
75     
76     /** Enable/Disable BUILD FAILED, when particular module's
77       * build failed.
78       */

79     public void setFailOnError (boolean b) {
80         failonerror = b;
81     }
82     
83     /** The top directory containing these modules as subdirectories. */
84     public void setTopdir (File JavaDoc f) {
85         topdir = f;
86     }
87     
88     public void setTopdirs (String JavaDoc str) {
89         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(str, ",");
90         int count = st.countTokens();
91         topdirs = new File JavaDoc [count];
92         for (int i = 0; i < count; i++) {
93             topdirs[i] = new File JavaDoc (st.nextToken().trim());
94         }
95     }
96  
97     /** Resolve compile-time dependencies and use them for cleaning */
98     private void resolveDependencies () throws BuildException {
99         Target dummy = new Target ();
100         String JavaDoc dummyName = "nbmerge-" + getOwningTarget().getName();
101         targets = getProject().getTargets();
102         while (targets.contains (dummyName))
103             dummyName += "-x";
104         dummy.setName (dummyName);
105         for (String JavaDoc module : modules) {
106             dummy.addDependency (deptargetprefix + module);
107         }
108         getProject().addTarget(dummy);
109         @SuppressWarnings JavaDoc("unchecked")
110         Vector JavaDoc<Target> fullList = getProject().topoSort(dummyName, targets);
111         // Now remove earlier ones: already done.
112
@SuppressWarnings JavaDoc("unchecked")
113         Vector JavaDoc<Target> doneList = getProject().topoSort(getOwningTarget().getName(), targets);
114         List JavaDoc<Target> todo = new ArrayList JavaDoc<Target>(fullList.subList(0, fullList.indexOf(dummy)));
115         todo.removeAll(doneList.subList(0, doneList.indexOf(getOwningTarget())));
116
117         for (Target t : todo) {
118             String JavaDoc _targetname = t.getName();
119             if (_targetname.startsWith(deptargetprefix)) {
120                 String JavaDoc module = _targetname.substring(deptargetprefix.length());
121                 if (modules.indexOf(module) < 0) {
122                     modules.add(module);
123                     log("Adding dependency module \"" + module + "\" to the list of modules for cleaning", Project.MSG_VERBOSE);
124                 }
125             }
126         }
127     }
128     
129     public void execute () throws BuildException {
130         
131         if (topdirs == null && topdir != null) {
132             topdirs = new File JavaDoc[1];
133             topdirs[0] = topdir;
134         }
135         
136         if (topdir == null && topdirs == null) {
137             throw new BuildException("You must set at least one topdir attribute", getLocation());
138         }
139         
140         if (resolvedependencies) resolveDependencies();
141             
142         for (int j = 0; j < topdirs.length; j++) {
143             topdir = topdirs[j];
144             for (String JavaDoc module : modules) {
145                 Ant ant = (Ant) getProject().createTask("ant");
146                 ant.init ();
147                 ant.setLocation(getLocation());
148                 File JavaDoc fl = new File JavaDoc(topdir.getAbsolutePath () +
149                     File.separatorChar + module + File.separatorChar + "build.xml");
150                 if (! fl.exists()) {
151                     continue;
152                 }
153                 ant.setDir(new File JavaDoc(topdir, module));
154                 ant.setTarget (targetname);
155                 try {
156                     log("Process '"+ module + "' location with '" + targetname + "' target", Project.MSG_INFO);
157                     ant.execute ();
158                 } catch (BuildException be) {
159                     if (failonerror) {
160                         throw new BuildException(be.getMessage(), be, getLocation());
161                     } else {
162                         log("Target \"" + targetname + "\" failed in module \"" + module + "\"", Project.MSG_WARN);
163                         log(fl.getAbsolutePath());
164                         log(be.getMessage());
165                         String JavaDoc fname = fl.getAbsolutePath();
166                         failedmodules.add(fname);
167                     }
168                 }
169             }
170         }
171         if (failedmodules.size() > 0) {
172             log("<cleanall> SOME MODULES FAILED TO BUILD, BUT THEIR BuildException WAS CAUGHT", Project.MSG_WARN);
173             log("<cleanall> cleanfailedmodules=\"" + failedmodules.toString() + "\"", Project.MSG_WARN);
174         }
175     }
176 }
177
Popular Tags