1 37 package net.sourceforge.cruisecontrol.builders; 38 39 import java.util.Iterator ; 40 import java.util.List ; 41 import java.util.Map ; 42 import java.util.StringTokenizer ; 43 44 import net.sourceforge.cruisecontrol.CruiseControlException; 45 import net.sourceforge.cruisecontrol.util.Commandline; 46 47 54 public class AntScript implements Script { 55 private Map buildProperties; 56 57 private boolean isWindows; 58 private String antScript; 59 private List args; 60 private String loggerClassName; 61 private String tempFileName = "log.xml"; 62 private boolean useScript; 63 private boolean useLogger; 64 private boolean useQuiet; 65 private boolean useDebug; 66 private String buildFile = "build.xml"; 67 private List properties; 68 private String target = ""; 69 private String systemClassPath; 70 private int exitCode; 71 72 73 79 public Commandline buildCommandline() throws CruiseControlException { 80 Commandline cmdLine = new Commandline(); 81 82 if (useScript) { 83 cmdLine.setExecutable(antScript); 84 } else { 85 if (isWindows) { 86 cmdLine.setExecutable("java.exe"); 87 } else { 88 cmdLine.setExecutable("java"); 89 } 90 for (Iterator argsIterator = args.iterator(); argsIterator.hasNext(); ) { 91 String arg = ((AntBuilder.JVMArg) argsIterator.next()).getArg(); 92 if (arg != null && arg.length() > 0) { 94 cmdLine.createArgument().setValue(arg); 95 } 96 } 97 98 cmdLine.createArgument().setValue("-classpath"); 99 cmdLine.createArgument().setValue(getAntLauncherJarLocation(systemClassPath, isWindows)); 100 cmdLine.createArgument().setValue("org.apache.tools.ant.launch.Launcher"); 101 cmdLine.createArgument().setValue("-lib"); 102 cmdLine.createArgument().setValue(systemClassPath); 103 } 104 105 if (useLogger) { 106 cmdLine.createArgument().setValue("-logger"); 107 cmdLine.createArgument().setValue(getLoggerClassName()); 108 cmdLine.createArgument().setValue("-logfile"); 109 cmdLine.createArgument().setValue(tempFileName); 110 } else { 111 cmdLine.createArgument().setValue("-listener"); 112 cmdLine.createArgument().setValue(getLoggerClassName()); 113 cmdLine.createArgument().setValue("-DXmlLogger.file=" + tempFileName); 114 } 115 116 if (useDebug) { 119 cmdLine.createArgument().setValue("-debug"); 120 } else if (useQuiet) { 121 cmdLine.createArgument().setValue("-quiet"); 122 } 123 124 for (Iterator propertiesIter = buildProperties.entrySet().iterator(); propertiesIter.hasNext(); ) { 125 Map.Entry property = (Map.Entry ) propertiesIter.next(); 126 String value = (String ) property.getValue(); 127 if (!"".equals(value)) { 128 cmdLine.createArgument().setValue("-D" + property.getKey() + "=" + value); 129 } 130 } 131 132 for (Iterator antPropertiesIterator = properties.iterator(); antPropertiesIterator.hasNext(); ) { 133 Property property = (Property) antPropertiesIterator.next(); 134 cmdLine.createArgument().setValue("-D" + property.getName() + "=" + property.getValue()); 135 } 136 137 cmdLine.createArgument().setValue("-buildfile"); 138 cmdLine.createArgument().setValue(buildFile); 139 140 StringTokenizer targets = new StringTokenizer (target); 141 while (targets.hasMoreTokens()) { 142 cmdLine.createArgument().setValue(targets.nextToken()); 143 } 144 return cmdLine; 145 } 146 147 148 151 String getAntLauncherJarLocation(String path, boolean isWindows) throws CruiseControlException { 152 String separator = isWindows ? ";" : ":"; 153 StringTokenizer pathTokenizer = new StringTokenizer (path, separator); 154 while (pathTokenizer.hasMoreTokens()) { 155 String pathElement = pathTokenizer.nextToken(); 156 if (pathElement.indexOf("ant-launcher") != -1 && pathElement.endsWith(".jar")) { 157 return pathElement; 158 } 159 } 160 throw new CruiseControlException("Couldn't find path to ant-launcher jar in this classpath: '" + path + "'"); 161 } 162 163 166 public void setBuildProperties(Map buildProperties) { 167 this.buildProperties = buildProperties; 168 } 169 170 173 public String getLoggerClassName() { 174 return loggerClassName; 175 } 176 179 public void setLoggerClassName(String loggerClassName) { 180 this.loggerClassName = loggerClassName; 181 } 182 185 public void setAntScript(String antScript) { 186 this.antScript = antScript; 187 } 188 191 public void setArgs(List args) { 192 this.args = args; 193 } 194 197 public void setWindows(boolean isWindows) { 198 this.isWindows = isWindows; 199 } 200 203 public void setBuildFile(String buildFile) { 204 this.buildFile = buildFile; 205 } 206 209 public void setTempFileName(String tempFileName) { 210 this.tempFileName = tempFileName; 211 } 212 215 public void setUseDebug(boolean useDebug) { 216 this.useDebug = useDebug; 217 } 218 221 public void setUseLogger(boolean useLogger) { 222 this.useLogger = useLogger; 223 } 224 227 public void setUseQuiet(boolean useQuiet) { 228 this.useQuiet = useQuiet; 229 } 230 233 public void setUseScript(boolean useScript) { 234 this.useScript = useScript; 235 } 236 239 public void setSystemClassPath(String systemClassPath) { 240 this.systemClassPath = systemClassPath; 241 } 242 245 public void setProperties(List properties) { 246 this.properties = properties; 247 } 248 251 public void setTarget(String target) { 252 this.target = target; 253 } 254 257 public int getExitCode() { 258 return exitCode; 259 } 260 263 public void setExitCode(int exitCode) { 264 this.exitCode = exitCode; 265 } 266 } 267 | Popular Tags |