1 package org.antmod.buildplugin; 2 3 import org.apache.tools.ant.BuildException; 4 import org.apache.tools.ant.Project; 5 import org.apache.tools.ant.Task; 6 import org.apache.tools.ant.types.EnumeratedAttribute; 7 8 16 public class BuildPluginEventTask extends Task { 17 18 private Level level; 19 private String eventName; 20 21 24 public BuildPluginEventTask() { 25 } 26 27 28 public void setLevel(Level level) { 29 this.level = level; 30 } 31 32 33 public void setEventName(String eventName) { 34 this.eventName = eventName; 35 } 36 37 private String constructTargetName() { 38 String trimmedName = this.eventName.trim(); 39 if (trimmedName.equalsIgnoreCase("-pre") || trimmedName.equalsIgnoreCase("pre")) { 40 return "event-" + getOwningTarget().getName() + "-pre"; 41 } else if (trimmedName.equalsIgnoreCase("-post") || trimmedName.equalsIgnoreCase("post")) { 42 return "event-" + getOwningTarget().getName() + "-post"; 43 } else { 44 return "event-" + trimmedName; 45 } 46 } 47 48 52 public void execute() throws BuildException { 53 if (this.level == null) { 54 throw new BuildException("Level unset: should be set to either 'release' or 'level'."); 55 } 56 if (this.eventName == null) { 57 throw new BuildException("Eventname unset, set it to the event you want to fire to the buildplugins."); 58 } 59 60 String levelValue = this.level.getValue(); 61 if (Level.RELEASE.equals(levelValue)) { 62 antOnPlugins(BuildPlugin.BUILDLEVEL_RELEASE, constructTargetName()); 63 } 64 else if (Level.MODULE.equals(levelValue)) { 65 antOnPlugins(BuildPlugin.BUILDLEVEL_MODULE, constructTargetName()); 66 } 67 else { 68 throw new BuildException("Invalid 'level' attribute value: should be either 'release' or 'module'."); 69 } 70 } 71 72 73 private void antOnPlugins(int buildLevel, String antTarget) { 74 BuildPlugin[] pluginList = BuildPluginFactory.getBuildPlugins(getProject()); 76 for (int i = pluginList.length; i-- > 0;) { 77 BuildPlugin plugin = pluginList[i]; 78 antOnPlugin(plugin, buildLevel, antTarget); 79 } 80 } 81 82 private void antOnPlugin(BuildPlugin plugin, int buildLevel, String antTarget) { 83 if (plugin.hasAntTarget(buildLevel, antTarget)) { 84 plugin.invokeAntTarget(getProject(), buildLevel, antTarget); 85 } 86 } 87 88 89 public static class Level extends EnumeratedAttribute { 90 public static String RELEASE = "release"; 91 public static String MODULE = "module"; 92 93 private static String [] values = 94 new String [] { 95 RELEASE, 96 MODULE}; 97 98 public String [] getValues() { 99 return Level.values; 100 } 101 102 } 103 } 104
| Popular Tags
|