Changeset 4170

Show
Ignore:
Timestamp:
03/01/09 20:51:40 (3 years ago)
Author:
michael
Message:

Added support for plugin projects

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/org/sarugo/ant/eclipse/EclipseTask.java

    r2794 r4170  
    66import java.io.Writer; 
    77import java.util.ArrayList; 
    8 import java.util.Iterator; 
    98import java.util.List; 
    109 
     
    1817/* 
    1918 * EclipseTask 
    20  * Copyright 2007 Sarugo Pty. Ltd. 
     19 * Copyright 2007, 2008 Sarugo Pty Ltd 
    2120 * http://www.sarugo.net 
    2221 * based on EclipseClasspath 
     
    4443 *  
    4544 * Usage: 
     45 *  
    4646 * <pre> 
    47  * <taskdef name="eclipse" classname="org.sarugo.ant.eclipse.EclipseTask" 
    48  * classpath="classes"/> 
    49  *  
    50  * <eclipse todir="." defaultBuildDir="bin"> 
    51  *  <sourcepath refid="project.src.path"/> 
    52  *  <globmapper from="src/*" to="build/*"/> 
    53  *  <classpath refid="project.class.path"/> 
    54  * </eclipse> 
     47 * &lt;taskdef name=&quot;eclipse&quot; classname=&quot;org.sarugo.ant.eclipse.EclipseTask&quot; 
     48 * classpath=&quot;classes&quot;/&gt; 
     49 *  
     50 * &lt;eclipse todir=&quot;.&quot; defaultBuildDir=&quot;bin&quot; plugin=&quot;true&quot;&gt; 
     51 *  &lt;sourcepath refid=&quot;project.src.path&quot;/&gt; 
     52 *  &lt;globmapper from=&quot;src/*&quot; to=&quot;build/*&quot;/&gt; 
     53 *  &lt;classpath refid=&quot;project.class.path&quot;/&gt; 
     54 *  &lt;exported refid=&quot;project.exports&quot;/&gt; 
     55 * &lt;/eclipse&gt; 
    5556 * </pre> 
    5657 */ 
    5758public class EclipseTask extends Task { 
    58         private List _sourceList = new ArrayList(); 
    59  
    60         private List _classpathList = new ArrayList(); 
    61  
    62         private File _todir; 
    63  
    64         private FileNameMapper _outputDirMapper; 
    65  
    66         private String _defaultBuildDir = "bin"; 
    67  
    68         // The method executing the task 
    69         public void execute() throws BuildException { 
    70                 if (_sourceList.isEmpty()) { 
    71                         throw new BuildException("classpath not set."); 
    72                 } 
    73                 if (_classpathList.isEmpty()) { 
    74                         throw new BuildException("classpath not set."); 
    75                 } 
    76                 if (_outputDirMapper == null) { 
    77                         _outputDirMapper = new IdentityMapper(); 
    78                 } 
    79                 if (_todir == null) { 
    80                         _todir = getProject().getBaseDir(); 
    81                 } 
    82                 log("Outputting to " + _todir); 
    83  
    84                 // Now copy the template file to the output file and 
    85                 // use the filter set for replacement 
    86                 FileUtils fu = FileUtils.newFileUtils(); 
    87                 try { 
    88                         // generate .project 
    89                         File projectFile = new File(_todir, ".project"); 
    90                         fu.createNewFile(projectFile, true); 
    91                         Writer projectDescription = new FileWriter(projectFile, false); 
    92                         projectDescription 
    93                                         .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 
    94                         projectDescription.append("<projectDescription>\n"); 
    95                         projectDescription.append(" <name>"); 
    96                         projectDescription.append(getProject().getName()); 
    97                         projectDescription.append("</name>\n"); 
    98                         projectDescription.append(" <comment></comment>\n"); 
    99                         projectDescription.append(" <projects>\n"); 
    100                         projectDescription.append(" </projects>\n"); 
    101                         projectDescription.append(" <buildSpec>\n"); 
    102                         projectDescription.append("  <buildCommand>\n"); 
    103                         projectDescription 
    104                                         .append("   <name>org.eclipse.jdt.core.javabuilder</name>\n"); 
    105                         projectDescription.append("    <arguments>\n"); 
    106                         projectDescription.append("    </arguments>\n"); 
    107                         projectDescription.append("   </buildCommand>\n"); 
    108                         projectDescription.append(" </buildSpec>\n"); 
    109                         projectDescription.append(" <natures>\n"); 
    110                         projectDescription 
    111                                         .append("  <nature>org.eclipse.jdt.core.javanature</nature>\n"); 
    112                         projectDescription.append(" </natures>\n"); 
    113                         projectDescription.append("</projectDescription>\n"); 
    114                         projectDescription.close(); 
    115  
    116                         // generate .classpath 
    117                         File baseDir = getProject().getBaseDir(); 
    118                         File classpathFile = new File(_todir, ".classpath"); 
    119                         fu.createNewFile(classpathFile, true); 
    120                         Writer classpath = new FileWriter(classpathFile, false); 
    121                         classpath.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 
    122                         classpath.append("<classpath>\n"); 
    123                         for (Iterator i = _sourceList.iterator(); i.hasNext();) { 
    124                                 Path p = (Path) i.next(); 
    125                                 String[] paths = p.list(); 
    126                                 for (int j = 0; j < paths.length; j++) { 
    127                                         String path = fu.removeLeadingPath(baseDir, fu.normalize(paths[j])); 
    128                                         String[] translatedPaths = _outputDirMapper 
    129                                                         .mapFileName(path); 
    130                                         if (translatedPaths.length != 1) { 
    131                                                 throw new BuildException( 
    132                                                                 "Output directory mapper must result in only one mapping per entry (" 
    133                                                                                 + translatedPaths.length + " found)"); 
    134                                         } 
    135                                         log("Mapping source folder \"" + path + "\" to output folder \"" + translatedPaths[0] + "\""); 
    136                                         classpath.append(" <classpathentry kind=\"src\" output=\""); 
    137                                         classpath.append(translatedPaths[0]); 
    138                                         classpath.append("\" path=\""); 
    139                                         classpath.append(path); 
    140                                         classpath.append("\"/>\n"); 
    141                                 } 
    142                         } 
    143                         classpath 
    144                                         .append(" <classpathentry kind=\"con\" path=\"org.eclipse.jdt.launching.JRE_CONTAINER\"/>\n"); 
    145                         for (Iterator i = _classpathList.iterator(); i.hasNext();) { 
    146                                 Path p = (Path) i.next(); 
    147                                 String[] paths = p.list(); 
    148                                 for (int j = 0; j < paths.length; j++) { 
    149                                         String path = fu.removeLeadingPath(baseDir, fu.normalize(paths[j])); 
    150                                         classpath.append(" <classpathentry kind=\"lib\" path=\""); 
    151                                         classpath.append(path); 
    152                                         classpath.append("\"/>\n"); 
    153                                 } 
    154                         } 
    155                         classpath.append(" <classpathentry kind=\"output\" path=\""); 
    156                         classpath.append(_defaultBuildDir); 
    157                         classpath.append("\"/>\n"); 
    158                         classpath.append("</classpath>\n"); 
    159                         classpath.close(); 
    160  
    161                 } catch (IOException e) { 
    162                         throw new BuildException(e); 
    163                 } 
    164  
    165         } 
    166  
    167         public void addClasspath(Path classpath) { 
    168                 if (classpath != null) { 
    169                         _classpathList.add(classpath); 
    170                 } 
    171         } 
    172  
    173         public void addSourcepath(Path sourcepath) { 
    174                 if (sourcepath != null) { 
    175                         _sourceList.add(sourcepath); 
    176                 } 
    177         } 
    178  
    179         public void add(FileNameMapper mapper) { 
    180                 _outputDirMapper = mapper; 
    181         } 
    182  
    183         public void setTodir(File dir) { 
    184                 _todir = dir; 
    185         } 
    186          
    187         public void setDefaultBuildDir(String value) { 
    188                 _defaultBuildDir = value; 
    189         } 
     59    private List<Path> sourcepathList = new ArrayList<Path>(); 
     60 
     61    private List<Path> classpathList = new ArrayList<Path>(); 
     62 
     63    private List<Path> exportedList = new ArrayList<Path>(); 
     64 
     65    private File todir; 
     66 
     67    private FileNameMapper outputDirMapper; 
     68 
     69    private String defaultBuildDir = "bin"; 
     70 
     71    private boolean plugin = false; 
     72 
     73    // The method executing the task 
     74    public void execute() throws BuildException { 
     75        if (sourcepathList.isEmpty()) { 
     76            throw new BuildException("sourcepath not set."); 
     77        } 
     78        if (classpathList.isEmpty()) { 
     79            throw new BuildException("classpath not set."); 
     80        } 
     81        if (outputDirMapper == null) { 
     82            outputDirMapper = new IdentityMapper(); 
     83        } 
     84        if (todir == null) { 
     85            todir = getProject().getBaseDir(); 
     86        } 
     87        log("Outputting to " + todir); 
     88 
     89        // Now copy the template file to the output file and 
     90        // use the filter set for replacement 
     91        FileUtils fu = FileUtils.getFileUtils(); 
     92        try { 
     93            // generate .project 
     94            File projectFile = new File(todir, ".project"); 
     95            fu.createNewFile(projectFile, true); 
     96            Writer projectDescription = new FileWriter(projectFile, false); 
     97            projectDescription 
     98                    .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 
     99            projectDescription.append("<projectDescription>\n"); 
     100            projectDescription.append(" <name>"); 
     101            projectDescription.append(getProject().getName()); 
     102            projectDescription.append("</name>\n"); 
     103            projectDescription.append(" <comment></comment>\n"); 
     104            projectDescription.append(" <projects>\n"); 
     105            projectDescription.append(" </projects>\n"); 
     106            projectDescription.append(" <buildSpec>\n"); 
     107            projectDescription.append("  <buildCommand>\n"); 
     108            projectDescription 
     109                    .append("   <name>org.eclipse.jdt.core.javabuilder</name>\n"); 
     110            projectDescription.append("    <arguments>\n"); 
     111            projectDescription.append("    </arguments>\n"); 
     112            projectDescription.append("   </buildCommand>\n"); 
     113            if (plugin) { 
     114                projectDescription.append("  <buildCommand>\n"); 
     115                projectDescription 
     116                        .append("   <name>org.eclipse.pde.ManifestBuilder</name>\n"); 
     117                projectDescription.append("    <arguments>\n"); 
     118                projectDescription.append("    </arguments>\n"); 
     119                projectDescription.append("   </buildCommand>\n"); 
     120                projectDescription.append("  <buildCommand>\n"); 
     121                projectDescription 
     122                        .append("   <name>org.eclipse.pde.SchemaBuilder</name>\n"); 
     123                projectDescription.append("    <arguments>\n"); 
     124                projectDescription.append("    </arguments>\n"); 
     125                projectDescription.append("   </buildCommand>\n"); 
     126            } 
     127            projectDescription.append(" </buildSpec>\n"); 
     128            projectDescription.append(" <natures>\n"); 
     129            projectDescription 
     130                    .append("  <nature>org.eclipse.jdt.core.javanature</nature>\n"); 
     131            if (plugin) { 
     132                projectDescription 
     133                        .append("  <nature>org.eclipse.pde.PluginNature</nature>\n"); 
     134            } 
     135            projectDescription.append(" </natures>\n"); 
     136            projectDescription.append("</projectDescription>\n"); 
     137            projectDescription.close(); 
     138 
     139            // generate .classpath 
     140            File baseDir = getProject().getBaseDir(); 
     141            File classpathFile = new File(todir, ".classpath"); 
     142            fu.createNewFile(classpathFile, true); 
     143            Writer classpath = new FileWriter(classpathFile, false); 
     144            classpath.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 
     145            classpath.append("<classpath>\n"); 
     146            for (Path p : sourcepathList) { 
     147                String[] paths = p.list(); 
     148                for (int j = 0; j < paths.length; j++) { 
     149                    String path = fu.removeLeadingPath(baseDir, fu 
     150                            .normalize(paths[j])); 
     151                    String[] translatedPaths = outputDirMapper 
     152                            .mapFileName(path); 
     153                    if (translatedPaths.length != 1) { 
     154                        throw new BuildException( 
     155                                "Output directory mapper must result in only one mapping per entry (" 
     156                                        + translatedPaths.length + " found)"); 
     157                    } 
     158                    log("Mapping source folder \"" + path 
     159                            + "\" to output folder \"" + translatedPaths[0] 
     160                            + "\""); 
     161                    classpath.append(" <classpathentry kind=\"src\" output=\""); 
     162                    classpath.append(translatedPaths[0]); 
     163                    classpath.append("\" path=\""); 
     164                    classpath.append(path); 
     165                    classpath.append("\"/>\n"); 
     166                } 
     167            } 
     168            classpath 
     169                    .append(" <classpathentry kind=\"con\" path=\"org.eclipse.jdt.launching.JRE_CONTAINER\"/>\n"); 
     170            if (plugin) { 
     171                classpath 
     172                        .append(" <classpathentry kind=\"con\" path=\"org.eclipse.pde.core.requiredPlugins\"/>\n"); 
     173            } 
     174            for (Path p : classpathList) { 
     175                String[] paths = p.list(); 
     176                for (int j = 0; j < paths.length; j++) { 
     177                    String path = fu.removeLeadingPath(baseDir, fu 
     178                            .normalize(paths[j])); 
     179                    classpath.append(" <classpathentry kind=\"lib\" path=\""); 
     180                    classpath.append(path); 
     181                    classpath.append("\"/>\n"); 
     182                } 
     183            } 
     184            for (Path p : exportedList) { 
     185                String[] paths = p.list(); 
     186                for (int j = 0; j < paths.length; j++) { 
     187                    String path = fu.removeLeadingPath(baseDir, fu 
     188                            .normalize(paths[j])); 
     189                    classpath 
     190                            .append(" <classpathentry exported=\"true\" kind=\"lib\" path=\""); 
     191                    classpath.append(path); 
     192                    classpath.append("\"/>\n"); 
     193                } 
     194            } 
     195            classpath.append(" <classpathentry kind=\"output\" path=\""); 
     196            classpath.append(defaultBuildDir); 
     197            classpath.append("\"/>\n"); 
     198            classpath.append("</classpath>\n"); 
     199            classpath.close(); 
     200 
     201        } catch (IOException e) { 
     202            throw new BuildException(e); 
     203        } 
     204 
     205    } 
     206 
     207    public void addSourcepath(Path sourcepath) { 
     208        sourcepathList.add(sourcepath); 
     209    } 
     210 
     211    public void addClasspath(Path classpath) { 
     212        classpathList.add(classpath); 
     213    } 
     214 
     215    public void addExported(Path exported) { 
     216        exportedList.add(exported); 
     217    } 
     218 
     219    public void add(FileNameMapper mapper) { 
     220        outputDirMapper = mapper; 
     221    } 
     222 
     223    public void setTodir(File dir) { 
     224        todir = dir; 
     225    } 
     226 
     227    public void setDefaultBuildDir(String value) { 
     228        defaultBuildDir = value; 
     229    } 
     230 
     231    public void setPlugin(boolean value) { 
     232        plugin = value; 
     233    } 
    190234}