KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.SortedMap JavaDoc;
28 import java.util.TreeMap JavaDoc;
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 import org.apache.tools.ant.taskdefs.CallTarget;
35 import org.apache.tools.ant.taskdefs.Echo;
36 import org.apache.tools.ant.taskdefs.Property;
37
38 /**
39  * Insert into the live project some all-* targets.
40  * This means they do not need to manually written into nbbuild/build.xml.
41  * Targets are only added for projectized modules which do not already
42  * have all-* entries in the build script.
43  * Generally, an all-* target for a projectized module in a named cluster depends on:
44  * 1. 'init'
45  * 2. The all-* targets for any module which it lists as <build-prerequisite/>s
46  * in project.xml, but which are not included in the same cluster as this module.
47  * Therefore cluster dependencies still have to be manually set.
48  * An all-* target for a projectized module without a specific cluster depends on:
49  * 1. 'init'
50  * 2. The all-* targets for any module which it lists as <build-prerequisite/>s
51  * in project.xml.
52  * cluster.properties must have already been read for this task to work.
53  * @author Jesse Glick
54  */

55 public final class InsertModuleAllTargets extends Task {
56     
57     public InsertModuleAllTargets() {}
58
59     public void execute() throws BuildException {
60         try {
61             Project project = getProject();
62             @SuppressWarnings JavaDoc("unchecked")
63             Set JavaDoc<String JavaDoc> existingTargets = project.getTargets().keySet();
64             if (existingTargets.contains("all-openide/util")) {
65                 log("Already seem to have inserted targets into this project; will not do it twice", Project.MSG_VERBOSE);
66                 return;
67             }
68             @SuppressWarnings JavaDoc("unchecked")
69             Hashtable JavaDoc<String JavaDoc,String JavaDoc> props = project.getProperties();
70             Map JavaDoc<String JavaDoc,String JavaDoc> clustersOfModules = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
71             for (Map.Entry JavaDoc<String JavaDoc,String JavaDoc> pair : props.entrySet()) {
72                 String JavaDoc cluster = pair.getKey();
73                 if (!cluster.startsWith("nb.cluster.") || cluster.endsWith(".depends") || cluster.endsWith(".dir")) {
74                     continue;
75                 }
76                 for (String JavaDoc module : pair.getValue().split(", *")) {
77                     clustersOfModules.put(module, cluster);
78                 }
79             }
80             ModuleListParser mlp = new ModuleListParser(props, ParseProjectXml.TYPE_NB_ORG, project);
81             SortedMap JavaDoc<String JavaDoc,ModuleListParser.Entry> entries = new TreeMap JavaDoc<String JavaDoc,ModuleListParser.Entry>();
82             for (ModuleListParser.Entry entry : mlp.findAll()) {
83                 String JavaDoc path = entry.getNetbeansOrgPath();
84                 assert path != null : entry;
85                 entries.put(path, entry);
86             }
87             for (ModuleListParser.Entry entry : entries.values()) {
88                 String JavaDoc path = entry.getNetbeansOrgPath();
89                 assert path != null : entry;
90                 String JavaDoc target = "all-" + path;
91                 if (existingTargets.contains(target)) {
92                     log("Not adding target " + target + " because one already exists", Project.MSG_INFO);
93                     continue;
94                 }
95                 String JavaDoc[] prereqsAsCnb = entry.getBuildPrerequisites();
96                 StringBuffer JavaDoc namedDeps = new StringBuffer JavaDoc("init");
97                 String JavaDoc myCluster = clustersOfModules.get(path);
98                 for (String JavaDoc cnb : prereqsAsCnb ) {
99                     ModuleListParser.Entry other = mlp.findByCodeNameBase(cnb);
100                     if (other == null) {
101                         log("Cannot find build prerequisite " + cnb + " of " + entry, Project.MSG_WARN);
102                         continue;
103                     }
104                     String JavaDoc otherPath = other.getNetbeansOrgPath();
105                     assert otherPath != null : other;
106                     String JavaDoc otherCluster = clustersOfModules.get(otherPath);
107                     if (myCluster == null || otherCluster == null || myCluster.equals(otherCluster)) {
108                         namedDeps.append(",all-");
109                         namedDeps.append(otherPath);
110                     }
111                 }
112                 String JavaDoc namedDepsS = namedDeps.toString();
113                 log("Adding target " + target + " with depends=\"" + namedDepsS + "\"", Project.MSG_VERBOSE);
114                 Target t = new Target();
115                 t.setName(target);
116                 t.setLocation(getLocation());
117                 t.setDepends(namedDepsS);
118                 project.addTarget(t);
119                 if (myCluster != null) {
120                     CallTarget call = (CallTarget) project.createTask("antcall");
121                     call.setTarget("build-one-cluster-dependencies");
122                     call.setInheritAll(false);
123                     Property param = call.createParam();
124                     param.setName("one.cluster.dependencies");
125                     param.setValue(props.get(myCluster + ".depends"));
126                     param = call.createParam();
127                     param.setName("one.cluster.name");
128                     param.setValue("this-cluster");
129                     t.addTask(call);
130                 }
131                 Echo echo = (Echo) project.createTask("echo");
132                 echo.setMessage("Building " + path + "...");
133                 t.addTask(echo);
134                 Ant ant = (Ant) project.createTask("ant");
135                 ant.setDir(project.resolveFile("../" + path));
136                 ant.setTarget("netbeans");
137                 t.addTask(ant);
138             }
139         } catch (IOException JavaDoc e) {
140             throw new BuildException(e.toString(), e, getLocation());
141         }
142     }
143     
144 }
145
Popular Tags