1 46 package org.codehaus.groovy.ant; 47 48 import java.io.File ; 49 import java.io.PrintWriter ; 50 import java.io.StringWriter ; 51 import java.nio.charset.Charset ; 52 53 import org.apache.tools.ant.BuildException; 54 import org.apache.tools.ant.DirectoryScanner; 55 import org.apache.tools.ant.Project; 56 import org.apache.tools.ant.taskdefs.MatchingTask; 57 import org.apache.tools.ant.types.Path; 58 import org.apache.tools.ant.types.Reference; 59 import org.apache.tools.ant.util.GlobPatternMapper; 60 import org.apache.tools.ant.util.SourceFileScanner; 61 import org.codehaus.groovy.control.CompilationUnit; 62 import org.codehaus.groovy.control.CompilerConfiguration; 63 import org.codehaus.groovy.tools.ErrorReporter; 64 65 66 85 public class Groovyc extends MatchingTask { 86 87 private CompilerConfiguration configuration = new CompilerConfiguration(); 88 private Path src; 89 private File destDir; 90 private Path compileClasspath; 91 private Path compileSourcepath; 92 private String encoding; 93 94 protected boolean failOnError = true; 95 protected boolean listFiles = false; 96 protected File [] compileList = new File [0]; 97 98 public Groovyc() { 99 } 100 101 106 public Path createSrc() { 107 if (src == null) { 108 src = new Path(getProject()); 109 } 110 return src.createPath(); 111 } 112 113 118 protected Path recreateSrc() { 119 src = null; 120 return createSrc(); 121 } 122 123 127 public void setSrcdir(Path srcDir) { 128 if (src == null) { 129 src = srcDir; 130 } 131 else { 132 src.append(srcDir); 133 } 134 } 135 136 140 public Path getSrcdir() { 141 return src; 142 } 143 144 149 public void setDestdir(File destDir) { 150 this.destDir = destDir; 151 } 152 153 158 public void setVerbose(boolean verbose) { 159 configuration.setVerbose( verbose ); 160 } 161 162 167 public File getDestdir() { 168 return destDir; 169 } 170 171 175 public void setSourcepath(Path sourcepath) { 176 if (compileSourcepath == null) { 177 compileSourcepath = sourcepath; 178 } 179 else { 180 compileSourcepath.append(sourcepath); 181 } 182 } 183 184 188 public Path getSourcepath() { 189 return compileSourcepath; 190 } 191 192 196 public Path createSourcepath() { 197 if (compileSourcepath == null) { 198 compileSourcepath = new Path(getProject()); 199 } 200 return compileSourcepath.createPath(); 201 } 202 203 207 public void setSourcepathRef(Reference r) { 208 createSourcepath().setRefid(r); 209 } 210 211 216 public void setClasspath(Path classpath) { 217 if (compileClasspath == null) { 218 compileClasspath = classpath; 219 } 220 else { 221 compileClasspath.append(classpath); 222 } 223 } 224 225 229 public Path getClasspath() { 230 return compileClasspath; 231 } 232 233 237 public Path createClasspath() { 238 if (compileClasspath == null) { 239 compileClasspath = new Path(getProject()); 240 } 241 return compileClasspath.createPath(); 242 } 243 244 248 public void setClasspathRef(Reference r) { 249 createClasspath().setRefid(r); 250 } 251 252 public String createEncoding() { 253 if (encoding == null) { 254 encoding = System.getProperty("file.encoding"); 255 } 256 return encoding; 257 } 258 259 public void setEncoding(String encoding) { 260 this.encoding = encoding; 261 } 262 263 public String getEncoding() { 264 return encoding; 265 } 266 267 271 public void setListfiles(boolean list) { 272 listFiles = list; 273 } 274 275 279 public boolean getListfiles() { 280 return listFiles; 281 } 282 283 288 public void setFailonerror(boolean fail) { 289 failOnError = fail; 290 } 291 292 296 public void setProceed(boolean proceed) { 297 failOnError = !proceed; 298 } 299 300 304 public boolean getFailonerror() { 305 return failOnError; 306 } 307 308 312 public void execute() throws BuildException { 313 checkParameters(); 314 resetFileLists(); 315 316 String [] list = src.list(); 319 for (int i = 0; i < list.length; i++) { 320 File srcDir = getProject().resolveFile(list[i]); 321 if (!srcDir.exists()) { 322 throw new BuildException("srcdir \"" + srcDir.getPath() + "\" does not exist!", getLocation()); 323 } 324 325 DirectoryScanner ds = this.getDirectoryScanner(srcDir); 326 String [] files = ds.getIncludedFiles(); 327 328 scanDir(srcDir, destDir != null ? destDir : srcDir, files); 329 } 330 331 compile(); 332 } 333 334 337 protected void resetFileLists() { 338 compileList = new File [0]; 339 } 340 341 349 protected void scanDir(File srcDir, File destDir, String [] files) { 350 GlobPatternMapper m = new GlobPatternMapper(); 351 m.setFrom("*.groovy"); 352 m.setTo("*.class"); 353 SourceFileScanner sfs = new SourceFileScanner(this); 354 File [] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m); 355 356 if (newFiles.length > 0) { 357 File [] newCompileList = new File [compileList.length + newFiles.length]; 358 System.arraycopy(compileList, 0, newCompileList, 0, compileList.length); 359 System.arraycopy(newFiles, 0, newCompileList, compileList.length, newFiles.length); 360 compileList = newCompileList; 361 } 362 } 363 364 368 public File [] getFileList() { 369 return compileList; 370 } 371 372 protected void checkParameters() throws BuildException { 373 if (src == null) { 374 throw new BuildException("srcdir attribute must be set!", getLocation()); 375 } 376 if (src.size() == 0) { 377 throw new BuildException("srcdir attribute must be set!", getLocation()); 378 } 379 380 if (destDir != null && !destDir.isDirectory()) { 381 throw new BuildException( 382 "destination directory \"" + destDir + "\" does not exist " + "or is not a directory", 383 getLocation()); 384 } 385 386 if (encoding != null && !Charset.isSupported(encoding)) { 387 throw new BuildException("encoding \"\" not supported"); 388 } 389 } 390 391 protected void compile() { 392 393 if (compileList.length > 0) { 394 log( 395 "Compiling " 396 + compileList.length 397 + " source file" 398 + (compileList.length == 1 ? "" : "s") 399 + (destDir != null ? " to " + destDir : "")); 400 401 if (listFiles) { 402 for (int i = 0; i < compileList.length; i++) { 403 String filename = compileList[i].getAbsolutePath(); 404 log(filename); 405 } 406 } 407 408 try { 409 Path classpath = getClasspath(); 410 if (classpath != null) { 411 configuration.setClasspath(classpath.toString()); 412 } 413 configuration.setTargetDirectory(destDir); 414 415 if (encoding != null) { 416 configuration.setSourceEncoding(encoding); 417 } 418 419 CompilationUnit unit = new CompilationUnit( configuration ); 420 unit.addSources( compileList ); 421 unit.compile( ); 422 } 423 catch (Exception e) { 424 425 StringWriter writer = new StringWriter (); 426 new ErrorReporter( e, false ).write( new PrintWriter (writer) ); 427 String message = writer.toString(); 428 429 if (failOnError) { 430 throw new BuildException(message, e, getLocation()); 431 } 432 else { 433 log(message, Project.MSG_ERR); 434 } 435 436 } 437 } 438 } 439 } 440 | Popular Tags |