Coverage Report - org.apache.maven.plugin.eclipse.writers.EclipseProjectWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
EclipseProjectWriter
86% 
100% 
6.6
 
 1  
 /*
 2  
  * Licensed under the Apache License, Version 2.0 (the "License");
 3  
  * you may not use this file except in compliance with the License.
 4  
  * You may obtain a copy of the License at
 5  
  *
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  *
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 
 15  
 package org.apache.maven.plugin.eclipse.writers;
 16  
 
 17  
 import java.io.File;
 18  
 import java.io.FileOutputStream;
 19  
 import java.io.FileReader;
 20  
 import java.io.IOException;
 21  
 import java.io.OutputStreamWriter;
 22  
 import java.io.Writer;
 23  
 import java.util.Iterator;
 24  
 import java.util.LinkedHashSet;
 25  
 import java.util.List;
 26  
 import java.util.Set;
 27  
 
 28  
 import org.apache.maven.model.Resource;
 29  
 import org.apache.maven.plugin.MojoExecutionException;
 30  
 import org.apache.maven.plugin.eclipse.BuildCommand;
 31  
 import org.apache.maven.plugin.eclipse.Messages;
 32  
 import org.apache.maven.plugin.ide.IdeDependency;
 33  
 import org.apache.maven.plugin.ide.IdeUtils;
 34  
 import org.codehaus.plexus.util.IOUtil;
 35  
 import org.codehaus.plexus.util.StringUtils;
 36  
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 37  
 import org.codehaus.plexus.util.xml.XMLWriter;
 38  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 39  
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 40  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 41  
 
 42  
 /**
 43  
  * Writes eclipse .project file.
 44  
  *
 45  
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 46  
  * @author <a href="mailto:kenney@neonics.com">Kenney Westerhof</a>
 47  
  * @author <a href="mailto:fgiust@apache.org">Fabrizio Giustina</a>
 48  
  * @version $Id: EclipseProjectWriter.java 472172 2006-11-07 17:09:41Z kenney $
 49  
  */
 50  12
 public class EclipseProjectWriter
 51  
     extends AbstractEclipseWriter
 52  
 {
 53  
     private static final String ELT_NAME = "name"; //$NON-NLS-1$
 54  
 
 55  
     private static final String ELT_BUILD_COMMAND = "buildCommand"; //$NON-NLS-1$
 56  
 
 57  
     private static final String ELT_BUILD_SPEC = "buildSpec"; //$NON-NLS-1$
 58  
 
 59  
     private static final String ELT_NATURE = "nature"; //$NON-NLS-1$
 60  
 
 61  
     private static final String ELT_NATURES = "natures"; //$NON-NLS-1$
 62  
 
 63  
     private static final String FILE_DOT_PROJECT = ".project"; //$NON-NLS-1$
 64  
 
 65  
     /**
 66  
      * Constant for links to files.
 67  
      */
 68  
     private static final int LINK_TYPE_FILE = 1;
 69  
 
 70  
     /**
 71  
      * Constant for links to directories.
 72  
      */
 73  
     private static final int LINK_TYPE_DIRECTORY = 2;
 74  
 
 75  
     /**
 76  
      * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
 77  
      */
 78  
     public void write()
 79  
         throws MojoExecutionException
 80  
     {
 81  
 
 82  12
         Set projectnatures = new LinkedHashSet();
 83  12
         Set buildCommands = new LinkedHashSet();
 84  
 
 85  12
         File dotProject = new File( config.getEclipseProjectDirectory(), FILE_DOT_PROJECT );
 86  
 
 87  12
         if ( dotProject.exists() )
 88  
         {
 89  
 
 90  1
             log.info( Messages.getString( "EclipsePlugin.keepexisting", dotProject.getAbsolutePath() ) ); //$NON-NLS-1$
 91  
 
 92  
             // parse existing file in order to keep manually-added entries
 93  1
             FileReader reader = null;
 94  
             try
 95  
             {
 96  1
                 reader = new FileReader( dotProject );
 97  1
                 Xpp3Dom dom = Xpp3DomBuilder.build( reader );
 98  
 
 99  1
                 Xpp3Dom naturesElement = dom.getChild( ELT_NATURES );
 100  1
                 if ( naturesElement != null )
 101  
                 {
 102  1
                     Xpp3Dom[] existingNatures = naturesElement.getChildren( ELT_NATURE );
 103  2
                     for ( int j = 0; j < existingNatures.length; j++ )
 104  
                     {
 105  
                         // adds all the existing natures
 106  1
                         projectnatures.add( existingNatures[j].getValue() );
 107  
                     }
 108  
                 }
 109  
 
 110  1
                 Xpp3Dom buildSpec = dom.getChild( ELT_BUILD_SPEC );
 111  1
                 if ( buildSpec != null )
 112  
                 {
 113  1
                     Xpp3Dom[] existingBuildCommands = buildSpec.getChildren( ELT_BUILD_COMMAND );
 114  2
                     for ( int j = 0; j < existingBuildCommands.length; j++ )
 115  
                     {
 116  1
                         Xpp3Dom buildCommandName = existingBuildCommands[j].getChild( ELT_NAME );
 117  1
                         if ( buildCommandName != null )
 118  
                         {
 119  1
                             buildCommands.add( new BuildCommand( existingBuildCommands[j] ) );
 120  
                         }
 121  
                     }
 122  
                 }
 123  
             }
 124  0
             catch ( XmlPullParserException e )
 125  
             {
 126  0
                 log.warn( Messages.getString( "EclipsePlugin.cantparseexisting", dotProject.getAbsolutePath() ) ); //$NON-NLS-1$
 127  
             }
 128  0
             catch ( IOException e )
 129  
             {
 130  0
                 log.warn( Messages.getString( "EclipsePlugin.cantparseexisting", dotProject.getAbsolutePath() ) ); //$NON-NLS-1$
 131  
             }
 132  
             finally
 133  
             {
 134  1
                 IOUtil.close( reader );
 135  1
             }
 136  
         }
 137  
 
 138  
         // adds new entries after the existing ones
 139  12
         for ( Iterator iter = config.getProjectnatures().iterator(); iter.hasNext(); )
 140  
         {
 141  22
             projectnatures.add( iter.next() );
 142  22
         }
 143  
 
 144  12
         for ( Iterator iter = config.getBuildCommands().iterator(); iter.hasNext(); )
 145  
         {
 146  25
             buildCommands.add( iter.next() );
 147  25
         }
 148  
 
 149  
         Writer w;
 150  
 
 151  
         try
 152  
         {
 153  12
             w = new OutputStreamWriter( new FileOutputStream( dotProject ), "UTF-8" );
 154  
         }
 155  0
         catch ( IOException ex )
 156  
         {
 157  0
             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
 158  12
         }
 159  
 
 160  12
         XMLWriter writer = new PrettyPrintXMLWriter( w );
 161  
 
 162  12
         writer.startElement( "projectDescription" ); //$NON-NLS-1$
 163  
 
 164  12
         writer.startElement( ELT_NAME );
 165  12
         writer.writeText( config.getEclipseProjectName() );
 166  12
         writer.endElement();
 167  
 
 168  
         // TODO: this entire element might be dropped if the comment is null.
 169  
         // but as the maven1 eclipse plugin does it, it's better to be safe than sorry
 170  
         // A eclipse developer might want to look at this.
 171  12
         writer.startElement( "comment" ); //$NON-NLS-1$
 172  
 
 173  12
         if ( config.getProject().getDescription() != null )
 174  
         {
 175  0
             writer.writeText( config.getProject().getDescription() );
 176  
         }
 177  
 
 178  12
         writer.endElement();
 179  
 
 180  12
         writer.startElement( "projects" ); //$NON-NLS-1$
 181  
 
 182  
         // referenced projects should not be added for plugins
 183  12
         if ( !config.isPde() )
 184  
         {
 185  11
             for ( int j = 0; j < config.getDeps().length; j++ )
 186  
             {
 187  3
                 IdeDependency dep = config.getDeps()[j];
 188  3
                 if ( dep.isReferencedProject() )
 189  
                 {
 190  0
                     writer.startElement( "project" ); //$NON-NLS-1$
 191  0
                     writer.writeText( dep.getArtifactId() );
 192  0
                     writer.endElement();
 193  
                 }
 194  
             }
 195  
         }
 196  
 
 197  12
         writer.endElement(); // projects
 198  
 
 199  12
         writer.startElement( ELT_BUILD_SPEC );
 200  
 
 201  12
         for ( Iterator it = buildCommands.iterator(); it.hasNext(); )
 202  
         {
 203  26
             ( (BuildCommand) it.next() ).print( writer );
 204  26
         }
 205  
 
 206  12
         writer.endElement(); // buildSpec
 207  
 
 208  12
         writer.startElement( ELT_NATURES );
 209  
 
 210  12
         for ( Iterator it = projectnatures.iterator(); it.hasNext(); )
 211  
         {
 212  22
             writer.startElement( ELT_NATURE );
 213  22
             writer.writeText( (String) it.next() );
 214  22
             writer.endElement(); // name
 215  22
         }
 216  
 
 217  12
         writer.endElement(); // natures
 218  
 
 219  12
         boolean addLinks = !config.getProjectBaseDir().equals( config.getEclipseProjectDirectory() );
 220  
 
 221  12
         if ( addLinks || ( config.isPde() && config.getDeps().length > 0 ) )
 222  
         {
 223  2
             writer.startElement( "linkedResources" ); //$NON-NLS-1$
 224  
 
 225  2
             if ( addLinks )
 226  
             {
 227  
 
 228  1
                 addFileLink( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(), config
 229  
                     .getProject().getFile() );
 230  
 
 231  1
                 addSourceLinks( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(), config
 232  
                     .getProject().getCompileSourceRoots() );
 233  1
                 addResourceLinks( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(), config
 234  
                     .getProject().getBuild().getResources() );
 235  
 
 236  1
                 addSourceLinks( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(), config
 237  
                     .getProject().getTestCompileSourceRoots() );
 238  1
                 addResourceLinks( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(), config
 239  
                     .getProject().getBuild().getTestResources() );
 240  
 
 241  
             }
 242  
 
 243  2
             if ( config.isPde() )
 244  
             {
 245  2
                 for ( int j = 0; j < config.getDeps().length; j++ )
 246  
                 {
 247  1
                     IdeDependency dep = config.getDeps()[j];
 248  
 
 249  1
                     if ( dep.isAddedToClasspath() && !dep.isProvided() && !dep.isReferencedProject()
 250  
                         && !dep.isTestDependency() && !dep.isOsgiBundle() )
 251  
                     {
 252  1
                         String name = dep.getFile().getName();
 253  1
                         addLink( writer, name, StringUtils.replace( IdeUtils.getCanonicalPath( dep.getFile() ), "\\",
 254  
                                                                     "/" ), LINK_TYPE_FILE );
 255  
                     }
 256  
                 }
 257  
             }
 258  
 
 259  2
             writer.endElement(); // linkedResources
 260  
         }
 261  
 
 262  12
         writer.endElement(); // projectDescription
 263  
 
 264  12
         IOUtil.close( w );
 265  12
     }
 266  
 
 267  
     private void addFileLink( XMLWriter writer, File projectBaseDir, File basedir, File file )
 268  
         throws MojoExecutionException
 269  
     {
 270  1
         if ( file.isFile() )
 271  
         {
 272  1
             String name = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, file, true );
 273  1
             String location = IdeUtils.getCanonicalPath( file ).replaceAll( "\\\\", "/" ); //$NON-NLS-1$ //$NON-NLS-2$
 274  
 
 275  1
             addLink( writer, name, location, LINK_TYPE_FILE );
 276  1
         }
 277  
         else
 278  
         {
 279  0
             log.warn( Messages.getString( "EclipseProjectWriter.notafile", file ) ); //$NON-NLS-1$
 280  
         }
 281  1
     }
 282  
 
 283  
     private void addSourceLinks( XMLWriter writer, File projectBaseDir, File basedir, List sourceRoots )
 284  
         throws MojoExecutionException
 285  
     {
 286  2
         for ( Iterator it = sourceRoots.iterator(); it.hasNext(); )
 287  
         {
 288  2
             String sourceRootString = (String) it.next();
 289  2
             File sourceRoot = new File( sourceRootString );
 290  
 
 291  2
             if ( sourceRoot.isDirectory() )
 292  
             {
 293  0
                 String name = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, sourceRoot, true );
 294  0
                 String location = IdeUtils.getCanonicalPath( sourceRoot ).replaceAll( "\\\\", "/" ); //$NON-NLS-1$ //$NON-NLS-2$
 295  
 
 296  0
                 addLink( writer, name, location, LINK_TYPE_DIRECTORY );
 297  
             }
 298  2
         }
 299  2
     }
 300  
 
 301  
     private void addResourceLinks( XMLWriter writer, File projectBaseDir, File basedir, List sourceRoots )
 302  
         throws MojoExecutionException
 303  
     {
 304  2
         for ( Iterator it = sourceRoots.iterator(); it.hasNext(); )
 305  
         {
 306  2
             String resourceDirString = ( (Resource) it.next() ).getDirectory();
 307  2
             File resourceDir = new File( resourceDirString );
 308  
 
 309  2
             if ( resourceDir.isDirectory() )
 310  
             {
 311  0
                 String name = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, resourceDir, true );
 312  0
                 String location = IdeUtils.getCanonicalPath( resourceDir ).replaceAll( "\\\\", "/" ); //$NON-NLS-1$ //$NON-NLS-2$
 313  
 
 314  0
                 addLink( writer, name, location, LINK_TYPE_DIRECTORY );
 315  
             }
 316  2
         }
 317  2
     }
 318  
 
 319  
     /**
 320  
      * @param writer
 321  
      * @param name
 322  
      * @param location
 323  
      */
 324  
     private void addLink( XMLWriter writer, String name, String location, int type )
 325  
     {
 326  2
         writer.startElement( "link" ); //$NON-NLS-1$
 327  
 
 328  2
         writer.startElement( ELT_NAME );
 329  2
         writer.writeText( name );
 330  2
         writer.endElement(); // name
 331  
 
 332  2
         writer.startElement( "type" ); //$NON-NLS-1$
 333  2
         writer.writeText( Integer.toString( type ) );
 334  2
         writer.endElement(); // type
 335  
 
 336  2
         writer.startElement( "location" ); //$NON-NLS-1$
 337  
 
 338  2
         writer.writeText( location );
 339  
 
 340  2
         writer.endElement(); // location
 341  
 
 342  2
         writer.endElement(); // link
 343  2
     }
 344  
 }