1 13 14 package org.eclipse.ant.internal.ui.datatransfer; 15 16 import java.io.ByteArrayInputStream ; 17 import java.io.File ; 18 import java.io.FilenameFilter ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.UnsupportedEncodingException ; 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.LinkedHashMap ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.StringTokenizer ; 31 import java.util.TreeMap ; 32 import java.util.TreeSet ; 33 34 import javax.xml.parsers.DocumentBuilderFactory ; 35 import javax.xml.parsers.ParserConfigurationException ; 36 import javax.xml.transform.TransformerConfigurationException ; 37 import javax.xml.transform.TransformerException ; 38 import javax.xml.transform.TransformerFactoryConfigurationError ; 39 40 import org.eclipse.ant.internal.ui.AntUIPlugin; 41 import org.eclipse.core.resources.IFile; 42 import org.eclipse.core.runtime.CoreException; 43 import org.eclipse.core.runtime.IPath; 44 import org.eclipse.core.runtime.IProgressMonitor; 45 import org.eclipse.core.runtime.Path; 46 import org.eclipse.core.variables.VariablesPlugin; 47 import org.eclipse.debug.core.DebugPlugin; 48 import org.eclipse.debug.core.ILaunchConfiguration; 49 import org.eclipse.debug.core.ILaunchManager; 50 import org.eclipse.jdt.core.IClasspathContainer; 51 import org.eclipse.jdt.core.IClasspathEntry; 52 import org.eclipse.jdt.core.IJavaProject; 53 import org.eclipse.jdt.core.IType; 54 import org.eclipse.jdt.core.JavaCore; 55 import org.eclipse.jdt.core.JavaModelException; 56 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 57 import org.eclipse.jface.dialogs.MessageDialog; 58 import org.eclipse.swt.widgets.Shell; 59 import org.w3c.dom.Comment ; 60 import org.w3c.dom.Document ; 61 import org.w3c.dom.Element ; 62 import org.w3c.dom.Node ; 63 import org.w3c.dom.ProcessingInstruction ; 64 import org.xml.sax.SAXException ; 65 66 69 public class BuildFileCreator 70 { 71 protected static final String IMPORT_BUILDFILE_PROCESSING_TARGET = "eclipse.ant.import"; protected static final String WARNING = " WARNING: Eclipse auto-generated file." + ExportUtil.NEWLINE + " Any modifications will be overwritten."; protected static final String NOTE = " To include a user specific buildfile here, " + "simply create one in the same" + ExportUtil.NEWLINE + " directory with the processing instruction " + "<?" + IMPORT_BUILDFILE_PROCESSING_TARGET + "?>" + ExportUtil.NEWLINE + " as the first entry and export the buildfile again. "; 80 protected static String BUILD_XML = "build.xml"; protected static String JUNIT_OUTPUT_DIR = "junit"; protected static boolean CHECK_SOURCE_CYCLES = true; 83 protected static boolean CREATE_ECLIPSE_COMPILE_TARGET = true; 84 85 private Document doc; 86 private Element root; 87 private IJavaProject project; 88 private String projectName; 89 private String projectRoot; 90 private Map variable2valueMap; 91 private Shell shell; 92 private Set visited = new TreeSet (); private Node classpathNode; 94 95 102 public BuildFileCreator(IJavaProject project, Shell shell) throws ParserConfigurationException 103 { 104 this.project = project; 105 this.projectName = project.getProject().getName(); 106 this.projectRoot = ExportUtil.getProjectRoot(project); 107 this.variable2valueMap = new LinkedHashMap (); 108 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 109 this.doc = dbf.newDocumentBuilder().newDocument(); 110 this.shell = shell; 111 } 112 113 122 public static List createBuildFiles(Set projects, Shell shell, IProgressMonitor pm) 123 throws JavaModelException, ParserConfigurationException , 124 TransformerConfigurationException , TransformerException , 125 IOException , CoreException, InterruptedException 126 { 127 List res = new ArrayList (); 128 try { 129 createBuildFilesLoop(projects, shell, pm, res); 130 } finally { 131 if (pm != null) { 132 pm.done(); 133 } 134 } 135 return res; 136 } 137 138 private static void createBuildFilesLoop(Set projects, Shell shell, IProgressMonitor pm, 139 List res) throws CoreException, ParserConfigurationException , 140 JavaModelException, TransformerConfigurationException , 141 TransformerFactoryConfigurationError , TransformerException , 142 UnsupportedEncodingException , InterruptedException { 143 144 List files = new ArrayList (); 146 for (Iterator iter = projects.iterator(); iter.hasNext();) 147 { 148 IJavaProject currentProject = (IJavaProject) iter.next(); 149 IFile file = currentProject.getProject().getFile(BuildFileCreator.BUILD_XML); 150 files.add(file); 151 } 152 153 Set confirmedFiles = ExportUtil.validateEdit(shell, files); 155 156 if (pm != null) { 157 pm.beginTask(DataTransferMessages.AntBuildfileExportPage_0, 158 confirmedFiles.size()); 159 } 160 161 int i = 0; 162 for (Iterator iter = projects.iterator(); iter.hasNext(); i++) 163 { 164 IJavaProject currentProject = (IJavaProject) iter.next(); 165 IFile file = currentProject.getProject().getFile(BuildFileCreator.BUILD_XML); 166 if (! confirmedFiles.contains(file)) 167 { 168 continue; 169 } 170 171 if (pm != null) { 172 pm.subTask(currentProject.getProject().getName()); 173 } 174 175 BuildFileCreator instance = new BuildFileCreator(currentProject, shell); 176 instance.createRoot(); 177 instance.createImports(); 178 EclipseClasspath classpath = new EclipseClasspath(currentProject); 179 if (CHECK_SOURCE_CYCLES) { 180 SourceAnalyzer.checkCycles(currentProject, classpath, shell); 181 } 182 instance.createClasspaths(classpath); 183 instance.createInit(classpath.srcDirs, classpath.classDirs); 184 instance.createClean(classpath.classDirs); 185 instance.createCleanAll(); 186 instance.createBuild(classpath.srcDirs, classpath.classDirs, 187 classpath.inclusionLists, classpath.exclusionLists); 188 instance.createBuildRef(); 189 if (CREATE_ECLIPSE_COMPILE_TARGET) { 190 instance.addInitEclipseCompiler(); 191 instance.addBuildEclipse(); 192 } 193 instance.createRun(); 194 instance.addSubProperties(currentProject, classpath); 195 instance.createProperty(); 196 197 String xml = ExportUtil.toString(instance.doc); 199 InputStream is = new ByteArrayInputStream (xml.getBytes("UTF-8")); if (file.exists()) 201 { 202 file.setContents(is, true, true, null); 203 } 204 else 205 { 206 file.create(is, true, null); 207 } 208 209 if (pm != null) { 210 pm.worked(1); 211 if (pm.isCanceled()) { 212 throw new InterruptedException (); 213 } 214 } 215 res.add(instance.projectName); 216 } 217 } 218 219 222 public void createProperty() 223 { 224 boolean source = JavaCore.GENERATE.equals(project.getOption(JavaCore.COMPILER_SOURCE_FILE_ATTR, true)); 226 boolean lines = JavaCore.GENERATE.equals(project.getOption(JavaCore.COMPILER_LINE_NUMBER_ATTR, true)); 227 boolean vars = JavaCore.GENERATE.equals(project.getOption(JavaCore.COMPILER_LOCAL_VARIABLE_ATTR, true)); 228 229 List debuglevel = new ArrayList (); 230 if (source) 231 { 232 debuglevel.add("source"); } 234 if (lines) 235 { 236 debuglevel.add("lines"); } 238 if (vars) 239 { 240 debuglevel.add("vars"); } 242 if (debuglevel.size() == 0) 243 { 244 debuglevel.add("none"); } 246 variable2valueMap.put("debuglevel", ExportUtil.toString(debuglevel, ",")); 248 String target = project.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true); 250 variable2valueMap.put("target", target); 252 255 String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true); 257 variable2valueMap.put("source", sourceLevel); 259 boolean first = true; 261 Node node = root.getFirstChild(); 262 for (Iterator iterator = variable2valueMap.keySet().iterator(); iterator.hasNext();) 263 { 264 String key = (String ) iterator.next(); 265 String value = (String ) variable2valueMap.get(key); 266 Element prop = doc.createElement("property"); prop.setAttribute("name", key); prop.setAttribute("value", value); if (first) 270 { 271 first = false; 272 } 273 else 274 { 275 node = node.getNextSibling(); 276 } 277 node = root.insertBefore(prop, node); 278 } 279 280 Element env = doc.createElement("property"); env.setAttribute("environment", "env"); root.insertBefore(env, root.getFirstChild()); 284 } 285 286 289 public void createRoot() 290 { 291 root = doc.createElement("project"); root.setAttribute("name" , projectName); root.setAttribute("default" , "build"); root.setAttribute("basedir" , "."); doc.appendChild(root); 297 298 Comment comment = doc.createComment(WARNING + ExportUtil.NEWLINE + NOTE); 300 doc.insertBefore(comment, root); 301 } 302 303 306 public void createImports() 307 { 308 File dir = new File (projectRoot); 310 FilenameFilter filter = new FilenameFilter () 311 { 312 public boolean accept(File acceptDir, String name) 313 { 314 return name.endsWith(".xml") && !name.endsWith(BUILD_XML); } 316 }; 317 318 File [] files = dir.listFiles(filter); 319 if (files == null) 320 { 321 return; 322 } 323 for (int i = 0; i < files.length; i++) 324 { 325 File file = files[i]; 328 Document docCandidate; 329 try { 330 docCandidate = ExportUtil.parseXmlFile(file); 331 Node node = docCandidate.getFirstChild(); 332 if (node instanceof ProcessingInstruction && 333 IMPORT_BUILDFILE_PROCESSING_TARGET.equals(((ProcessingInstruction ) node).getTarget().trim())) { 334 Element element = doc.createElement("import"); element.setAttribute("file", file.getName()); root.appendChild(element); 337 } 338 } catch (ParserConfigurationException e){ 339 AntUIPlugin.log("invalid XML file not imported: " + file.getAbsolutePath(), e); } catch (SAXException e) { 341 AntUIPlugin.log("invalid XML file not imported: " + file.getAbsolutePath(), e); } catch (IOException e) { 343 AntUIPlugin.log("invalid XML file not imported: " + file.getAbsolutePath(), e); } 345 } 346 } 347 348 351 public void createClasspaths(EclipseClasspath classpath) throws JavaModelException 352 { 353 createClasspaths(null, project, classpath); 354 } 355 356 360 public void createClasspaths(String pathId, IJavaProject currentProject, EclipseClasspath classpath) 361 throws JavaModelException 362 { 363 if (currentProject == null) 364 { 365 AntUIPlugin.log("project is not loaded in workspace: " + pathId, null); return; 367 } 368 Element element = doc.createElement("path"); if (pathId == null) 375 { 376 pathId = currentProject.getProject().getName() + ".classpath"; } 378 element.setAttribute("id", pathId); visited.add(pathId); 380 variable2valueMap.putAll(classpath.variable2valueMap); 381 for (Iterator iter = ExportUtil.removeDuplicates(classpath.rawClassPathEntries).iterator(); iter.hasNext();) 382 { 383 String entry = (String ) iter.next(); 384 if (EclipseClasspath.isProjectReference(entry)) 385 { 386 Element pathElement = doc.createElement("path"); IJavaProject referencedProject = EclipseClasspath.resolveProjectReference(entry); 388 if (referencedProject == null) 389 { 390 AntUIPlugin.log("project is not loaded in workspace: " + pathId, null); continue; 392 } 393 String refPathId = referencedProject.getProject().getName() + ".classpath"; pathElement.setAttribute("refid", refPathId); element.appendChild(pathElement); 396 if (visited.add(refPathId)) 397 { 398 createClasspaths(null, referencedProject, new EclipseClasspath(referencedProject)); } 400 } 401 else if (EclipseClasspath.isUserLibraryReference(entry) || 402 EclipseClasspath.isUserSystemLibraryReference(entry) || 403 EclipseClasspath.isLibraryReference(entry)) 404 { 405 addUserLibrary(element, entry); 406 } 407 else 408 { 409 String prefix = ""; if (!entry.startsWith("${") && !projectName.equals(currentProject.getProject().getName())) { 414 String currentProjectRoot= ExportUtil.getProjectRoot(currentProject); 415 entry= ExportUtil.getRelativePath(entry, currentProjectRoot); 416 if (!new Path(entry).isAbsolute()) { 417 prefix = "${" + currentProject.getProject().getName() + ".location}/"; } 419 } 420 Element pathElement = doc.createElement("pathelement"); String path = ExportUtil.getRelativePath(prefix + entry, projectRoot); 422 pathElement.setAttribute("location", path); element.appendChild(pathElement); 424 } 425 } 426 addToClasspathBlock(element); 427 } 428 429 private void addUserLibrary(Element element, String entry) 430 { 431 Element pathElement = doc.createElement("path"); IClasspathContainer container = EclipseClasspath.resolveUserLibraryReference(entry); 434 String name = ExportUtil.removePrefixAndSuffix(entry, "${", "}"); pathElement.setAttribute("refid", name); if (!EclipseClasspath.isUserSystemLibraryReference(entry)) 437 { 438 element.appendChild(pathElement); 439 } 440 441 if (visited.add(entry)) 443 { 444 Element userElement = doc.createElement("path"); userElement.setAttribute("id", name); IClasspathEntry entries[] = container.getClasspathEntries(); 447 for (int i = 0; i < entries.length; i++) 448 { 449 String jarFile = entries[i].getPath().toString(); 450 if (EclipseClasspath.isLibraryReference(entry)) 452 { 453 IPath home = JavaCore.getClasspathVariable("ECLIPSE_HOME"); if (home != null && home.isPrefixOf(entries[i].getPath())) 455 { 456 variable2valueMap.put("ECLIPSE_HOME", home.toString()); jarFile = "${ECLIPSE_HOME}" + jarFile.substring(home.toString().length()); } 459 else if (! new File (jarFile).exists() && 460 jarFile.startsWith('/' + projectName) && 461 new File (projectRoot, jarFile.substring(('/' + projectName).length())).exists()) 462 { 463 jarFile = jarFile.substring(('/' + projectName).length() + 1); 468 } 469 } 470 jarFile = ExportUtil.getRelativePath(jarFile, projectRoot); 471 Element userPathElement = doc.createElement("pathelement"); userPathElement.setAttribute("location", jarFile); userElement.appendChild(userPathElement); 474 } 475 addToClasspathBlock(userElement); 476 } 477 } 478 479 private void addToClasspathBlock(Element element) 480 { 481 if (classpathNode == null) 483 { 484 classpathNode = root.appendChild(element); 485 } 486 else 487 { 488 classpathNode = classpathNode.getNextSibling(); 489 classpathNode = root.insertBefore(element, classpathNode); 490 } 491 } 492 493 496 public void addSubProperties(IJavaProject subproject, EclipseClasspath classpath) throws JavaModelException 497 { 498 for (Iterator iterator = ExportUtil.getClasspathProjectsRecursive(subproject).iterator(); iterator.hasNext();) 499 { 500 IJavaProject subProject = (IJavaProject) iterator.next(); 501 String location = subProject.getProject().getName() + ".location"; String subProjectRoot = ExportUtil.getProjectRoot(subProject); 504 String relativePath = ExportUtil.getRelativePath(subProjectRoot, 505 projectRoot); 506 variable2valueMap.put(location, relativePath); 507 variable2valueMap.putAll(classpath.variable2valueMap); 508 } 509 } 510 511 516 public void createInit(List srcDirs, List classDirs) 517 { 518 Element element = doc.createElement("target"); element.setAttribute("name", "init"); List classDirsUnique = ExportUtil.removeDuplicates(classDirs); 524 for (Iterator iterator = classDirsUnique.iterator(); iterator.hasNext();) 525 { 526 String classDir = (String ) iterator.next(); 527 if (!classDir.equals(".") && !EclipseClasspath.isReference(classDir)) 529 { 530 Element pathElement = doc.createElement("mkdir"); pathElement.setAttribute("dir", classDir); element.appendChild(pathElement); 533 } 534 } 535 root.appendChild(element); 536 537 createCopyResources(srcDirs, classDirs, element); 538 } 539 540 private void createCopyResources(List srcDirs, List classDirs, Element element) 541 { 542 String filter = project.getOption(JavaCore.CORE_JAVA_BUILD_RESOURCE_COPY_FILTER, true); 544 StringTokenizer tokenizer = new StringTokenizer (filter, ","); List filters = Collections.list(tokenizer); 546 filters.add("*.java"); 548 for (int i = 0; i < filters.size(); i++) 550 { 551 String item = ((String ) filters.get(i)).trim(); 552 if (item.equals("*")) { 554 return; 556 } 557 filters.set(i, "**/" + item); } 559 560 for (int i = 0; i < srcDirs.size(); i++) 564 { 565 String srcDir = (String ) srcDirs.get(i); 566 String classDir = (String ) classDirs.get(i); 567 if (! EclipseClasspath.isReference(classDir)) 568 { 569 Element copyElement = doc.createElement("copy"); copyElement.setAttribute("todir", classDir); copyElement.setAttribute("includeemptydirs", "false"); Element filesetElement = doc.createElement("fileset"); filesetElement.setAttribute("dir", srcDir); filesetElement.setAttribute("excludes", ExportUtil.toString(filters, ", ")); copyElement.appendChild(filesetElement); 576 element.appendChild(copyElement); 577 } 578 } 579 } 580 581 585 public void createClean(List classDirs) 586 { 587 Element element = doc.createElement("target"); element.setAttribute("name", "clean"); List classDirUnique = ExportUtil.removeDuplicates(classDirs); 593 for (Iterator iterator = classDirUnique.iterator(); iterator.hasNext();) 594 { 595 String classDir = (String ) iterator.next(); 596 if (!classDir.equals(".") && !EclipseClasspath.isReference(classDir)) 598 { 599 Element deleteElement = doc.createElement("delete"); deleteElement.setAttribute("dir", classDir); element.appendChild(deleteElement); 602 } 603 } 604 root.appendChild(element); 605 606 if (classDirs.contains(".")) { 613 Element deleteElement = doc.createElement("delete"); Element filesetElement = doc.createElement("fileset"); filesetElement.setAttribute("dir", "."); filesetElement.setAttribute("includes", "**/*.class"); deleteElement.appendChild(filesetElement); 618 element.appendChild(deleteElement); 619 } 620 } 621 622 625 public void createCleanAll() throws JavaModelException 626 { 627 Element element = doc.createElement("target"); element.setAttribute("name", "cleanall"); element.setAttribute("depends", "clean"); List subProjects = ExportUtil.getClasspathProjectsRecursive(project); 634 for (Iterator iterator = subProjects.iterator(); iterator.hasNext();) 635 { 636 IJavaProject subProject = (IJavaProject) iterator.next(); 637 Element antElement = doc.createElement("ant"); antElement.setAttribute("antfile", "${" + subProject.getProject().getName() + ".location}/" + BUILD_XML); antElement.setAttribute("target", "clean"); antElement.setAttribute("inheritAll", "false"); element.appendChild(antElement); 642 } 643 root.appendChild(element); 644 } 645 646 653 public void createBuild(List srcDirs, List classDirs, List inclusionLists, List exclusionLists) throws JavaModelException 654 { 655 Element element = doc.createElement("target"); element.setAttribute("name", "build"); element.setAttribute("depends", "build-subprojects,build-project"); root.appendChild(element); 660 661 element = doc.createElement("target"); element.setAttribute("name", "build-subprojects"); List subProjects = ExportUtil.getClasspathProjectsRecursive(project); 667 for (Iterator iterator = subProjects.iterator(); iterator.hasNext();) 668 { 669 IJavaProject subProject = (IJavaProject) iterator.next(); 670 Element antElement = doc.createElement("ant"); antElement.setAttribute("antfile", "${" + subProject.getProject().getName() + ".location}/" + BUILD_XML); antElement.setAttribute("target", "build-project"); antElement.setAttribute("inheritAll", "false"); if (CREATE_ECLIPSE_COMPILE_TARGET) { 675 Element propertysetElement = doc.createElement("propertyset"); Element propertyrefElement = doc.createElement("propertyref"); propertyrefElement.setAttribute("name", "build.compiler"); propertysetElement.appendChild(propertyrefElement); 679 antElement.appendChild(propertysetElement); 680 } 681 element.appendChild(antElement); 682 } 683 root.appendChild(element); 684 685 element = doc.createElement("target"); element.setAttribute("name", "build-project"); element.setAttribute("depends", "init"); Element echoElement = doc.createElement("echo"); echoElement.setAttribute("message", "${ant.project.name}: ${ant.file}"); element.appendChild(echoElement); 700 for (int i = 0; i < srcDirs.size(); i++) 701 { 702 String srcDir = (String ) srcDirs.get(i); 703 if (!EclipseClasspath.isReference(srcDir)) 704 { 705 String classDir = (String ) classDirs.get(i); 706 List inclusions = (List ) inclusionLists.get(i); 707 List exclusions = (List ) exclusionLists.get(i); 708 Element javacElement = doc.createElement("javac"); javacElement.setAttribute("destdir", classDir); javacElement.setAttribute("debug", "true"); javacElement.setAttribute("debuglevel", "${debuglevel}"); javacElement.setAttribute("source", "${source}"); javacElement.setAttribute("target", "${target}"); 715 Element srcElement = doc.createElement("src"); srcElement.setAttribute("path", srcDir); javacElement.appendChild(srcElement); 718 719 for (Iterator iter = inclusions.iterator(); iter.hasNext();) 720 { 721 String inclusion = (String ) iter.next(); 722 Element includeElement = doc.createElement("include"); includeElement.setAttribute("name", inclusion); javacElement.appendChild(includeElement); 725 } 726 for (Iterator iter = exclusions.iterator(); iter.hasNext();) 727 { 728 String exclusion = (String ) iter.next(); 729 Element excludeElement = doc.createElement("exclude"); excludeElement.setAttribute("name", exclusion); javacElement.appendChild(excludeElement); 732 } 733 Element classpathRefElement = doc.createElement("classpath"); classpathRefElement.setAttribute("refid", projectName + ".classpath"); javacElement.appendChild(classpathRefElement); 736 element.appendChild(javacElement); 737 738 addCompilerBootClasspath(srcDirs, javacElement); 739 } 740 } 741 root.appendChild(element); 742 } 743 744 748 private void createBuildRef() throws JavaModelException { 749 750 Set refProjects = new TreeSet (ExportUtil.getJavaProjectComparator()); 751 IJavaProject[] projects = project.getJavaModel().getJavaProjects(); 752 for (int i = 0; i < projects.length; i++) { 753 List subProjects = ExportUtil.getClasspathProjects(projects[i]); 754 for (Iterator iter = subProjects.iterator(); iter.hasNext();) { 755 IJavaProject p = (IJavaProject) iter.next(); 756 if (projectName.equals(p.getProject().getName())) { 757 refProjects.add(projects[i]); 758 } 759 } 760 } 761 762 Element element = doc.createElement("target"); element.setAttribute("name", "build-refprojects"); element.setAttribute("description", "Build all projects which " + "reference this project. Useful to propagate changes."); for (Iterator iter = refProjects.iterator(); iter.hasNext();) { 771 IJavaProject p = (IJavaProject) iter.next(); 772 String location = p.getProject().getName() + ".location"; String refProjectRoot = ExportUtil.getProjectRoot(p); 774 String relativePath = ExportUtil.getRelativePath(refProjectRoot, 775 projectRoot); 776 variable2valueMap.put(location, relativePath); 777 778 Element antElement = doc.createElement("ant"); antElement.setAttribute("antfile", "${" + p.getProject().getName() + ".location}/" + BUILD_XML); antElement.setAttribute("target", "clean"); antElement.setAttribute("inheritAll", "false"); element.appendChild(antElement); 783 784 antElement = doc.createElement("ant"); antElement.setAttribute("antfile", "${" + p.getProject().getName() + ".location}/" + BUILD_XML); antElement.setAttribute("target", "build"); antElement.setAttribute("inheritAll", "false"); if (CREATE_ECLIPSE_COMPILE_TARGET) { 789 Element propertysetElement = doc.createElement("propertyset"); Element propertyrefElement = doc.createElement("propertyref"); propertyrefElement.setAttribute("name", "build.compiler"); propertysetElement.appendChild(propertyrefElement); 793 antElement.appendChild(propertysetElement); 794 } 795 element.appendChild(antElement); 796 } 797 root.appendChild(element); 798 } 799 800 804 public void addInitEclipseCompiler() 805 { 806 IPath value = JavaCore.getClasspathVariable("ECLIPSE_HOME"); if (value != null) { 809 variable2valueMap.put("ECLIPSE_HOME", ExportUtil.getRelativePath( value.toString(), projectRoot)); 811 } 812 Element element = doc.createElement("target"); element.setAttribute("name", "init-eclipse-compiler"); element.setAttribute("description", "copy Eclipse compiler jars to ant lib directory"); Element copyElement = doc.createElement("copy"); copyElement.setAttribute("todir", "${ant.library.dir}"); Element filesetElement = doc.createElement("fileset"); filesetElement.setAttribute("dir", "${ECLIPSE_HOME}/plugins"); filesetElement.setAttribute("includes", "org.eclipse.jdt.core_*.jar"); copyElement.appendChild(filesetElement); 831 element.appendChild(copyElement); 832 Element unzipElement = doc.createElement("unzip"); unzipElement.setAttribute("dest", "${ant.library.dir}"); Element patternsetElement = doc.createElement("patternset"); patternsetElement.setAttribute("includes", "jdtCompilerAdapter.jar"); unzipElement.appendChild(patternsetElement); 837 unzipElement.appendChild(filesetElement.cloneNode(false)); 838 element.appendChild(unzipElement); 839 root.appendChild(element); 840 } 841 842 845 public void addBuildEclipse() 846 { 847 Element element = doc.createElement("target"); element.setAttribute("name", "build-eclipse-compiler"); element.setAttribute("description", "compile project with Eclipse compiler"); Element propertyElement = doc.createElement("property"); propertyElement.setAttribute("name", "build.compiler"); propertyElement.setAttribute("value", "org.eclipse.jdt.core.JDTCompilerAdapter"); element.appendChild(propertyElement); 858 Element antcallElement = doc.createElement("antcall"); antcallElement.setAttribute("target", "build"); element.appendChild(antcallElement); 861 root.appendChild(element); 862 } 863 864 867 private void addCompilerBootClasspath(List srcDirs, Element javacElement) 868 { 869 Element bootclasspathElement = doc.createElement("bootclasspath"); boolean bootclasspathUsed = false; 874 for (Iterator iter = srcDirs.iterator(); iter.hasNext();) 875 { 876 String entry = (String ) iter.next(); 877 if (EclipseClasspath.isUserSystemLibraryReference(entry)) 878 { 879 Element pathElement = doc.createElement("path"); pathElement.setAttribute("refid", ExportUtil.removePrefixAndSuffix(entry, "${", "}")); bootclasspathElement.appendChild(pathElement); 882 bootclasspathUsed = true; 883 } 884 } 885 if (bootclasspathUsed) 886 { 887 javacElement.appendChild(bootclasspathElement); 888 } 889 } 890 891 897 public void createRun() throws CoreException, TransformerFactoryConfigurationError , UnsupportedEncodingException 898 { 899 ILaunchConfiguration[] confs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(); 908 boolean junitUsed = false; 909 for (int i = 0; i < confs.length; i++) 910 { 911 ILaunchConfiguration conf = confs[i]; 912 if (!projectName.equals(conf.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""))) { 914 continue; 915 } 916 917 if (conf.getType().getIdentifier().equals(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION)) 918 { 919 addJavaApplication(variable2valueMap, conf); 920 } 921 else if (conf.getType().getIdentifier().equals(IJavaLaunchConfigurationConstants.ID_JAVA_APPLET)) 922 { 923 addApplet(variable2valueMap, conf); 924 } 925 else if (conf.getType().getIdentifier().equals("org.eclipse.jdt.junit.launchconfig" )) { 927 addJUnit(variable2valueMap, conf); 928 junitUsed = true; 929 } 930 } 931 932 if (junitUsed) 933 { 934 addJUnitReport(); 935 } 936 } 937 938 944 public void addJavaApplication(Map variable2value, ILaunchConfiguration conf) throws CoreException 945 { 946 Element element = doc.createElement("target"); element.setAttribute("name", conf.getName()); Element javaElement = doc.createElement("java"); javaElement.setAttribute("fork", "yes"); javaElement.setAttribute("classname", conf.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "")); javaElement.setAttribute("failonerror", "true"); String dir = conf.getAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, ""); ExportUtil.addVariable(variable2value, dir, projectRoot); 954 if (!dir.equals("")) { 956 javaElement.setAttribute("dir", ExportUtil.getRelativePath(dir, projectRoot)); } 958 if (!conf.getAttribute(ILaunchManager.ATTR_APPEND_ENVIRONMENT_VARIABLES, true)) 959 { 960 javaElement.setAttribute("newenvironment", "true"); } 962 Map props = conf.getAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, new TreeMap ()); 963 addElements(props, doc, javaElement, "env", "key", "value"); addElement(conf.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, ""), doc, javaElement, "jvmarg", "line", variable2value, projectRoot); addElement(conf.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, ""), doc, javaElement, "arg", "line", variable2value, projectRoot); element.appendChild(javaElement); 967 968 addRuntimeClasspath(conf, javaElement); 969 addRuntimeBootClasspath(conf, javaElement); 970 root.appendChild(element); 971 } 972 973 982 public void addApplet(Map variable2value, ILaunchConfiguration conf) throws CoreException, TransformerFactoryConfigurationError , UnsupportedEncodingException 983 { 984 String dir = conf.getAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, ""); if (dir.equals("")) { 987 dir = projectRoot; 988 } 989 ExportUtil.addVariable(variable2value, dir, projectRoot); 990 String value; 991 try 992 { 993 value = VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(dir); 994 } 995 catch (CoreException e) 996 { 997 value = null; 999 } 1000 1001 String htmlfile = ((value != null) ? value : dir) + '/' + conf.getName() + ".html"; if (ExportUtil.existsUserFile(htmlfile) && !MessageDialog.openConfirm(shell, DataTransferMessages.AntBuildfileExportPage_4, DataTransferMessages.AntBuildfileExportPage_4 + ": " + htmlfile)) { 1005 return; 1006 } 1007 IJavaProject javaProject = ExportUtil.getJavaProjectByName(projectName); 1008 IFile file = javaProject.getProject().getFile(conf.getName() + ".html"); if (ExportUtil.validateEdit(shell, file)) { 1011 String html = AppletUtil.buildHTMLFile(conf); 1013 InputStream is = new ByteArrayInputStream (html.getBytes("UTF-8")); if (file.exists()) 1015 { 1016 file.setContents(is, true, true, null); 1017 } 1018 else 1019 { 1020 file.create(is, true, null); 1021 } 1022 } 1023 1024 Element element = doc.createElement("target"); element.setAttribute("name", conf.getName()); Element javaElement = doc.createElement("java"); javaElement.setAttribute("fork", "yes"); javaElement.setAttribute("classname", conf.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_APPLETVIEWER_CLASS, "sun.applet.AppletViewer")); javaElement.setAttribute("failonerror", "true"); if (value != null) 1031 { 1032 javaElement.setAttribute("dir", ExportUtil.getRelativePath(dir, projectRoot)); } 1034 addElement(conf.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, ""), doc, javaElement, "jvmarg", "line", variable2value, projectRoot); addElement(conf.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, ""), doc, javaElement, "arg", "line", variable2value, projectRoot); addElement(conf.getName() + ".html", doc, javaElement, "arg", "line", variable2value, projectRoot); element.appendChild(javaElement); 1038 addRuntimeClasspath(conf, javaElement); 1039 addRuntimeBootClasspath(conf, javaElement); 1040 root.appendChild(element); 1041 } 1042 1043 1049 public void addJUnit(Map variable2value, ILaunchConfiguration conf) throws CoreException 1050 { 1051 String testClass = conf.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, ""); Element element = doc.createElement("target"); element.setAttribute("name", conf.getName()); 1065 Element mkdirElement = doc.createElement("mkdir"); mkdirElement.setAttribute("dir", "${junit.output.dir}"); element.appendChild(mkdirElement); 1068 1069 Element junitElement = doc.createElement("junit"); junitElement.setAttribute("fork", "yes"); junitElement.setAttribute("printsummary", "withOutAndErr"); String dir = conf.getAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, ""); ExportUtil.addVariable(variable2value, dir, projectRoot); 1074 if (!dir.equals("")) { 1076 junitElement.setAttribute("dir", ExportUtil.getRelativePath(dir, projectRoot)); } 1078 if (!conf.getAttribute(ILaunchManager.ATTR_APPEND_ENVIRONMENT_VARIABLES, true)) 1079 { 1080 junitElement.setAttribute("newenvironment", "true"); } 1082 Element formatterElement = doc.createElement("formatter"); formatterElement.setAttribute("type", "xml"); junitElement.appendChild(formatterElement); 1085 if (!testClass.equals("")) { 1087 Element testElement = doc.createElement("test"); testElement.setAttribute("name", testClass); testElement.setAttribute("todir", "${junit.output.dir}"); junitElement.appendChild(testElement); 1092 } 1093 else 1094 { 1095 String container = conf.getAttribute("org.eclipse.jdt.junit.CONTAINER" , ""); IType[] types = ExportUtil.findTestsInContainer(container); 1098 Set sortedTypes = new TreeSet (ExportUtil.getITypeComparator()); 1099 sortedTypes.addAll(Arrays.asList(types)); 1100 for (Iterator iter = sortedTypes.iterator(); iter.hasNext();) { 1101 IType type = (IType) iter.next(); 1102 Element testElement = doc.createElement("test"); testElement.setAttribute("name", type.getFullyQualifiedName()); testElement.setAttribute("todir", "${junit.output.dir}"); junitElement.appendChild(testElement); 1106 } 1107 } 1108 Map props = conf.getAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, new TreeMap ()); 1109 addElements(props, doc, junitElement, "env", "key", "value"); addElement(conf.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, ""), doc, junitElement, "jvmarg", "line", variable2value, projectRoot); element.appendChild(junitElement); 1112 addRuntimeClasspath(conf, junitElement); 1113 addRuntimeBootClasspath(conf, junitElement); 1114 root.appendChild(element); 1115 } 1116 1117 1120 public void addJUnitReport() 1121 { 1122 variable2valueMap.put("junit.output.dir", JUNIT_OUTPUT_DIR); 1124 Element element = doc.createElement("target"); element.setAttribute("name", "junitreport"); Element junitreport = doc.createElement("junitreport"); junitreport.setAttribute("todir", "${junit.output.dir}"); Element fileset = doc.createElement("fileset"); fileset.setAttribute("dir", "${junit.output.dir}"); junitreport.appendChild(fileset); 1139 Element include = doc.createElement("include"); include.setAttribute("name", "TEST-*.xml"); fileset.appendChild(include); 1142 Element report = doc.createElement("report"); report.setAttribute("format", "frames"); report.setAttribute("todir", "${junit.output.dir}"); junitreport.appendChild(report); 1146 element.appendChild(junitreport); 1147 root.appendChild(element); 1148 } 1149 1150 1153 private void addRuntimeClasspath(ILaunchConfiguration conf, Element javaElement) throws JavaModelException 1154 { 1155 Element classpathRefElement = doc.createElement("classpath"); EclipseClasspath runtimeClasspath = new EclipseClasspath(project, conf, false); 1158 if (ExportUtil.isDefaultClasspath(project, runtimeClasspath)) 1159 { 1160 classpathRefElement.setAttribute("refid", projectName + ".classpath"); } 1162 else 1163 { 1164 String pathId = "run." + conf.getName() + ".classpath"; classpathRefElement.setAttribute("refid", pathId); createClasspaths(pathId, project, runtimeClasspath); 1167 } 1168 javaElement.appendChild(classpathRefElement); 1169 } 1170 1171 1174 private void addRuntimeBootClasspath(ILaunchConfiguration conf, Element javaElement) throws JavaModelException 1175 { 1176 EclipseClasspath bootClasspath = new EclipseClasspath(project, conf, true); 1181 if (bootClasspath.rawClassPathEntries.size() > 0) 1182 { 1183 String pathId = "run." + conf.getName() + ".bootclasspath"; createClasspaths(pathId, project, bootClasspath); 1185 Element bootclasspath = doc.createElement("bootclasspath"); Element classpathRefElement = doc.createElement("path"); classpathRefElement.setAttribute("refid", pathId); bootclasspath.appendChild(classpathRefElement); 1189 Element jrelib = doc.createElement("fileset"); jrelib.setAttribute("dir", "${java.home}/lib"); jrelib.setAttribute("includes", "*.jar"); bootclasspath.appendChild(jrelib); 1193 javaElement.appendChild(bootclasspath); 1194 } 1195 } 1196 1197 1209 private static void addElement(String cmdLineArgs, Document doc, 1210 Element element, String elementName, String attributeName, 1211 Map variable2valueMap, String projectRoot) { 1212 1213 if (cmdLineArgs == null || cmdLineArgs.length() == 0) { 1214 return; 1215 } 1216 ExportUtil.addVariable(variable2valueMap, cmdLineArgs, projectRoot); 1217 Element itemElement = doc.createElement(elementName); 1218 itemElement.setAttribute(attributeName, cmdLineArgs); 1219 element.appendChild(itemElement); 1220 } 1221 1222 1233 private static void addElements(Map map, Document doc, Element element, String elementName, 1234 String keyAttributeName, String valueAttributeName) 1235 { 1236 for (Iterator iter = map.keySet().iterator(); iter.hasNext();) 1237 { 1238 String key = (String ) iter.next(); 1239 String value = (String ) map.get(key); 1240 Element itemElement = doc.createElement(elementName); 1241 itemElement.setAttribute(keyAttributeName, key); 1242 itemElement.setAttribute(valueAttributeName, value); 1243 element.appendChild(itemElement); 1244 } 1245 } 1246 1247 1254 public static void setOptions(String buildfilename, String junitdir, 1255 boolean checkcycles, boolean eclipsecompiler) { 1256 1257 if (buildfilename.length() > 0) { 1258 BUILD_XML = buildfilename; 1259 } 1260 if (junitdir.length() > 0) { 1261 JUNIT_OUTPUT_DIR = junitdir; 1262 } 1263 CHECK_SOURCE_CYCLES = checkcycles; 1264 CREATE_ECLIPSE_COMPILE_TARGET = eclipsecompiler; 1265 } 1266} | Popular Tags |