Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2004, 2009 Richard Hoefter and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: Richard Hoefter (richard.hoefter@web.de) - initial API and implementation, bug 95297, bug 97051, bug 128103, bug 201180, bug 161354 IBM Corporation - nlsing and incorporating into Eclipse, bug 108276, bug 124210, bug 161845, bug 177833 Nikolay Metchev (N.Metchev@teamphone.com) - bug 108276 Ryan Fong (rfong@trapezenetworks.com) - bug 201143 /
  
  
  package org.eclipse.ant.internal.ui.datatransfer;
  
  import java.io.File;
  import java.util.Arrays;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  import java.util.TreeMap;
  import java.util.TreeSet;
  
  
  import  org.eclipse.swt.widgets.Shell;
  import org.w3c.dom.Node;
Creates build.xml file.
  
  public class BuildFileCreator
  {
      protected static final String IMPORT_BUILDFILE_PROCESSING_TARGET = "eclipse.ant.import"//$NON-NLS-1$
      protected static final String WARNING = " WARNING: Eclipse auto-generated file." + . + //$NON-NLS-1$
                                              "              Any modifications will be overwritten."//$NON-NLS-1$
      protected static final String NOTE =    "              To include a user specific buildfile here, " + //$NON-NLS-1$
                                              "simply create one in the same" + . + //$NON-NLS-1$
                                              "              directory with the processing instruction " + //$NON-NLS-1$
                                              "<?" +   + "?>" + . + //$NON-NLS-1$ //$NON-NLS-2$
                                              "              as the first entry and export the buildfile again. "//$NON-NLS-1$
  
      protected static String BUILD_XML = "build.xml"//$NON-NLS-1$
      protected static String JUNIT_OUTPUT_DIR = "junit"//$NON-NLS-1$
      protected static boolean CHECK_SOURCE_CYCLES = true;
      protected static boolean CREATE_ECLIPSE_COMPILE_TARGET = true;
      
      private Document doc;
      private Element root;
      private IJavaProject project;
      private String projectName;
      private String projectRoot;
      private Map variable2valueMap;
      private Shell shell;
      private Set visited = new TreeSet(); // record used sub classpaths
      private Node classpathNode;
    
    
Constructor. Please prefer createBuildFiles(Set, Shell, IProgressMonitor) if you do not want call the various createXXX() methods yourself.

Parameters:
project create buildfile for this project
shell parent instance for dialogs
 
     public BuildFileCreator(IJavaProject project, Shell shellthrows ParserConfigurationException
     {
         this. = project;
         this. = project.getProject().getName();
         this. = ExportUtil.getProjectRoot(project);
         this. = new LinkedHashMap();
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         this. = dbf.newDocumentBuilder().newDocument();
         this. = shell;
     }
      
    
Create buildfile for given projects.

Parameters:
projects create buildfiles for these IJavaProject objects
shell parent instance for dialogs
Returns:
project names for which buildfiles were created
Throws:
InterruptedException thrown when user cancels task
 
     public static List createBuildFiles(Set projects, Shell shellIProgressMonitor pm)
                IOExceptionCoreExceptionInterruptedException
     {
         List res = new ArrayList();
         try {
             createBuildFilesLoop(projectsshellpmres);
         } finally {
             if (pm != null) {
                 pm.done();
             }
         }
         return res;
     }
 
     private static void createBuildFilesLoop(Set projects, Shell shellIProgressMonitor pmList resthrows CoreExceptionParserConfigurationException,
             UnsupportedEncodingException {
 
         // determine files to create/change
         List files = new ArrayList();
         for (Iterator iter = projects.iterator(); iter.hasNext();)
         {
             IJavaProject currentProject = (IJavaProjectiter.next();
             IFile file = currentProject.getProject().getFile(.);
             files.add(file);
         }
 
         // trigger checkout
         Set confirmedFiles = ExportUtil.validateEdit(shellfiles);
         SubMonitor localmonitor = SubMonitor.convert(pm.confirmedFiles.size());
         try {
             int i = 0;
             for (Iterator iter = projects.iterator(); iter.hasNext(); i++)
             {
                 IJavaProject currentProject = (IJavaProjectiter.next();
                 IFile file = currentProject.getProject().getFile(.);
                 if (! confirmedFiles.contains(file))
                 {
                     continue;
                 }
                 
                 localmonitor.setTaskName(NLS.bind(.currentProject.getProject().getName()));
                 
                 BuildFileCreator instance = new BuildFileCreator(currentProjectshell);
                 instance.createRoot();
                 instance.createImports();
                 EclipseClasspath classpath = new EclipseClasspath(currentProject);
                 if () {
                     SourceAnalyzer.checkCycles(currentProjectclasspathshell);
                 }
                 instance.createClasspaths(classpath);
                 instance.createInit(classpath.srcDirsclasspath.classDirs,
                     classpath.inclusionListsclasspath.exclusionLists);   
                 instance.createClean(classpath.classDirs);
                 instance.createCleanAll();
                 instance.createBuild(classpath.srcDirsclasspath.classDirs,
                                      classpath.inclusionListsclasspath.exclusionLists);
                 instance.createBuildRef();
                 if () {
                     instance.addInitEclipseCompiler();
                     instance.addBuildEclipse();
                 }
                 instance.createRun();           
                 instance.addSubProperties(currentProjectclasspath);
                 instance.createProperty();
     
                 // write build file
                 String xml = ExportUtil.toString(instance.doc);
                 InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8")); //$NON-NLS-1$
                 if (file.exists())
                 {
                     file.setContents(istruetruenull);
                 }
                 else
                 {
                     file.create(istruenull);
                 }    
                 if(localmonitor.isCanceled()) {
                     return;
                 }
                 localmonitor.worked(1);
                 res.add(instance.projectName);
             }
         }
         finally {
             if(!localmonitor.isCanceled()) {
                 localmonitor.done();
             }
         }
     }

    
Add property tag.
 
     public void createProperty()
     {
         // read debug options from Eclipse settings
         boolean source = ..equals(.getOption(.true));
         boolean lines = ..equals(.getOption(.true));
         boolean vars = ..equals(.getOption(.true));
         
         List debuglevel = new ArrayList();
         if (source)
         {
             debuglevel.add("source"); //$NON-NLS-1$
         }
         if (lines)
         {
             debuglevel.add("lines"); //$NON-NLS-1$
         }
         if (vars)
         {
             debuglevel.add("vars"); //$NON-NLS-1$
         }
         if (debuglevel.size() == 0)
         {
             debuglevel.add("none"); //$NON-NLS-1$
         }
         .put("debuglevel", ExportUtil.toString(debuglevel",")); //$NON-NLS-1$ //$NON-NLS-2$
 
         // "Generated .class files compatibility"
         String target = .getOption(.true); 
         .put("target"target); //$NON-NLS-1$
 
         // "Compiler compliance level"
         //String compliance = project.getOption(JavaCore.COMPILER_COMPLIANCE, true); 
         
         // "Source compatibility"
         String sourceLevel = .getOption(.true);
         .put("source"sourceLevel); //$NON-NLS-1$
         
         // <property name="x" value="y"/>
         boolean first = true;
         Node node = .getFirstChild();
         for (Iterator iterator = .keySet().iterator(); iterator.hasNext();)
         {
             String key = (Stringiterator.next();
             String value = (String.get(key);
             Element prop = .createElement("property"); //$NON-NLS-1$
             prop.setAttribute("name"key); //$NON-NLS-1$
             prop.setAttribute("value"value); //$NON-NLS-1$
             if (first)
             {
                 first = false;               
             }
             else
             {
                 node = node.getNextSibling();
             }
             node = .insertBefore(propnode);
         }
         
         // <property environment="env"/>
         Element env = .createElement("property"); //$NON-NLS-1$
         env.setAttribute("environment""env"); //$NON-NLS-1$ //$NON-NLS-2$
         .insertBefore(env.getFirstChild());
     }

    
Create project tag.
 
     public void createRoot()
     {   
         // <project name="hello" default="build" basedir=".">
          = .createElement("project"); //$NON-NLS-1$
         .setAttribute("name" , ); //$NON-NLS-1$
         .setAttribute("default" , "build"); //$NON-NLS-1$ //$NON-NLS-2$
         .setAttribute("basedir" , "."); //$NON-NLS-1$ //$NON-NLS-2$
         .appendChild();
         
         // <!-- warning -->
         Comment comment = .createComment( + . + );
         .insertBefore(comment);
     }
    
    
Find buildfiles in project root directory and automatically import them.
 
     public void createImports()
     {
         // <import file="javadoc.xml"/>
         File dir = new File();
         FilenameFilter filter = new FilenameFilter()
         {
             public boolean accept(File acceptDirString name)
             {
                 return name.endsWith(".xml") && !name.endsWith(); //$NON-NLS-1$
             }
         };
 
         File[] files = dir.listFiles(filter);
         if (files == null)
         {
             return;
         }
         for (int i = 0; i < files.lengthi++)
         {
             // import file if it is an XML document with marker comment as first
             // child
             File file = files[i];
             Document docCandidate;
             try {
                 docCandidate = ExportUtil.parseXmlFile(file);
                 Node node = docCandidate.getFirstChild();
                 if (node instanceof ProcessingInstruction &&  
                         .equals(((ProcessingInstructionnode).getTarget().trim())) {
                     Element element = .createElement("import"); //$NON-NLS-1$
                     element.setAttribute("file"file.getName()); //$NON-NLS-1$
                     .appendChild(element);
                 }
             } catch (ParserConfigurationException e){
                 AntUIPlugin.log("invalid XML file not imported: " + file.getAbsolutePath(), e); //$NON-NLS-1$
             } catch (SAXException e) {
                 AntUIPlugin.log("invalid XML file not imported: " + file.getAbsolutePath(), e); //$NON-NLS-1$
             } catch (IOException e) {
                 AntUIPlugin.log("invalid XML file not imported: " + file.getAbsolutePath(), e); //$NON-NLS-1$
             }
         }
     }

    
Create classpath tags.
 
     public void createClasspaths(EclipseClasspath classpaththrows JavaModelException
     {
         createClasspaths(nullclasspath);
     }
    
    
Create classpath tags. Allows to specify ID.

Parameters:
pathId specify id, if null project name is used
 
     public void createClasspaths(String pathIdIJavaProject currentProjectEclipseClasspath classpath)
         throws JavaModelException
     {
         if (currentProject == null)
         {
             AntUIPlugin.log("project is not loaded in workspace: " + pathIdnull); //$NON-NLS-1$ 
             return;
         }
         // <path id="hello.classpath">
         //     <pathelement location="${hello.location}/classes"/>
         //     <pathelement location="${hello.location}/x.jar"/>
         //     <path refid="${goodbye.classpath}"/>
         // </path>
         Element element = .createElement("path"); //$NON-NLS-1$
         String pathid = pathId;
         if (pathid == null)
         {
             pathid = currentProject.getProject().getName() + ".classpath"//$NON-NLS-1$
         }
         element.setAttribute("id"pathid); //$NON-NLS-1$
         .add(pathid);
         .putAll(classpath.variable2valueMap);
         for (Iterator iter = ExportUtil.removeDuplicates(classpath.rawClassPathEntries).iterator(); iter.hasNext();)
         {
             String entry = (Stringiter.next(); 
             if (EclipseClasspath.isProjectReference(entry))
             {
                 Element pathElement = .createElement("path"); //$NON-NLS-1$
                 IJavaProject referencedProject = EclipseClasspath.resolveProjectReference(entry); 
                 if (referencedProject == null)
                 {
                     AntUIPlugin.log("project is not loaded in workspace: " + pathidnull); //$NON-NLS-1$
                     continue;
                 }
                 String refPathId = referencedProject.getProject().getName() + ".classpath"//$NON-NLS-1$
                 pathElement.setAttribute("refid"refPathId); //$NON-NLS-1$
                 element.appendChild(pathElement);               
                 if (.add(refPathId))
                 {
                     createClasspaths(nullreferencedProjectnew EclipseClasspath(referencedProject)); // recursion
                 }
             }
             else if (EclipseClasspath.isUserLibraryReference(entry) ||
                      EclipseClasspath.isLibraryReference(entry))
             {
                 addUserLibrary(elemententry);
             }
             else if (EclipseClasspath.isUserSystemLibraryReference(entry))
             {
                 if (pathid.endsWith(".bootclasspath")) { //$NON-NLS-1$
                     addUserLibrary(elemententry);
                 }
             }            
             else if (EclipseClasspath.isJreReference(entry))
             {
                 if (pathid.endsWith(".bootclasspath")) { //$NON-NLS-1$
                     addJre(element);
                 }
             }
             else
             {
                 // prefix with ${project.location}
                 String prefix = ""//$NON-NLS-1$
                 if (!entry.startsWith("${") &&                                  // no variable ${var}/classes //$NON-NLS-1$
                     !.equals(currentProject.getProject().getName())) // not main project 
                 {
                     String currentProjectRoot= ExportUtil.getProjectRoot(currentProject);
                     entry= ExportUtil.getRelativePath(entrycurrentProjectRoot);
                     if (!new Path(entry).isAbsolute()) {
                         prefix = "${" + currentProject.getProject().getName() + ".location}/"//$NON-NLS-1$ //$NON-NLS-2$
                     }
                 }
                 Element pathElement = .createElement("pathelement"); //$NON-NLS-1$
                 String path = ExportUtil.getRelativePath(prefix + entry);
                 pathElement.setAttribute("location"path); //$NON-NLS-1$
                 element.appendChild(pathElement);
             }
         }
         addToClasspathBlock(element);
     }
 
     private void addUserLibrary(Element elementString entry)
     {
         // add classpath reference
         Element pathElement = .createElement("path"); //$NON-NLS-1$
         IClasspathContainer container = EclipseClasspath.resolveUserLibraryReference(entry); 
         String name = ExportUtil.removePrefixAndSuffix(entry"${""}"); //$NON-NLS-1$ //$NON-NLS-2$
         pathElement.setAttribute("refid"name); //$NON-NLS-1$
         element.appendChild(pathElement);
 
         // add classpath
         if (.add(entry))
         {
             Element userElement = .createElement("path"); //$NON-NLS-1$
             userElement.setAttribute("id"name); //$NON-NLS-1$
             IClasspathEntry entries[] = container.getClasspathEntries();
             for (int i = 0; i < entries.lengthi++)
             {   
                 String jarFile = entries[i].getPath().toString();
                 // use ECLIPSE_HOME variable for library jars
                 if (EclipseClasspath.isLibraryReference(entry))
                 {
                     IPath home = JavaCore.getClasspathVariable("ECLIPSE_HOME"); //$NON-NLS-1$
                     if (home != null && home.isPrefixOf(entries[i].getPath()))
                     {
                         .put("ECLIPSE_HOME"home.toString()); //$NON-NLS-1$
                         jarFile = "${ECLIPSE_HOME}" + jarFile.substring(home.toString().length()); //$NON-NLS-1$ 
                     }
                     else if (! new File(jarFile).exists() &&
                             jarFile.startsWith('/' + ) &&
                             new File(jarFile.substring(('/' + ).length())).exists())
                     {
                         // workaround that additional jars are stored with
                         // leading project root in container object, although
                         // they are relative and indeed correctly stored in
                         // build.properties (jars.extra.classpath)
                         jarFile = jarFile.substring(('/' + ).length() + 1);
                     }
                 }
                 jarFile = ExportUtil.getRelativePath(jarFile);
                 Element userPathElement = .createElement("pathelement"); //$NON-NLS-1$
                 userPathElement.setAttribute("location"jarFile); //$NON-NLS-1$
                 userElement.appendChild(userPathElement);
             }
             addToClasspathBlock(userElement);
         }
     }
    
    
Add JRE to given classpath.

Parameters:
element classpath tag
 
     private void addJre(Element element)
     {
         // <fileset dir="${java.home}/lib" includes="*.jar"/>
         // <fileset dir="${java.home}/lib/ext" includes="*.jar"/>
         Element pathElement = .createElement("fileset"); //$NON-NLS-1$
         pathElement.setAttribute("dir""${java.home}/lib"); //$NON-NLS-1$ //$NON-NLS-2$
         pathElement.setAttribute("includes""*.jar"); //$NON-NLS-1$ //$NON-NLS-2$
         element.appendChild(pathElement);
         pathElement = .createElement("fileset"); //$NON-NLS-1$
         pathElement.setAttribute("dir""${java.home}/lib/ext"); //$NON-NLS-1$ //$NON-NLS-2$
         pathElement.setAttribute("includes""*.jar"); //$NON-NLS-1$ //$NON-NLS-2$
         element.appendChild(pathElement);
     }
 
     private void addToClasspathBlock(Element element)
     {
         // remember node to insert all classpaths at same location
         if ( == null)
         {
              = .appendChild(element);
         }
         else
         {
              = .getNextSibling();
              = .insertBefore(element);
         }
     }
    
    
Add properties of sub-projects to internal properties map.
 
     public void addSubProperties(IJavaProject subprojectEclipseClasspath classpaththrows JavaModelException
     { 
         for (Iterator iterator = ExportUtil.getClasspathProjectsRecursive(subproject).iterator(); iterator.hasNext();)
         {
             IJavaProject subProject = (IJavaProjectiterator.next(); 
             String location = subProject.getProject().getName() + ".location"//$NON-NLS-1$
             // add subproject properties to variable2valueMap
             String subProjectRoot = ExportUtil.getProjectRoot(subProject);
             String relativePath = ExportUtil.getRelativePath(subProjectRoot,
                     );
             .put(locationrelativePath);
             .putAll(classpath.variable2valueMap);    
         }
     }

    
Create init target. Creates directories and copies resources.

Parameters:
srcDirs source directories to copy resources from
classDirs classes directories to copy resources to
 
     public void createInit(List srcDirsList classDirs,
         List inclusionListsList exclusionLists)
     {
         // <target name="init">
         //     <mkdir dir="classes"/>
         // </target>
         Element element = .createElement("target"); //$NON-NLS-1$
         element.setAttribute("name""init"); //$NON-NLS-1$ //$NON-NLS-2$
         List classDirsUnique = ExportUtil.removeDuplicates(classDirs);        
         for (Iterator iterator = classDirsUnique.iterator(); iterator.hasNext();)
         {            
             String classDir = (Stringiterator.next();
             if (!classDir.equals(".") && //$NON-NLS-1$
                 !EclipseClasspath.isReference(classDir))
             {
                 Element pathElement = .createElement("mkdir"); //$NON-NLS-1$
                 pathElement.setAttribute("dir"classDir); //$NON-NLS-1$
                 element.appendChild(pathElement);
             }
         }
         .appendChild(element);
         
         createCopyResources(srcDirsclassDirselementinclusionLists,
             exclusionLists);
     }
 
     private void createCopyResources(List srcDirsList classDirsElement element,
         List inclusionListsList exclusionLists)
     {
         // Check filter for copying resources
         StringTokenizer tokenizer = new StringTokenizer(filter","); //$NON-NLS-1$
         List filters = Collections.list(tokenizer);
         filters.add("*.java"); //$NON-NLS-1$
         
         // prefix filters with wild card
         for (int i = 0; i < filters.size(); i++)
         {
             String item = ((Stringfilters.get(i)).trim();
             if (item.equals("*")) //$NON-NLS-1$
             {
                 // everything is excluded from copying
                 return;
             }            
             filters.set(i"**/" + item); //$NON-NLS-1$
         }
         
         // <copy todir="classes" includeemptydirs="false">
         //     <fileset dir="src" excludes="**/*.java"/>
         // </copy>
         for (int i = 0; i < srcDirs.size(); i++)
         {
             String srcDir = (StringsrcDirs.get(i);
             String classDir = (StringclassDirs.get(i);
             if (! EclipseClasspath.isReference(classDir))
             {
                 Element copyElement = .createElement("copy"); //$NON-NLS-1$
                 copyElement.setAttribute("todir"classDir); //$NON-NLS-1$
                 copyElement.setAttribute("includeemptydirs""false"); //$NON-NLS-1$ //$NON-NLS-2$
                 Element filesetElement = .createElement("fileset"); //$NON-NLS-1$
                 filesetElement.setAttribute("dir"srcDir); //$NON-NLS-1$
 
                 List inclusions = (ListinclusionLists.get(i);
                 List exclusions = (ListexclusionLists.get(i);
 
                 for (Iterator iter = inclusions.iterator(); iter.hasNext();)
                 {
                     String inclusion = (Stringiter.next();
                     Element includeElement = .createElement("include"); //$NON-NLS-1$
                     includeElement.setAttribute("name"inclusion); //$NON-NLS-1$
                     filesetElement.appendChild(includeElement);
                 }           
                 for (Iterator iter = filters.iterator(); iter.hasNext();)
                 {
                     String exclusion = (Stringiter.next();
                     Element excludeElement = .createElement("exclude"); //$NON-NLS-1$
                     excludeElement.setAttribute("name"exclusion); //$NON-NLS-1$
                     filesetElement.appendChild(excludeElement);
                 }
                 for (Iterator iter = exclusions.iterator(); iter.hasNext();)
                 {
                     String exclusion = (Stringiter.next();
                     Element excludeElement = .createElement("exclude"); //$NON-NLS-1$
                     excludeElement.setAttribute("name"exclusion); //$NON-NLS-1$
                     filesetElement.appendChild(excludeElement);
                 }
                 
                 copyElement.appendChild(filesetElement);
                 element.appendChild(copyElement);
             }
         }
     }

    
Create clean target.

Parameters:
classDirs classes directories to delete
 
     public void createClean(List classDirs)
     {
         // <target name="clean">
         //     <delete dir="classes"/>
         // </target>
         Element element = .createElement("target"); //$NON-NLS-1$
         element.setAttribute("name""clean"); //$NON-NLS-1$ //$NON-NLS-2$
         List classDirUnique = ExportUtil.removeDuplicates(classDirs);
         for (Iterator iterator = classDirUnique.iterator(); iterator.hasNext();)
         {
             String classDir = (Stringiterator.next();
             if (!classDir.equals(".") && //$NON-NLS-1$
                 !EclipseClasspath.isReference(classDir))
             {
                 Element deleteElement = .createElement("delete"); //$NON-NLS-1$
                 deleteElement.setAttribute("dir"classDir); //$NON-NLS-1$
                 element.appendChild(deleteElement);
             }
         }
         .appendChild(element);    
 
         // <target name="clean">
         //     <delete>
         //         <fileset dir="." includes="**/*.class"/>
         //     </delete>
         // </target>
         if (classDirs.contains(".")) //$NON-NLS-1$
         {
             Element deleteElement = .createElement("delete"); //$NON-NLS-1$
             Element filesetElement = .createElement("fileset"); //$NON-NLS-1$
             filesetElement.setAttribute("dir""."); //$NON-NLS-1$ //$NON-NLS-2$
             filesetElement.setAttribute("includes""**/*.class"); //$NON-NLS-1$ //$NON-NLS-2$
             deleteElement.appendChild(filesetElement);           
             element.appendChild(deleteElement);           
         }
     }
    
    
Create clean all target.
 
     public void createCleanAll() throws JavaModelException
     {
         // <target name="cleanall" depends="clean">
         //     <ant antfile="${hello.location}/build.xml" dir="${hello.location}" inheritAll="false" target="clean"/>
         // </target>
         Element element = .createElement("target"); //$NON-NLS-1$
         element.setAttribute("name""cleanall"); //$NON-NLS-1$ //$NON-NLS-2$
         element.setAttribute("depends""clean"); //$NON-NLS-1$ //$NON-NLS-2$
         List subProjects = ExportUtil.getClasspathProjectsRecursive();
         for (Iterator iterator = subProjects.iterator(); iterator.hasNext();)
         {
             IJavaProject subProject = (IJavaProjectiterator.next();
             Element antElement = .createElement("ant"); //$NON-NLS-1$
             antElement.setAttribute("antfile""${" + subProject.getProject().getName() + ".location}/" + ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
             antElement.setAttribute("dir""${" + subProject.getProject().getName() + ".location}"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
             antElement.setAttribute("target""clean");  //$NON-NLS-1$ //$NON-NLS-2$
             antElement.setAttribute("inheritAll""false");  //$NON-NLS-1$ //$NON-NLS-2$
             element.appendChild(antElement);
         }
         .appendChild(element);
     }

    
Create build target.

Parameters:
srcDirs source directories of main project
classDirs class directories of main project
inclusionLists inclusion filters of main project
exclusionLists exclusion filters of main project
 
     public void createBuild(List srcDirsList classDirsList inclusionListsList exclusionListsthrows JavaModelException
     {
         // <target name="build" depends="build-subprojects,build-project"/>
         Element element = .createElement("target"); //$NON-NLS-1$
         element.setAttribute("name""build"); //$NON-NLS-1$ //$NON-NLS-2$
         element.setAttribute("depends""build-subprojects,build-project"); //$NON-NLS-1$ //$NON-NLS-2$
         .appendChild(element);
         
         // <target name="build-subprojects">
         //     <ant antfile="${hello.location}/build.xml" dir="${hello.location}" inheritAll="false" target="build-project"/>
         // </target>
         element = .createElement("target"); //$NON-NLS-1$
         element.setAttribute("name""build-subprojects"); //$NON-NLS-1$ //$NON-NLS-2$
         List subProjects = ExportUtil.getClasspathProjectsRecursive();
         for (Iterator iterator = subProjects.iterator(); iterator.hasNext();)
         {
             IJavaProject subProject = (IJavaProjectiterator.next();
             Element antElement = .createElement("ant"); //$NON-NLS-1$
             antElement.setAttribute("antfile""${" + subProject.getProject().getName() + ".location}/" + ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
             antElement.setAttribute("dir""${" + subProject.getProject().getName() + ".location}"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
             antElement.setAttribute("target""build-project");  //$NON-NLS-1$ //$NON-NLS-2$
             antElement.setAttribute("inheritAll""false");  //$NON-NLS-1$ //$NON-NLS-2$
             if () {
                 Element propertysetElement = .createElement("propertyset"); //$NON-NLS-1$
                 Element propertyrefElement = .createElement("propertyref"); //$NON-NLS-1$
                 propertyrefElement.setAttribute("name""build.compiler");  //$NON-NLS-1$ //$NON-NLS-2$
                 propertysetElement.appendChild(propertyrefElement);
                 antElement.appendChild(propertysetElement);
             }
             element.appendChild(antElement);           
         }
         .appendChild(element);
         
         // <target name="build-project" depends="init">
         //     <echo message="${ant.project.name}: ${ant.file}"/>
         //     <javac destdir="classes">
         //         <src path="src"/>
         //         <include name=""/>
         //         <exclude name=""/>
         //         <classpath refid="project.classpath"/>
         //     </javac>    
         // </target>        
         element = .createElement("target"); //$NON-NLS-1$
         element.setAttribute("name""build-project"); //$NON-NLS-1$ //$NON-NLS-2$
         element.setAttribute("depends""init"); //$NON-NLS-1$ //$NON-NLS-2$
         Element echoElement = .createElement("echo"); //$NON-NLS-1$
         echoElement.setAttribute("message""${ant.project.name}: ${ant.file}"); //$NON-NLS-1$ //$NON-NLS-2$
         element.appendChild(echoElement);           
         for (int i = 0; i < srcDirs.size(); i++)
         {
             String srcDir = (StringsrcDirs.get(i);
             if (!EclipseClasspath.isReference(srcDir))
             {
                 String classDir = (StringclassDirs.get(i);
                 List inclusions = (ListinclusionLists.get(i);
                 List exclusions = (ListexclusionLists.get(i);
                 Element javacElement = .createElement("javac"); //$NON-NLS-1$
                 javacElement.setAttribute("destdir"classDir); //$NON-NLS-1$
                 javacElement.setAttribute("debug""true"); //$NON-NLS-1$ //$NON-NLS-2$
                 javacElement.setAttribute("debuglevel""${debuglevel}"); //$NON-NLS-1$ //$NON-NLS-2$
                 javacElement.setAttribute("source""${source}"); //$NON-NLS-1$ //$NON-NLS-2$
                 javacElement.setAttribute("target""${target}"); //$NON-NLS-1$ //$NON-NLS-2$
     
                 Element srcElement = .createElement("src"); //$NON-NLS-1$
                 srcElement.setAttribute("path"srcDir); //$NON-NLS-1$
                 javacElement.appendChild(srcElement);            
     
                 for (Iterator iter = inclusions.iterator(); iter.hasNext();)
                 {
                     String inclusion = (Stringiter.next();
                     Element includeElement = .createElement("include"); //$NON-NLS-1$
                     includeElement.setAttribute("name"inclusion); //$NON-NLS-1$
                     javacElement.appendChild(includeElement);
                 }           
                 for (Iterator iter = exclusions.iterator(); iter.hasNext();)
                 {
                     String exclusion = (Stringiter.next();
                     Element excludeElement = .createElement("exclude"); //$NON-NLS-1$
                     excludeElement.setAttribute("name"exclusion); //$NON-NLS-1$
                     javacElement.appendChild(excludeElement);
                 }           
                 Element classpathRefElement = .createElement("classpath"); //$NON-NLS-1$
                 classpathRefElement.setAttribute("refid" + ".classpath"); //$NON-NLS-1$ //$NON-NLS-2$
                 javacElement.appendChild(classpathRefElement);
                 element.appendChild(javacElement);
                 
                 addCompilerBootClasspath(srcDirsjavacElement);
             }
         }
         .appendChild(element);
     }
    
    
Create target build-refprojects which compiles projects which reference current project.
 
     private void createBuildRef() throws JavaModelException {
         
         Set refProjects = new TreeSet(ExportUtil.getJavaProjectComparator());
         IJavaProject[] projects = .getJavaModel().getJavaProjects();
         for (int i = 0; i < projects.lengthi++) {
             List subProjects = ExportUtil.getClasspathProjects(projects[i]);
             for (Iterator iter = subProjects.iterator(); iter.hasNext();) {
                 IJavaProject p = (IJavaProjectiter.next();
                 if (.equals(p.getProject().getName())) {
                     refProjects.add(projects[i]);
                 }
             }
         }
         
         // <target name="build-refprojects">
         //     <ant antfile="${hello.location}/build.xml" dir="${hello.location}" target="clean" inheritAll="false"/> 
         //     <ant antfile="${hello.location}/build.xml" dir="${hello.location}" target="build" inheritAll="false"/> 
         // </target>
         Element element = .createElement("target"); //$NON-NLS-1$
         element.setAttribute("name""build-refprojects"); //$NON-NLS-1$ //$NON-NLS-2$
         element.setAttribute("description""Build all projects which " + //$NON-NLS-1$ //$NON-NLS-2$
                 "reference this project. Useful to propagate changes."); //$NON-NLS-1$
         for (Iterator iter = refProjects.iterator(); iter.hasNext();) {
             IJavaProject p = (IJavaProjectiter.next();
             String location = p.getProject().getName() + ".location"//$NON-NLS-1$
             String refProjectRoot = ExportUtil.getProjectRoot(p);
             String relativePath = ExportUtil.getRelativePath(refProjectRoot,
                     );
             .put(locationrelativePath);
 
             Element antElement = .createElement("ant"); //$NON-NLS-1$
             antElement.setAttribute("antfile""${" + p.getProject().getName() + ".location}/" + ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
             antElement.setAttribute("dir""${" + p.getProject().getName() + ".location}"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
             antElement.setAttribute("target""clean"); //$NON-NLS-1$ //$NON-NLS-2$
             antElement.setAttribute("inheritAll""false"); //$NON-NLS-1$ //$NON-NLS-2$
             element.appendChild(antElement);
             
             antElement = .createElement("ant"); //$NON-NLS-1$
             antElement.setAttribute("antfile""${" + p.getProject().getName() + ".location}/" + ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
             antElement.setAttribute("dir""${" + p.getProject().getName() + ".location}"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
             antElement.setAttribute("target""build"); //$NON-NLS-1$ //$NON-NLS-2$
             antElement.setAttribute("inheritAll""false");  //$NON-NLS-1$ //$NON-NLS-2$
             if () {
                 Element propertysetElement = .createElement("propertyset"); //$NON-NLS-1$
                 Element propertyrefElement = .createElement("propertyref"); //$NON-NLS-1$
                 propertyrefElement.setAttribute("name""build.compiler");  //$NON-NLS-1$ //$NON-NLS-2$
                 propertysetElement.appendChild(propertyrefElement);
                 antElement.appendChild(propertysetElement);
             }
             element.appendChild(antElement);
         }
         .appendChild(element);
     }
    
    
Add target to initialize Eclipse compiler. It copies required jars to ant lib directory.
 
     public void addInitEclipseCompiler()
     {
         // use ECLIPSE_HOME classpath variable
         IPath value = JavaCore.getClasspathVariable("ECLIPSE_HOME"); //$NON-NLS-1$
         if (value != null) {
             .put("ECLIPSE_HOME", ExportUtil.getRelativePath//$NON-NLS-1$
                 value.toString(), ));
         }
         // <target name="init-eclipse-compiler" description="copy Eclipse compiler jars to ant lib directory">
         //     <property name="ECLIPSE_HOME" value="C:/Program/eclipse-3.1" />
         //     <copy todir="${ant.library.dir}">
         //         <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar" />
         //     </copy>
         //     <unzip dest="${ant.library.dir}">
         //         <patternset includes="jdtCompilerAdapter.jar" />
         //         <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar" />
         //     </unzip>
         // </target>
         Element element = .createElement("target"); //$NON-NLS-1$
         element.setAttribute("name""init-eclipse-compiler"); //$NON-NLS-1$ //$NON-NLS-2$
         element.setAttribute("description""copy Eclipse compiler jars to ant lib directory"); //$NON-NLS-1$ //$NON-NLS-2$
         Element copyElement = .createElement("copy"); //$NON-NLS-1$
         copyElement.setAttribute("todir""${ant.library.dir}"); //$NON-NLS-1$ //$NON-NLS-2$
         Element filesetElement = .createElement("fileset"); //$NON-NLS-1$
         filesetElement.setAttribute("dir""${ECLIPSE_HOME}/plugins"); //$NON-NLS-1$ //$NON-NLS-2$
         filesetElement.setAttribute("includes""org.eclipse.jdt.core_*.jar"); //$NON-NLS-1$ //$NON-NLS-2$
         copyElement.appendChild(filesetElement);
         element.appendChild(copyElement);
         Element unzipElement = .createElement("unzip"); //$NON-NLS-1$
         unzipElement.setAttribute("dest""${ant.library.dir}"); //$NON-NLS-1$ //$NON-NLS-2$
         Element patternsetElement = .createElement("patternset"); //$NON-NLS-1$
         patternsetElement.setAttribute("includes""jdtCompilerAdapter.jar"); //$NON-NLS-1$ //$NON-NLS-2$
         unzipElement.appendChild(patternsetElement);
         unzipElement.appendChild(filesetElement.cloneNode(false));
         element.appendChild(unzipElement);
         .appendChild(element);
     }

    
Add target to compile project using Eclipse compiler.
 
     public void addBuildEclipse()
     {
         // <target name="build-eclipse-compiler" description="compile project with Eclipse compiler">
         //     <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter" />
         //     <antcall target="build" />
         // </target>
         Element element = .createElement("target"); //$NON-NLS-1$
         element.setAttribute("name""build-eclipse-compiler"); //$NON-NLS-1$ //$NON-NLS-2$
         element.setAttribute("description""compile project with Eclipse compiler"); //$NON-NLS-1$ //$NON-NLS-2$
         Element propertyElement = .createElement("property"); //$NON-NLS-1$
         propertyElement.setAttribute("name""build.compiler"); //$NON-NLS-1$ //$NON-NLS-2$
         propertyElement.setAttribute("value""org.eclipse.jdt.core.JDTCompilerAdapter"); //$NON-NLS-1$ //$NON-NLS-2$
         element.appendChild(propertyElement);
         Element antcallElement = .createElement("antcall"); //$NON-NLS-1$
         antcallElement.setAttribute("target""build"); //$NON-NLS-1$ //$NON-NLS-2$
         element.appendChild(antcallElement);
         .appendChild(element);
     }

    
Add all boot classpaths in srcDirs to given javacElement.
 
     private void addCompilerBootClasspath(List srcDirsElement javacElement)
     {
         // <bootclasspath>
         //     <path refid="mylib.bootclasspath"/>
         //     <fileset dir="${java.home}/lib" includes="*.jar"/>
         //     <fileset dir="${java.home}/lib/ext" includes="*.jar"/>
         // </bootclasspath>
         Element bootclasspathElement = .createElement("bootclasspath"); //$NON-NLS-1$
         boolean bootclasspathUsed = false;
         for (Iterator iter = srcDirs.iterator(); iter.hasNext();)
         {
             String entry = (Stringiter.next();
             if (EclipseClasspath.isUserSystemLibraryReference(entry))
             {
                 Element pathElement = .createElement("path"); //$NON-NLS-1$                        
                 pathElement.setAttribute("refid", ExportUtil.removePrefixAndSuffix(entry"${""}")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                 bootclasspathElement.appendChild(pathElement);
                 bootclasspathUsed = true;
             } else if (EclipseClasspath.isJreReference(entry)) {
                 addJre(bootclasspathElement);
             }
         }
         if (bootclasspathUsed)
         {
             javacElement.appendChild(bootclasspathElement);
         }
     }

    
Add run targets.

Throws:
CoreException thrown if problem accessing the launch configuration
TransformerFactoryConfigurationError thrown if applet file could not get created
UnsupportedEncodingException thrown if applet file could not get created
 
     {
         // <target name="run">
         //     <java fork="yes" classname="class" failonerror="true" dir="." newenvironment="true">
         //         <env key="a" value="b"/>
         //         <jvmarg value="-Dx=y"/>
         //         <arg value="arg"/>
         //         <classpath refid="project.classpath"/>
         //     </java>
         // </target>
         ILaunchConfiguration[] confs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations();
         boolean junitUsed = false;
         for (int i = 0; i < confs.lengthi++)
         {
             ILaunchConfiguration conf = confs[i];
             if (!.equals(conf.getAttribute(.""))) //$NON-NLS-1$
             {
                 continue;
             }
                     
             {
                 addJavaApplication(conf);
             }
             else if (conf.getType().getIdentifier().equals(.))
             {
                 addApplet(conf);
             }
             else if (conf.getType().getIdentifier().equals("org.eclipse.jdt.junit.launchconfig" /*JUnitLaunchConfiguration.ID_JUNIT_APPLICATION*/)) //$NON-NLS-1$
             {                    
                 addJUnit(conf);
                 junitUsed = true;
             }           
         }
         
         if (junitUsed)
         {
             addJUnitReport();
         }
     }

    
Convert Java application launch configuration to ant target and add it to a document.

Parameters:
variable2value adds Eclipse variables to this map, if run configuration makes use of this feature
conf Java application launch configuration
    public void addJavaApplication(Map variable2valueILaunchConfiguration confthrows CoreException
    {
        Element element = .createElement("target"); //$NON-NLS-1$
        element.setAttribute("name"conf.getName()); //$NON-NLS-1$
        Element javaElement = .createElement("java"); //$NON-NLS-1$
        javaElement.setAttribute("fork""yes"); //$NON-NLS-1$ //$NON-NLS-2$
        javaElement.setAttribute("classname"conf.getAttribute(."")); //$NON-NLS-1$ //$NON-NLS-2$
        javaElement.setAttribute("failonerror""true"); //$NON-NLS-1$ //$NON-NLS-2$
        String dir = conf.getAttribute(.""); //$NON-NLS-1$
        ExportUtil.addVariable(variable2valuedir);                
        if (!dir.equals("")) //$NON-NLS-1$
        {
            javaElement.setAttribute("dir", ExportUtil.getRelativePath(dir)); //$NON-NLS-1$
        }
        {
            javaElement.setAttribute("newenvironment""true"); //$NON-NLS-1$ //$NON-NLS-2$
        }
        Map props = conf.getAttribute(.new TreeMap());
        addElements(propsjavaElement"env""key""value"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        addElement(conf.getAttribute(.""), javaElement"jvmarg""line"variable2value); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        addElement(conf.getAttribute(.""), javaElement"arg""line"variable2value); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        element.appendChild(javaElement);
        
        addRuntimeClasspath(confjavaElement);
        addRuntimeBootClasspath(confjavaElement);
        .appendChild(element);
    }