| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package net.sf.antcontrib.walls; |
| 17 |
import java.io.File; |
| 18 |
import java.io.IOException; |
| 19 |
import java.io.PrintWriter; |
| 20 |
import java.io.StringWriter; |
| 21 |
import java.util.ArrayList; |
| 22 |
import java.util.Arrays; |
| 23 |
import java.util.HashMap; |
| 24 |
import java.util.Iterator; |
| 25 |
import java.util.List; |
| 26 |
import java.util.Map; |
| 27 |
|
| 28 |
import org.apache.tools.ant.BuildException; |
| 29 |
import org.apache.tools.ant.Project; |
| 30 |
import org.apache.tools.ant.Task; |
| 31 |
import org.apache.tools.ant.taskdefs.Copy; |
| 32 |
import org.apache.tools.ant.taskdefs.Javac; |
| 33 |
import org.apache.tools.ant.types.FileSet; |
| 34 |
import org.apache.tools.ant.types.Path; |
| 35 |
import org.apache.tools.ant.util.JAXPUtils; |
| 36 |
import org.xml.sax.HandlerBase; |
| 37 |
import org.xml.sax.Parser; |
| 38 |
import org.xml.sax.SAXException; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class CompileWithWalls extends Task { |
| 51 |
private boolean setWallsTwice = false; |
| 52 |
private boolean setJavacTwice = false; |
| 53 |
private Walls walls; |
| 54 |
private Javac javac; |
| 55 |
private File wallsFile; |
| 56 |
private File tempBuildDir; |
| 57 |
|
| 58 |
private Map packagesNeedingCompiling = new HashMap(); |
| 59 |
|
| 60 |
private SAXException cachedSAXException = null; |
| 61 |
private IOException cachedIOException = null; |
| 62 |
|
| 63 |
public void setIntermediaryBuildDir(File f) { |
| 64 |
tempBuildDir = f; |
| 65 |
} |
| 66 |
|
| 67 |
public File getIntermediaryBuildDir() { |
| 68 |
return tempBuildDir; |
| 69 |
} |
| 70 |
|
| 71 |
public void setWalls(File f) { |
| 72 |
this.wallsFile = f; |
| 73 |
|
| 74 |
Parser parser = JAXPUtils.getParser(); |
| 75 |
HandlerBase hb = new WallsFileHandler(this, wallsFile); |
| 76 |
parser.setDocumentHandler(hb); |
| 77 |
parser.setEntityResolver(hb); |
| 78 |
parser.setErrorHandler(hb); |
| 79 |
parser.setDTDHandler(hb); |
| 80 |
try { |
| 81 |
log("about to start parsing walls file", Project.MSG_INFO); |
| 82 |
parser.parse(wallsFile.toURL().toExternalForm()); |
| 83 |
} catch (SAXException e) { |
| 84 |
cachedSAXException = e; |
| 85 |
throw new ParsingWallsException("Problem parsing walls file attached:", e); |
| 86 |
} catch (IOException e) { |
| 87 |
cachedIOException = e; |
| 88 |
throw new ParsingWallsException("IOException on walls file attached:", e); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
public File getWalls() { |
| 93 |
return wallsFile; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
public Walls createWalls() { |
| 98 |
if (walls != null) |
| 99 |
setWallsTwice = true; |
| 100 |
walls = new Walls(); |
| 101 |
return walls; |
| 102 |
} |
| 103 |
public Javac createJavac() { |
| 104 |
if (javac != null) |
| 105 |
setJavacTwice = true; |
| 106 |
javac = new Javac(); |
| 107 |
return javac; |
| 108 |
} |
| 109 |
public void execute() throws BuildException { |
| 110 |
if(cachedIOException != null) |
| 111 |
throw new BuildException(cachedIOException, getLocation()); |
| 112 |
else if(cachedSAXException != null) |
| 113 |
throw new BuildException(cachedSAXException, getLocation()); |
| 114 |
else if(tempBuildDir == null) |
| 115 |
throw new BuildException( |
| 116 |
"intermediaryBuildDir attribute must be specified on the compilewithwalls element" |
| 117 |
, getLocation()); |
| 118 |
else if (javac == null) |
| 119 |
throw new BuildException( |
| 120 |
"There must be a nested javac element", |
| 121 |
getLocation()); |
| 122 |
else if (walls == null) |
| 123 |
throw new BuildException( |
| 124 |
"There must be a nested walls element", |
| 125 |
getLocation()); |
| 126 |
else if (setWallsTwice) |
| 127 |
throw new BuildException( |
| 128 |
"compilewithwalls task only supports one nested walls element or one walls attribute", |
| 129 |
getLocation()); |
| 130 |
else if (setJavacTwice) |
| 131 |
throw new BuildException( |
| 132 |
"compilewithwalls task only supports one nested javac element", |
| 133 |
getLocation()); |
| 134 |
|
| 135 |
getProject().addTaskDefinition("SilentMove", SilentMove.class); |
| 136 |
getProject().addTaskDefinition("SilentCopy", SilentCopy.class); |
| 137 |
|
| 138 |
File destDir = javac.getDestdir(); |
| 139 |
Path src = javac.getSrcdir(); |
| 140 |
|
| 141 |
if(src == null) |
| 142 |
throw new BuildException("Javac inside compilewithwalls must have a srcdir specified"); |
| 143 |
|
| 144 |
String[] list = src.list(); |
| 145 |
File[] tempSrcDirs1 = new File[list.length]; |
| 146 |
for(int i = 0; i < list.length; i++) { |
| 147 |
tempSrcDirs1[i] = getProject().resolveFile(list[i]); |
| 148 |
} |
| 149 |
|
| 150 |
String[] classpaths = new String[0]; |
| 151 |
if(javac.getClasspath() != null) |
| 152 |
classpaths = javac.getClasspath().list(); |
| 153 |
|
| 154 |
File temp = null; |
| 155 |
for(int i = 0; i < classpaths.length; i++) { |
| 156 |
temp = new File(classpaths[i]); |
| 157 |
if(temp.isDirectory()) { |
| 158 |
|
| 159 |
for(int n = 0; n < tempSrcDirs1.length; n++) { |
| 160 |
if(tempSrcDirs1[n].compareTo(temp) == 0) |
| 161 |
throw new BuildException("The classpath cannot contain any of the\n" |
| 162 |
+"src directories, but it does.\n" |
| 163 |
+"srcdir="+tempSrcDirs1[n]); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
List srcDirs2 = new ArrayList(); |
| 170 |
for(int i = 0; i < tempSrcDirs1.length; i++) { |
| 171 |
if(tempSrcDirs1[i].exists()) |
| 172 |
srcDirs2.add(tempSrcDirs1[i]); |
| 173 |
} |
| 174 |
|
| 175 |
if (destDir == null) |
| 176 |
throw new BuildException( |
| 177 |
"destdir was not specified in nested javac task", |
| 178 |
getLocation()); |
| 179 |
|
| 180 |
|
| 181 |
if(file1IsChildOfFile2(tempBuildDir, destDir)) |
| 182 |
throw new BuildException("intermediaryBuildDir attribute cannot be specified\n" |
| 183 |
+"to be the same as destdir or inside desdir of the javac task.\n" |
| 184 |
+"This is an intermediary build directory only used by the\n" |
| 185 |
+"compilewithwalls task, not the class file output directory.\n" |
| 186 |
+"The class file output directory is specified in javac's destdir attribute", getLocation()); |
| 187 |
|
| 188 |
|
| 189 |
if(!tempBuildDir.exists()) { |
| 190 |
tempBuildDir.mkdirs(); |
| 191 |
log("created direction="+tempBuildDir, Project.MSG_VERBOSE); |
| 192 |
} |
| 193 |
|
| 194 |
Iterator iter = walls.getPackagesToCompile(); |
| 195 |
while (iter.hasNext()) { |
| 196 |
Package toCompile = (Package)iter.next(); |
| 197 |
|
| 198 |
File buildSpace = toCompile.getBuildSpace(tempBuildDir); |
| 199 |
if(!buildSpace.exists()) { |
| 200 |
buildSpace.mkdir(); |
| 201 |
log("created directory="+buildSpace, Project.MSG_VERBOSE); |
| 202 |
} |
| 203 |
|
| 204 |
FileSet javaIncludes2 = |
| 205 |
toCompile.getJavaCopyFileSet(getProject(), getLocation()); |
| 206 |
|
| 207 |
for(int i = 0; i < srcDirs2.size(); i++) { |
| 208 |
File srcDir = (File)srcDirs2.get(i); |
| 209 |
javaIncludes2.setDir(srcDir); |
| 210 |
log(toCompile.getPackage()+": sourceDir["+i+"]="+srcDir+" destDir="+buildSpace, Project.MSG_VERBOSE); |
| 211 |
copyFiles(srcDir, buildSpace, javaIncludes2); |
| 212 |
} |
| 213 |
|
| 214 |
Path srcDir2 = toCompile.getSrcPath(tempBuildDir, getProject()); |
| 215 |
Path classPath = toCompile.getClasspath(tempBuildDir, getProject()); |
| 216 |
if(javac.getClasspath() != null) |
| 217 |
classPath.addExisting(javac.getClasspath()); |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
Javac buildSpaceJavac = new Javac(); |
| 225 |
buildSpaceJavac.setProject(getProject()); |
| 226 |
buildSpaceJavac.setOwningTarget(getOwningTarget()); |
| 227 |
buildSpaceJavac.setTaskName(getTaskName()); |
| 228 |
log(toCompile.getPackage()+": Compiling"); |
| 229 |
log(toCompile.getPackage()+": sourceDir="+srcDir2, Project.MSG_VERBOSE); |
| 230 |
log(toCompile.getPackage()+": classPath="+classPath, Project.MSG_VERBOSE); |
| 231 |
log(toCompile.getPackage()+": destDir="+buildSpace, Project.MSG_VERBOSE); |
| 232 |
buildSpaceJavac.setSrcdir(srcDir2); |
| 233 |
buildSpaceJavac.setDestdir(buildSpace); |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
buildSpaceJavac.setClasspath(classPath); |
| 239 |
|
| 240 |
buildSpaceJavac.setBootclasspath(javac.getBootclasspath()); |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
buildSpaceJavac.setExtdirs(javac.getExtdirs()); |
| 245 |
buildSpaceJavac.setEncoding(javac.getEncoding()); |
| 246 |
buildSpaceJavac.setNowarn(javac.getNowarn()); |
| 247 |
buildSpaceJavac.setDebug(javac.getDebug()); |
| 248 |
buildSpaceJavac.setDebugLevel(javac.getDebugLevel()); |
| 249 |
buildSpaceJavac.setOptimize(javac.getOptimize()); |
| 250 |
buildSpaceJavac.setDeprecation(javac.getDeprecation()); |
| 251 |
buildSpaceJavac.setTarget(javac.getTarget()); |
| 252 |
buildSpaceJavac.setVerbose(javac.getVerbose()); |
| 253 |
buildSpaceJavac.setDepend(javac.getDepend()); |
| 254 |
buildSpaceJavac.setIncludeantruntime(javac.getIncludeantruntime()); |
| 255 |
buildSpaceJavac.setIncludejavaruntime(javac.getIncludejavaruntime()); |
| 256 |
buildSpaceJavac.setFork(javac.isForkedJavac()); |
| 257 |
buildSpaceJavac.setExecutable(javac.getJavacExecutable()); |
| 258 |
buildSpaceJavac.setMemoryInitialSize(javac.getMemoryInitialSize()); |
| 259 |
buildSpaceJavac.setMemoryMaximumSize(javac.getMemoryMaximumSize()); |
| 260 |
buildSpaceJavac.setFailonerror(javac.getFailonerror()); |
| 261 |
buildSpaceJavac.setSource(javac.getSource()); |
| 262 |
buildSpaceJavac.setCompiler(javac.getCompiler()); |
| 263 |
|
| 264 |
Javac.ImplementationSpecificArgument arg; |
| 265 |
String[] args = javac.getCurrentCompilerArgs(); |
| 266 |
if(args != null) { |
| 267 |
for(int i = 0; i < args.length;i++) { |
| 268 |
arg = buildSpaceJavac.createCompilerArg(); |
| 269 |
arg.setValue(args[i]); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
buildSpaceJavac.setProject(getProject()); |
| 274 |
buildSpaceJavac.perform(); |
| 275 |
|
| 276 |
|
| 277 |
copyFiles(buildSpace, destDir, toCompile.getClassCopyFileSet(getProject(), getLocation())); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
private boolean file1IsChildOfFile2(File tempBuildDir, File destDir) { |
| 286 |
File parent = tempBuildDir; |
| 287 |
for(int i = 0; i < 1000; i++) { |
| 288 |
if(parent.compareTo(destDir) == 0) |
| 289 |
return true; |
| 290 |
parent = parent.getParentFile(); |
| 291 |
if(parent == null) |
| 292 |
return false; |
| 293 |
} |
| 294 |
|
| 295 |
throw new RuntimeException("You either have more than 1000 directories in" |
| 296 |
+"\nyour heirarchy or this is a bug, please report. parent="+parent+" destDir="+destDir); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
private void copyFiles( |
| 313 |
File srcDir, |
| 314 |
File destDir, |
| 315 |
FileSet fileset) { |
| 316 |
|
| 317 |
fileset.setDir(srcDir); |
| 318 |
if (!srcDir.exists()) |
| 319 |
throw new BuildException( |
| 320 |
"Directory=" + srcDir + " does not exist", |
| 321 |
getLocation()); |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
Copy move = (Copy)getProject().createTask("SilentCopy"); |
| 330 |
move.setProject(getProject()); |
| 331 |
move.setOwningTarget(getOwningTarget()); |
| 332 |
move.setTaskName(getTaskName()); |
| 333 |
move.setLocation(getLocation()); |
| 334 |
move.setTodir(destDir); |
| 335 |
|
| 336 |
move.addFileset(fileset); |
| 337 |
move.perform(); |
| 338 |
} |
| 339 |
|
| 340 |
public void log(String msg, int level) { |
| 341 |
super.log(msg, level); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
private class ParsingWallsException extends RuntimeException { |
| 346 |
|
| 347 |
private String message; |
| 348 |
|
| 349 |
public ParsingWallsException(String message, Throwable cause) { |
| 350 |
super(message); |
| 351 |
|
| 352 |
this.message = message+"\n"; |
| 353 |
|
| 354 |
StringWriter sw = new StringWriter(); |
| 355 |
PrintWriter pw = new PrintWriter(sw); |
| 356 |
cause.printStackTrace(pw); |
| 357 |
|
| 358 |
this.message += sw; |
| 359 |
} |
| 360 |
|
| 361 |
public String getMessage() { |
| 362 |
return message; |
| 363 |
} |
| 364 |
|
| 365 |
public String toString() { |
| 366 |
return getMessage(); |
| 367 |
} |
| 368 |
} |
| 369 |
} |