1 28 29 30 package org.objectweb.ecm.taskdefs; 31 32 103 public class BuildJarTask 104 extends org.apache.tools.ant.Task 105 { 106 private java.io.File _srcdir; 108 private java.io.File _classdir; 109 private java.io.File _compileto; 110 private String _packagedir; 111 private org.apache.tools.ant.types.Reference _classpath; 112 private org.apache.tools.ant.types.Reference _bootclasspath; 113 private boolean _bcpcreated; 114 private org.apache.tools.ant.taskdefs.Javac _javac; 115 116 private String _archive; 118 private java.io.File _buildto; 119 private org.apache.tools.ant.taskdefs.Jar _jar; 120 private java.util.ArrayList _library; 121 122 static private int _UNKNOWN_MODE = 0; 124 static private int _COMPILE_ONLY_MODE = 1<<1; 125 static private int _BUILD_ONLY_MODE = 1<<2; 126 static private int _TWIN_MODE = _COMPILE_ONLY_MODE | _BUILD_ONLY_MODE; 127 private int _mode; 128 private boolean _verbose; 129 130 131 public 133 BuildJarTask() 134 { 135 _srcdir = null; 137 _classdir = null; 138 _compileto = null; 139 _packagedir = null; 140 _classpath = null; 141 _bootclasspath = null; 142 _bcpcreated = false; 143 _javac = null; 144 145 _archive = null; 147 _buildto = null; 148 _jar = null; 149 _library = new java.util.ArrayList (); 150 151 _verbose = false; 153 _mode = _UNKNOWN_MODE; 154 } 155 156 160 161 private org.apache.tools.ant.taskdefs.Javac 162 javac() 163 { 164 if (_javac==null) { 165 org.apache.tools.ant.Task task = super.project.createTask("javac"); 167 _javac = (org.apache.tools.ant.taskdefs.Javac)task; 168 } 169 170 return _javac; 171 } 172 173 private org.apache.tools.ant.taskdefs.Jar 174 jar() 175 { 176 if (_jar==null) { 177 org.apache.tools.ant.Task task = super.project.createTask("jar"); 179 _jar = (org.apache.tools.ant.taskdefs.Jar)task; 180 } 181 182 return _jar; 183 } 184 185 private void 186 addLibrary() 187 { 188 NestedElementWithFile[] libs = (NestedElementWithFile[])_library.toArray(new NestedElementWithFile[0]); 190 org.apache.tools.ant.types.FileSet set = null; 191 for (int i=0;i<libs.length;i++) { 192 set = new org.apache.tools.ant.types.FileSet(); 193 set.setFile(libs[i].getFile()); 194 jar().addZipGroupFileset(set); 195 } 196 } 197 198 199 private void 201 validateAttributes() 202 throws org.apache.tools.ant.BuildException 203 { 204 String msg = ""; 205 if ((_srcdir!=null) && (_archive==null)) { 207 _mode = _COMPILE_ONLY_MODE; 208 log("[validateAttributes] compile-only mode detected"); 209 } 210 211 if ((_archive!=null) && (_srcdir==null)) { 212 _mode = _BUILD_ONLY_MODE; 213 log("[validateAttributes] build-only mode detected"); 214 } 215 216 if ((_archive!=null) && (_srcdir!=null)) { 217 _mode = _TWIN_MODE; 218 log("[validateAttributes] twin mode detected"); 219 } 220 221 if ((_srcdir==null) && (_archive==null)) { 222 msg = "either srcdir or archivename attribute must be set"; 223 throw new org.apache.tools.ant.BuildException(msg); 224 } 225 226 if (((_mode&_COMPILE_ONLY_MODE)!=0) && (_compileto==null)) { 227 msg = "compileto attribute is missing"; 228 throw new org.apache.tools.ant.BuildException(msg); 229 } 230 231 if (((_mode&_COMPILE_ONLY_MODE)!=0) && 232 ((!_srcdir.exists()) || (!_srcdir.isDirectory()))) { 233 msg = "source directory does not exist or is not a directory"; 234 throw new org.apache.tools.ant.BuildException(msg); 235 } 236 237 if (((_mode&_COMPILE_ONLY_MODE)!=0) && 238 ((!_compileto.exists()) || (!_compileto.isDirectory()))) { 239 msg = "class directory does not exist or is not a directory"; 240 throw new org.apache.tools.ant.BuildException(msg); 241 } 242 243 if (((_mode&_BUILD_ONLY_MODE)!=0) && (_buildto==null)) { 244 msg = "buildto attribute is missing"; 245 throw new org.apache.tools.ant.BuildException(msg); 246 } 247 248 if (((_mode&_BUILD_ONLY_MODE)!=0) && 249 ((!_buildto.exists()) || (!_buildto.isDirectory()))) { 250 msg = "archive directory does not exist or is not a directory"; 251 throw new org.apache.tools.ant.BuildException(msg); 252 } 253 254 if (((_mode&_BUILD_ONLY_MODE)==0) && (_bootclasspath==null) && (!_bcpcreated)){ 255 msg = "bootclasspath attribute or nested element required"; 256 throw new org.apache.tools.ant.BuildException(msg); 257 } 258 259 if (_packagedir!=null) { 260 String classdirname = _compileto.getPath()+java.io.File.separator+_packagedir; 262 _classdir = new java.io.File (classdirname); 263 } 264 } 265 266 private boolean 267 classUpToDate() 268 { 269 if (_packagedir!=null) { 270 if ((!_classdir.exists()) || (!_classdir.isDirectory())) { 272 return false; 273 } 274 275 java.io.File [] sources = FileHelper.listFiles(_srcdir); 277 java.io.File [] classes = FileHelper.listFiles(_classdir); 278 279 286 287 for (int i=0;i<sources.length;i++) { 288 290 String sname = sources[i].getName(); 292 int idx = sname.lastIndexOf('.'); 293 String classname = sname.substring(0, idx)+".class"; 294 java.io.File classfile = new java.io.File (_classdir, classname); 295 296 java.io.File foundclass = null; 298 for (int j=0;j<classes.length;j++) { 299 if (classes[j].equals(classfile)) { 301 foundclass = classes[j]; 302 break; 303 } 304 } 305 306 if (foundclass==null) { 308 return false; 309 } 310 311 if (sources[i].lastModified()>foundclass.lastModified()) { 313 return false; 314 } 315 } 316 317 log("[classUpToDate] source classes are uptodate: "+_srcdir.getPath()); 319 return true; 320 } 321 322 return false; 323 } 324 325 private boolean 326 jarUpToDate(java.io.File tmpdir) 327 { 328 if (tmpdir.listFiles().length==0) { 330 return true; 331 } 332 333 return false; 334 } 335 336 340 final public void 341 setSrcdir(java.io.File dir) 342 { 343 _srcdir = dir; 344 } 345 346 final public void 347 setCompileto(java.io.File dir) 348 { 349 _compileto = dir; 350 } 351 352 final public void 353 setPackagedir(String dir) 354 { 355 _packagedir = dir.replace('/', java.io.File.separatorChar); 356 } 357 358 final public void 359 setClasspathRef(org.apache.tools.ant.types.Reference cp) 360 { 361 _classpath = cp; 362 } 363 364 final public void 365 setClasspathref(org.apache.tools.ant.types.Reference cp) 366 { 367 _classpath = cp; 368 } 369 370 final public void 371 setBootclasspathRef(org.apache.tools.ant.types.Reference cp) 372 { 373 _bootclasspath = cp; 374 } 375 376 final public void 377 setBootclasspathref(org.apache.tools.ant.types.Reference cp) 378 { 379 _bootclasspath = cp; 380 } 381 382 final public org.apache.tools.ant.types.Path 383 createClasspath() 384 { 385 return javac().createClasspath(); 386 } 387 388 final public org.apache.tools.ant.types.Path 389 createBootclasspath() 390 { 391 _bcpcreated = true; 392 return javac().createBootclasspath(); 393 } 394 395 final public void 396 setArchivename(String name) 397 { 398 _archive = name; 399 } 400 401 final public void 402 setBuildto(java.io.File dir) 403 { 404 _buildto = dir; 405 } 406 407 final public void 408 addLibraries(org.apache.tools.ant.types.FileSet set) 409 { 410 jar().addZipGroupFileset(set); 411 } 412 413 final public NestedElementWithFile 414 createLibrary() 415 { 416 NestedElementWithFile lib = new NestedElementWithFile(); 417 _library.add(lib); 418 return lib; 419 } 420 421 final public void 422 addClasses(org.apache.tools.ant.types.FileSet set) 423 { 424 jar().addFileset(set); 425 } 426 427 final public void 428 setVerbose(boolean verbose) 429 { 430 _verbose = verbose; 431 } 432 433 437 final public void 438 execute() 439 throws org.apache.tools.ant.BuildException 440 { 441 validateAttributes(); 443 444 java.io.File tmpdir = null; 445 if (_mode!=_BUILD_ONLY_MODE) { 446 tmpdir = FileHelper.createTempDir(); 448 } 449 450 if ((_mode!=_BUILD_ONLY_MODE) && (!classUpToDate())) { 459 log("[execute] compiling sources: "+_srcdir.getPath()); 462 org.apache.tools.ant.types.Path srcpath = new org.apache.tools.ant.types.Path(super.project); 463 srcpath.createPathElement().setLocation(_srcdir); 464 javac().setSrcdir(srcpath); 465 javac().setDestdir(tmpdir); 466 javac().setIncludes("*.java **/*.java"); 467 javac().setDebug(true); 468 javac().setClasspathRef(_classpath); 469 javac().setBootClasspathRef(_bootclasspath); 470 org.apache.tools.ant.types.Path cp = javac().createClasspath(); 472 cp.setPath(_compileto.getPath()); 473 474 javac().setVerbose(_verbose); 476 477 javac().execute(); 478 } 479 480 if (_mode==_BUILD_ONLY_MODE) { 484 log("[execute] building jar (build-only): "+_buildto.getPath()+"/"+_archive); 486 jar().setDestFile(new java.io.File (_buildto, _archive)); 487 jar().setUpdate(true); 488 org.apache.tools.ant.types.FileSet set = new org.apache.tools.ant.types.FileSet(); 489 set.setDir(_buildto); 490 set.setIncludes("*.class **/*.class"); 491 jar().addFileset(set); 492 addLibrary(); 493 jar().execute(); 494 } 495 else if ((_mode==_TWIN_MODE) && (!jarUpToDate(tmpdir))) { 497 log("[execute] building jar (: "+_buildto.getPath()+"/"+_archive); 499 jar().setDestFile(new java.io.File (_buildto, _archive)); 500 jar().setUpdate(true); 501 org.apache.tools.ant.types.FileSet set = new org.apache.tools.ant.types.FileSet(); 502 set.setDir(tmpdir); 503 set.setIncludes("*.class **/*.class"); 504 jar().addFileset(set); 505 addLibrary(); 506 jar().execute(); 507 } 508 509 if (_mode!=_BUILD_ONLY_MODE) { 510 FileHelper.moveFiles(tmpdir.listFiles(), _compileto); 512 513 FileHelper.deleteDir(tmpdir); 515 } 516 } 517 } 518 | Popular Tags |