Coverage Report - org.apache.maven.plugin.eclipse.writers.EclipseProjectWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
EclipseProjectWriter
45%
67/147
28%
24/84
8.5
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.maven.plugin.eclipse.writers;
 20  
 
 21  
 import java.io.File;
 22  
 import java.io.FileInputStream;
 23  
 import java.io.FileOutputStream;
 24  
 import java.io.IOException;
 25  
 import java.io.InputStreamReader;
 26  
 import java.io.OutputStreamWriter;
 27  
 import java.io.Reader;
 28  
 import java.io.Writer;
 29  
 import java.util.ArrayList;
 30  
 import java.util.Iterator;
 31  
 import java.util.LinkedHashSet;
 32  
 import java.util.List;
 33  
 import java.util.Set;
 34  
 
 35  
 import org.apache.maven.model.Resource;
 36  
 import org.apache.maven.plugin.MojoExecutionException;
 37  
 import org.apache.maven.plugin.eclipse.BuildCommand;
 38  
 import org.apache.maven.plugin.eclipse.LinkedResource;
 39  
 import org.apache.maven.plugin.eclipse.Messages;
 40  
 import org.apache.maven.plugin.ide.IdeDependency;
 41  
 import org.apache.maven.plugin.ide.IdeUtils;
 42  
 import org.codehaus.plexus.util.IOUtil;
 43  
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 44  
 import org.codehaus.plexus.util.xml.XMLWriter;
 45  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 46  
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 47  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 48  
 
 49  
 /**
 50  
  * Writes eclipse .project file.
 51  
  * 
 52  
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 53  
  * @author <a href="mailto:kenney@neonics.com">Kenney Westerhof</a>
 54  
  * @author <a href="mailto:fgiust@apache.org">Fabrizio Giustina</a>
 55  
  * @version $Id: EclipseProjectWriter.java 1185446 2011-10-18 01:00:41Z baerrach $
 56  
  */
 57  1
 public class EclipseProjectWriter
 58  
     extends AbstractEclipseWriter
 59  
 {
 60  
     private static final String ELT_NAME = "name"; //$NON-NLS-1$
 61  
 
 62  
     private static final String ELT_COMMENT = "comment"; //$NON-NLS-1$
 63  
 
 64  
     private static final String ELT_BUILD_COMMAND = "buildCommand"; //$NON-NLS-1$
 65  
 
 66  
     private static final String ELT_LINK = "link"; //$NON-NLS-1$
 67  
 
 68  
     private static final String ELT_BUILD_SPEC = "buildSpec"; //$NON-NLS-1$
 69  
 
 70  
     private static final String ELT_LINKED_RESOURCES = "linkedResources"; //$NON-NLS-1$
 71  
 
 72  
     private static final String ELT_NATURE = "nature"; //$NON-NLS-1$
 73  
 
 74  
     private static final String ELT_NATURES = "natures"; //$NON-NLS-1$
 75  
 
 76  
     private static final String FILE_DOT_PROJECT = ".project"; //$NON-NLS-1$
 77  
 
 78  
     /**
 79  
      * Constant for links to files.
 80  
      */
 81  
     private static final int LINK_TYPE_FILE = 1;
 82  
 
 83  
     /**
 84  
      * Constant for links to directories.
 85  
      */
 86  
     private static final int LINK_TYPE_DIRECTORY = 2;
 87  
 
 88  
     /**
 89  
      * To Store the link names
 90  
      */
 91  1
     ArrayList linkNames = new ArrayList();
 92  
 
 93  
     /**
 94  
      * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
 95  
      */
 96  
     public void write()
 97  
         throws MojoExecutionException
 98  
     {
 99  
 
 100  1
         Set projectnatures = new LinkedHashSet();
 101  1
         Set buildCommands = new LinkedHashSet();
 102  1
         Set linkedResources = new LinkedHashSet();
 103  
 
 104  1
         File dotProject = new File( config.getEclipseProjectDirectory(), FILE_DOT_PROJECT );
 105  
 
 106  1
         if ( dotProject.exists() )
 107  
         {
 108  
 
 109  1
             log.info( Messages.getString( "EclipsePlugin.keepexisting", dotProject.getAbsolutePath() ) ); //$NON-NLS-1$
 110  
 
 111  
             // parse existing file in order to keep manually-added entries
 112  1
             Reader reader = null;
 113  
             try
 114  
             {
 115  1
                 reader = new InputStreamReader( new FileInputStream( dotProject ), "UTF-8" );
 116  1
                 Xpp3Dom dom = Xpp3DomBuilder.build( reader );
 117  
 
 118  1
                 Xpp3Dom naturesElement = dom.getChild( ELT_NATURES );
 119  1
                 if ( naturesElement != null )
 120  
                 {
 121  0
                     Xpp3Dom[] existingNatures = naturesElement.getChildren( ELT_NATURE );
 122  0
                     for ( int j = 0; j < existingNatures.length; j++ )
 123  
                     {
 124  
                         // adds all the existing natures
 125  0
                         projectnatures.add( existingNatures[j].getValue() );
 126  
                     }
 127  
                 }
 128  
 
 129  1
                 Xpp3Dom buildSpec = dom.getChild( ELT_BUILD_SPEC );
 130  1
                 if ( buildSpec != null )
 131  
                 {
 132  0
                     Xpp3Dom[] existingBuildCommands = buildSpec.getChildren( ELT_BUILD_COMMAND );
 133  0
                     for ( int j = 0; j < existingBuildCommands.length; j++ )
 134  
                     {
 135  0
                         Xpp3Dom buildCommandName = existingBuildCommands[j].getChild( ELT_NAME );
 136  0
                         if ( buildCommandName != null )
 137  
                         {
 138  0
                             buildCommands.add( new BuildCommand( existingBuildCommands[j] ) );
 139  
                         }
 140  
                     }
 141  
                 }
 142  
                 // Added the below code to preserve the Symbolic links
 143  1
                 Xpp3Dom linkedResourcesElement = dom.getChild( ELT_LINKED_RESOURCES );
 144  1
                 if ( linkedResourcesElement != null )
 145  
                 {
 146  1
                     Xpp3Dom[] existingLinks = linkedResourcesElement.getChildren( ELT_LINK );
 147  2
                     for ( int j = 0; j < existingLinks.length; j++ )
 148  
                     {
 149  1
                         Xpp3Dom linkName = existingLinks[j].getChild( ELT_NAME );
 150  1
                         if ( linkName != null )
 151  
                         {
 152  
                             // add all the existing symbolic links
 153  1
                             linkNames.add( existingLinks[j].getChild( ELT_NAME ).getValue() );
 154  1
                             linkedResources.add( new LinkedResource( existingLinks[j] ) );
 155  
                         }
 156  
                     }
 157  
                 }
 158  
 
 159  
             }
 160  0
             catch ( XmlPullParserException e )
 161  
             {
 162  0
                 log.warn( Messages.getString( "EclipsePlugin.cantparseexisting", dotProject.getAbsolutePath() ) ); //$NON-NLS-1$
 163  
             }
 164  0
             catch ( IOException e )
 165  
             {
 166  0
                 log.warn( Messages.getString( "EclipsePlugin.cantparseexisting", dotProject.getAbsolutePath() ) ); //$NON-NLS-1$
 167  
             }
 168  
             finally
 169  
             {
 170  1
                 IOUtil.close( reader );
 171  1
             }
 172  
         }
 173  
 
 174  
         // adds new entries after the existing ones
 175  1
         for ( Iterator iter = config.getProjectnatures().iterator(); iter.hasNext(); )
 176  
         {
 177  0
             projectnatures.add( iter.next() );
 178  
         }
 179  
 
 180  1
         for ( Iterator iter = config.getBuildCommands().iterator(); iter.hasNext(); )
 181  
         {
 182  0
             buildCommands.add( (BuildCommand) iter.next() );
 183  
         }
 184  
 
 185  1
         for ( Iterator iter = config.getLinkedResources().iterator(); iter.hasNext(); )
 186  
         {
 187  0
             linkedResources.add( (LinkedResource) iter.next() );
 188  
         }
 189  
 
 190  
         Writer w;
 191  
 
 192  
         try
 193  
         {
 194  1
             w = new OutputStreamWriter( new FileOutputStream( dotProject ), "UTF-8" );
 195  
         }
 196  0
         catch ( IOException ex )
 197  
         {
 198  0
             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
 199  1
         }
 200  
 
 201  1
         XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
 202  
 
 203  1
         writer.startElement( "projectDescription" ); //$NON-NLS-1$
 204  
 
 205  1
         writer.startElement( ELT_NAME );
 206  1
         writer.writeText( config.getEclipseProjectName() );
 207  1
         writer.endElement();
 208  
 
 209  1
         addComment( writer, config.getProject().getDescription() );
 210  
 
 211  1
         writer.startElement( "projects" ); //$NON-NLS-1$
 212  
 
 213  1
         IdeDependency[] dependencies = config.getDeps();
 214  
         
 215  
         // referenced projects should not be added for plugins
 216  1
         if ( !config.isPde() )
 217  
         {
 218  1
             List duplicates = new ArrayList();
 219  1
             for ( int j = 0; j < dependencies.length; j++ )
 220  
             {
 221  0
                 IdeDependency dep = dependencies[j];
 222  
                 // Avoid duplicates entries when same project is refered using multiple types
 223  
                 // (ejb, test-jar ...)
 224  0
                 if ( dep.isReferencedProject() && !duplicates.contains( dep.getEclipseProjectName() ) )
 225  
                 {
 226  0
                     writer.startElement( "project" ); //$NON-NLS-1$
 227  0
                     writer.writeText( dep.getEclipseProjectName() );
 228  0
                     writer.endElement();
 229  0
                     duplicates.add( dep.getEclipseProjectName() );
 230  
                 }
 231  
             }
 232  
         }
 233  
 
 234  1
         writer.endElement(); // projects
 235  
 
 236  1
         writer.startElement( ELT_BUILD_SPEC );
 237  
 
 238  1
         for ( Iterator it = buildCommands.iterator(); it.hasNext(); )
 239  
         {
 240  0
             ( (BuildCommand) it.next() ).print( writer );
 241  
         }
 242  
 
 243  1
         writer.endElement(); // buildSpec
 244  
 
 245  1
         writer.startElement( ELT_NATURES );
 246  
 
 247  1
         for ( Iterator it = projectnatures.iterator(); it.hasNext(); )
 248  
         {
 249  0
             writer.startElement( ELT_NATURE );
 250  0
             writer.writeText( (String) it.next() );
 251  0
             writer.endElement(); // name
 252  
         }
 253  
 
 254  1
         writer.endElement(); // natures
 255  
 
 256  1
         boolean addLinks = !config.getProjectBaseDir().equals( config.getEclipseProjectDirectory() );
 257  
 
 258  1
         if ( addLinks || ( config.isPde() && dependencies.length > 0 ) || linkedResources.size() > 0 )
 259  
         {
 260  1
             writer.startElement( "linkedResources" ); //$NON-NLS-1$
 261  
             // preserve the symbolic links
 262  1
             if ( linkedResources.size() > 0 )
 263  
             {
 264  1
                 for ( Iterator it = linkedResources.iterator(); it.hasNext(); )
 265  
                 {
 266  1
                     ( (LinkedResource) it.next() ).print( writer );
 267  
                 }
 268  
             }
 269  
 
 270  1
             if ( addLinks )
 271  
             {
 272  
 
 273  0
                 addFileLink( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(),
 274  
                              config.getProject().getFile() );
 275  
 
 276  0
                 addSourceLinks( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(),
 277  
                                 config.getProject().getCompileSourceRoots() );
 278  0
                 addResourceLinks( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(),
 279  
                                   config.getProject().getBuild().getResources() );
 280  
 
 281  0
                 addSourceLinks( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(),
 282  
                                 config.getProject().getTestCompileSourceRoots() );
 283  0
                 addResourceLinks( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(),
 284  
                                   config.getProject().getBuild().getTestResources() );
 285  
 
 286  
             }
 287  
 
 288  1
             if ( config.isPde() )
 289  
             {
 290  0
                 for ( int j = 0; j < dependencies.length; j++ )
 291  
                 {
 292  0
                     IdeDependency dep = dependencies[j];
 293  
 
 294  0
                     if ( dep.isAddedToClasspath() && !dep.isProvided() && !dep.isReferencedProject()
 295  
                         && !dep.isTestDependency() && !dep.isOsgiBundle() )
 296  
                     {
 297  0
                         String name = dep.getFile().getName();
 298  0
                         addLink( writer, name, IdeUtils.fixSeparator( IdeUtils.getCanonicalPath( dep.getFile() ) ),
 299  
                                  LINK_TYPE_FILE );
 300  
                     }
 301  
                 }
 302  
             }
 303  
 
 304  1
             writer.endElement(); // linkedResources
 305  
         }
 306  
 
 307  1
         writer.endElement(); // projectDescription
 308  
 
 309  1
         IOUtil.close( w );
 310  1
     }
 311  
 
 312  
     private void addFileLink( XMLWriter writer, File projectBaseDir, File basedir, File file )
 313  
         throws MojoExecutionException
 314  
     {
 315  0
         if ( file.isFile() )
 316  
         {
 317  0
             String name = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, file, true );
 318  0
             String location = IdeUtils.fixSeparator( IdeUtils.getCanonicalPath( file ) );
 319  
 
 320  0
             addLink( writer, name, location, LINK_TYPE_FILE );
 321  0
         }
 322  
         else
 323  
         {
 324  0
             log.warn( Messages.getString( "EclipseProjectWriter.notafile", file ) ); //$NON-NLS-1$
 325  
         }
 326  0
     }
 327  
 
 328  
     private void addSourceLinks( XMLWriter writer, File projectBaseDir, File basedir, List sourceRoots )
 329  
         throws MojoExecutionException
 330  
     {
 331  0
         for ( Iterator it = sourceRoots.iterator(); it.hasNext(); )
 332  
         {
 333  0
             String sourceRootString = (String) it.next();
 334  0
             File sourceRoot = new File( sourceRootString );
 335  
 
 336  0
             if ( sourceRoot.isDirectory() )
 337  
             {
 338  0
                 String name = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, sourceRoot, true );
 339  0
                 String location = IdeUtils.fixSeparator( IdeUtils.getCanonicalPath( sourceRoot ) );
 340  
 
 341  0
                 addLink( writer, name, location, LINK_TYPE_DIRECTORY );
 342  
             }
 343  0
         }
 344  0
     }
 345  
 
 346  
     private void addResourceLinks( XMLWriter writer, File projectBaseDir, File basedir, List sourceRoots )
 347  
         throws MojoExecutionException
 348  
     {
 349  0
         for ( Iterator it = sourceRoots.iterator(); it.hasNext(); )
 350  
         {
 351  0
             String resourceDirString = ( (Resource) it.next() ).getDirectory();
 352  0
             File resourceDir = new File( resourceDirString );
 353  
 
 354  0
             if ( resourceDir.isDirectory() )
 355  
             {
 356  0
                 String name = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, resourceDir, true );
 357  0
                 String location = IdeUtils.fixSeparator( IdeUtils.getCanonicalPath( resourceDir ) );
 358  
 
 359  0
                 addLink( writer, name, location, LINK_TYPE_DIRECTORY );
 360  
             }
 361  0
         }
 362  0
     }
 363  
 
 364  
     /**
 365  
      * @param writer
 366  
      * @param name
 367  
      * @param location
 368  
      */
 369  
     private void addLink( XMLWriter writer, String name, String location, int type )
 370  
     {
 371  
         // Avoid duplicates entries of the link..
 372  0
         if ( !linkNames.contains( name ) )
 373  
         {
 374  
 
 375  0
             writer.startElement( "link" ); //$NON-NLS-1$
 376  
 
 377  0
             writer.startElement( ELT_NAME );
 378  0
             writer.writeText( name );
 379  0
             writer.endElement(); // name
 380  
 
 381  0
             writer.startElement( "type" ); //$NON-NLS-1$
 382  0
             writer.writeText( Integer.toString( type ) );
 383  0
             writer.endElement(); // type
 384  
 
 385  0
             writer.startElement( "location" ); //$NON-NLS-1$
 386  
 
 387  0
             writer.writeText( location );
 388  
 
 389  0
             writer.endElement(); // location
 390  
 
 391  0
             writer.endElement(); // link
 392  
         }
 393  0
     }
 394  
 
 395  
     private void addComment( XMLWriter writer, String projectDescription )
 396  
     {
 397  1
         String comment = "";
 398  
 
 399  1
         if ( projectDescription != null )
 400  
         {
 401  0
             comment = projectDescription.trim();
 402  
 
 403  0
             if ( comment.length() > 0 )
 404  
             {
 405  0
                 if ( !comment.endsWith( "." ) )
 406  
                 {
 407  0
                     comment += ".";
 408  
                 }
 409  0
                 comment += " ";
 410  
             }
 411  
         }
 412  
 
 413  
         //
 414  
         // Project files that are generated with m-p-e cannot be supported by M2Eclipse
 415  
         //
 416  1
         comment += "NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.";
 417  
 
 418  1
         writer.startElement( ELT_COMMENT );
 419  1
         writer.writeText( comment );
 420  1
         writer.endElement();
 421  1
     }
 422  
 
 423  
 }