1 package org.antmod.buildplugin; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.lang.reflect.Method ; 6 import java.util.ArrayList ; 7 import java.util.Enumeration ; 8 import java.util.Hashtable ; 9 10 import org.antmod.util.AntUtil; 11 import org.apache.commons.io.FileUtils; 12 import org.apache.tools.ant.Project; 13 14 19 public final class BuildPlugin { 20 public final static int BUILDLEVEL_MODULE = 1111; 21 public final static int BUILDLEVEL_RELEASE = 2222; 22 23 private File pluginDir; 24 25 private Project moduleAntProject; 26 private ArrayList moduleAntTargets; 27 28 private Project releaseAntProject; 29 private ArrayList releaseAntTargets; 30 31 37 BuildPlugin(Project invokingAntProject, Object otherBuildPlugin) { 38 this.pluginDir = (File )invokeGetter(otherBuildPlugin, "getPluginHome"); 39 this.moduleAntProject = (Project)invokeGetter(otherBuildPlugin, "getModuleAntProject"); 40 this.moduleAntTargets = (ArrayList )invokeGetter(otherBuildPlugin, "getModuleAntTargets"); 41 this.releaseAntProject = (Project)invokeGetter(otherBuildPlugin, "getReleaseAntProject"); 42 this.releaseAntTargets = (ArrayList )invokeGetter(otherBuildPlugin, "getReleaseAntTargets"); 43 51 } 52 53 54 BuildPlugin(Project antProject, File pluginDirectory) { 55 this.pluginDir = pluginDirectory; 57 58 File modulebuildFile = new File (pluginDirectory, "modulebuild.xml"); 61 if (modulebuildFile.exists()) { 62 this.moduleAntProject = AntUtil.getAntProject(antProject, modulebuildFile, true); 63 if (this.moduleAntProject == null) { 64 throw new RuntimeException ("modulebuild.xml for plugin " + getName() + " not found or initialized"); 65 } 66 this.moduleAntProject.setProperty("antmod.plugins." + getName() + ".home", pluginDirectory.getAbsolutePath()); 68 this.moduleAntTargets = AntUtil.getAntTargets(this.moduleAntProject); 69 } 70 71 File releasebuildFile = new File (pluginDirectory, "releasebuild.xml"); 73 if (releasebuildFile.exists()) { 74 this.releaseAntProject = AntUtil.getAntProject(antProject, releasebuildFile, true); 75 if (this.releaseAntProject == null) { 76 throw new RuntimeException ("releasebuild.xml for plugin " + getName() + " not found or initialized"); 77 } 78 this.releaseAntProject.setProperty("antmod.plugins." + getName() + ".home", pluginDirectory.getAbsolutePath()); 80 this.releaseAntTargets = AntUtil.getAntTargets(this.releaseAntProject); 81 } 82 } 84 85 86 private static Object invokeGetter(Object instance, String getMethod) { 87 Method m; 88 try { 89 m = instance.getClass().getMethod(getMethod, null); 90 return m.invoke(instance, null); 91 } 92 catch (Exception e) { 93 e.printStackTrace(); 94 } 95 return null; 96 } 97 98 101 public File getPluginHome() { 102 return pluginDir; 103 } 104 105 108 public String getName() { 109 return pluginDir.getName(); 110 } 111 112 public Project getModuleAntProject() { 113 return moduleAntProject; 114 } 115 public ArrayList getModuleAntTargets() { 116 return moduleAntTargets; 117 } 118 public Project getReleaseAntProject() { 119 return releaseAntProject; 120 } 121 public ArrayList getReleaseAntTargets() { 122 return releaseAntTargets; 123 } 124 125 130 public boolean hasAntTarget(int buildLevel, String antTarget) { 131 if (buildLevel == BUILDLEVEL_MODULE) { 132 return this.moduleAntTargets != null && this.moduleAntTargets.contains(antTarget); 133 } else if (buildLevel == BUILDLEVEL_RELEASE) { 134 return this.releaseAntTargets != null && this.releaseAntTargets.contains(antTarget); 135 } 136 throw new IllegalArgumentException ("Invalid build level - should be either BUILDLEVEL_MODULE or BUILDLEVEL_RELEASE"); 137 } 138 139 146 public void invokeAntTarget(Project parentProject, int buildLevel, String antTarget) { 147 if (hasAntTarget(buildLevel, antTarget)) { 148 if (buildLevel == BUILDLEVEL_MODULE) { 150 159 160 Project newModuleProject = AntUtil.getAntProject(parentProject, new File (this.pluginDir, "modulebuild.xml"), true); 162 AntUtil.inheritRefs(parentProject, newModuleProject); 163 newModuleProject.executeTarget(antTarget); 164 passBackProperties("antmod." + getName(), newModuleProject, parentProject); 165 } 166 else if (buildLevel == BUILDLEVEL_RELEASE) { 167 176 177 Project newReleaseProject = AntUtil.getAntProject(parentProject, new File (this.pluginDir, "releasebuild.xml"), true); 179 AntUtil.inheritRefs(parentProject, newReleaseProject); 180 newReleaseProject.executeTarget(antTarget); 181 passBackProperties("antmod." + getName(), newReleaseProject, parentProject); 182 } 183 else { 184 throw new IllegalArgumentException ("Invalid build level - should be either BUILDLEVEL_MODULE or BUILDLEVEL_RELEASE"); 185 } 186 } 187 } 188 189 193 public String getInjectReleaseContents() { 194 File injectRelease = new File (this.pluginDir, "injectrelease.xml"); 195 if (injectRelease.exists()) { 196 try { 197 return FileUtils.readFileToString(injectRelease, System.getProperty("file.encoding")); 198 } catch (IOException ioe) { 199 throw new IllegalStateException ("Cannot read contents of file \"" + injectRelease.getPath() + "\": " + ioe); 200 } 201 } 202 return null; 203 } 204 205 209 public String getInjectModuleContents() { 210 File injectModule = new File (this.pluginDir, "injectmodule.xml"); 211 if (injectModule.exists()) { 212 try { 213 return FileUtils.readFileToString(injectModule, System.getProperty("file.encoding")); 214 } catch (IOException ioe) { 215 throw new IllegalStateException ("Cannot read contents of file \"" + injectModule.getPath() + "\": " + ioe); 216 } 217 } 218 return null; 219 } 220 221 222 private void passBackProperties(String propnameStart, Project fromProject, Project toProject) { 223 Hashtable props = fromProject.getProperties(); 224 Enumeration propNames = props.keys(); 225 String propName; 226 while (propNames.hasMoreElements()) { 227 propName = (String )propNames.nextElement(); 228 if (propName.startsWith(propnameStart)) { 229 toProject.setProperty(propName, fromProject.getProperty(propName)); 230 } 231 } 232 } 233 234 } 235
| Popular Tags
|