Coverage Report - org.apache.maven.plugin.eclipse.writers.EclipseSettingsWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
EclipseSettingsWriter
0%
0/31
0%
0/12
10
 
 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.FileNotFoundException;
 24  
 import java.io.FileOutputStream;
 25  
 import java.io.IOException;
 26  
 import java.util.Properties;
 27  
 
 28  
 import org.apache.maven.plugin.MojoExecutionException;
 29  
 import org.apache.maven.plugin.eclipse.Messages;
 30  
 import org.apache.maven.plugin.ide.IdeUtils;
 31  
 
 32  
 /**
 33  
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 34  
  * @author <a href="mailto:kenney@neonics.com">Kenney Westerhof</a>
 35  
  * @author <a href="mailto:fgiust@users.sourceforge.net">Fabrizio Giustina</a>
 36  
  * @version $Id: EclipseSettingsWriter.java 728546 2008-12-21 22:56:51Z bentmann $
 37  
  */
 38  0
 public class EclipseSettingsWriter
 39  
     extends AbstractEclipseWriter
 40  
 {
 41  
 
 42  
     private static final String JDK_1_2_SOURCES = "1.2"; //$NON-NLS-1$
 43  
 
 44  
     private static final String FILE_ECLIPSE_JDT_CORE_PREFS = "org.eclipse.jdt.core.prefs"; //$NON-NLS-1$
 45  
 
 46  
     private static final String PROP_ECLIPSE_PREFERENCES_VERSION = "eclipse.preferences.version"; //$NON-NLS-1$
 47  
 
 48  
     private static final String DIR_DOT_SETTINGS = ".settings"; //$NON-NLS-1$
 49  
 
 50  
     private static final String PROP_JDT_CORE_COMPILER_COMPLIANCE = "org.eclipse.jdt.core.compiler.compliance"; //$NON-NLS-1$
 51  
 
 52  
     private static final String PROP_JDT_CORE_COMPILER_SOURCE = "org.eclipse.jdt.core.compiler.source"; //$NON-NLS-1$
 53  
 
 54  
     /**
 55  
      * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
 56  
      */
 57  
     public void write()
 58  
         throws MojoExecutionException
 59  
     {
 60  
 
 61  
         // check if it's necessary to create project specific settings
 62  0
         Properties coreSettings = new Properties();
 63  
 
 64  0
         String source = IdeUtils.getCompilerSourceVersion( config.getProject() );
 65  0
         String target = IdeUtils.getCompilerTargetVersion( config.getProject() );
 66  
 
 67  0
         if ( source != null )
 68  
         {
 69  0
             coreSettings.put( PROP_JDT_CORE_COMPILER_SOURCE, source );
 70  0
             coreSettings.put( PROP_JDT_CORE_COMPILER_COMPLIANCE, source );
 71  
         }
 72  
 
 73  0
         if ( target != null && !JDK_1_2_SOURCES.equals( target ) )
 74  
         {
 75  0
             coreSettings.put( "org.eclipse.jdt.core.compiler.codegen.targetPlatform", target ); //$NON-NLS-1$
 76  
         }
 77  
 
 78  
         // write the settings, if needed
 79  0
         if ( !coreSettings.isEmpty() )
 80  
         {
 81  0
             File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_DOT_SETTINGS ); //$NON-NLS-1$
 82  
 
 83  0
             settingsDir.mkdirs();
 84  
 
 85  0
             coreSettings.put( PROP_ECLIPSE_PREFERENCES_VERSION, "1" ); //$NON-NLS-1$ 
 86  
 
 87  
             try
 88  
             {
 89  
                 File oldCoreSettingsFile;
 90  
 
 91  0
                 File coreSettingsFile = new File( settingsDir, FILE_ECLIPSE_JDT_CORE_PREFS );
 92  
 
 93  0
                 if ( coreSettingsFile.exists() )
 94  
                 {
 95  0
                     oldCoreSettingsFile = coreSettingsFile;
 96  
 
 97  0
                     Properties oldsettings = new Properties();
 98  0
                     oldsettings.load( new FileInputStream( oldCoreSettingsFile ) );
 99  
 
 100  0
                     Properties newsettings = (Properties) oldsettings.clone();
 101  0
                     newsettings.putAll( coreSettings );
 102  
 
 103  0
                     if ( !oldsettings.equals( newsettings ) )
 104  
                     {
 105  0
                         newsettings.store( new FileOutputStream( coreSettingsFile ), null );
 106  
                     }
 107  
                 }
 108  
                 else
 109  
                 {
 110  0
                     coreSettings.store( new FileOutputStream( coreSettingsFile ), null );
 111  
 
 112  0
                     log.info( Messages.getString( "EclipseSettingsWriter.wrotesettings", //$NON-NLS-1$
 113  
                                                   coreSettingsFile.getCanonicalPath() ) );
 114  
                 }
 115  
             }
 116  0
             catch ( FileNotFoundException e )
 117  
             {
 118  0
                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.cannotcreatesettings" ), e ); //$NON-NLS-1$
 119  
             }
 120  0
             catch ( IOException e )
 121  
             {
 122  0
                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.errorwritingsettings" ), e ); //$NON-NLS-1$
 123  0
             }
 124  
         }
 125  
         else
 126  
         {
 127  0
             log.info( Messages.getString( "EclipseSettingsWriter.usingdefaults" ) ); //$NON-NLS-1$
 128  
         }
 129  0
     }
 130  
 }