Coverage Report - org.apache.maven.plugin.eclipse.writers.EclipseSettingsWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
EclipseSettingsWriter
27% 
60% 
10
 
 1  
 package org.apache.maven.plugin.eclipse.writers;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * 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, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.FileInputStream;
 21  
 import java.io.FileNotFoundException;
 22  
 import java.io.FileOutputStream;
 23  
 import java.io.IOException;
 24  
 import java.util.Properties;
 25  
 
 26  
 import org.apache.maven.plugin.MojoExecutionException;
 27  
 import org.apache.maven.plugin.eclipse.Messages;
 28  
 import org.apache.maven.plugin.ide.IdeUtils;
 29  
 
 30  
 /**
 31  
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 32  
  * @author <a href="mailto:kenney@neonics.com">Kenney Westerhof</a>
 33  
  * @author <a href="mailto:fgiust@users.sourceforge.net">Fabrizio Giustina</a>
 34  
  * @version $Id: EclipseSettingsWriter.java 424062 2006-07-20 21:04:59Z fgiust $
 35  
  */
 36  12
 public class EclipseSettingsWriter
 37  
     extends AbstractEclipseWriter
 38  
 {
 39  
 
 40  
     /**
 41  
      * 'target' property for maven-compiler-plugin.
 42  
      */
 43  
     private static final String PROPERTY_TARGET = "target"; //$NON-NLS-1$
 44  
 
 45  
     /**
 46  
      * 'source' property for maven-compiler-plugin.
 47  
      */
 48  
     private static final String PROPERTY_SOURCE = "source"; //$NON-NLS-1$
 49  
 
 50  
     private static final String JDK_1_2_SOURCES = "1.2"; //$NON-NLS-1$
 51  
 
 52  
     private static final String FILE_ECLIPSE_JDT_CORE_PREFS = "org.eclipse.jdt.core.prefs"; //$NON-NLS-1$
 53  
 
 54  
     private static final String PROP_ECLIPSE_PREFERENCES_VERSION = "eclipse.preferences.version"; //$NON-NLS-1$
 55  
 
 56  
     private static final String DIR_DOT_SETTINGS = ".settings"; //$NON-NLS-1$
 57  
 
 58  
     private static final String PROP_JDT_CORE_COMPILER_COMPLIANCE = "org.eclipse.jdt.core.compiler.compliance"; //$NON-NLS-1$
 59  
 
 60  
     private static final String PROP_JDT_CORE_COMPILER_SOURCE = "org.eclipse.jdt.core.compiler.source"; //$NON-NLS-1$
 61  
 
 62  
     private static final String ARTIFACT_MAVEN_COMPILER_PLUGIN = "maven-compiler-plugin"; //$NON-NLS-1$
 63  
 
 64  
     /**
 65  
      * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
 66  
      */
 67  
     public void write()
 68  
         throws MojoExecutionException
 69  
     {
 70  
 
 71  
         // check if it's necessary to create project specific settings
 72  12
         Properties coreSettings = new Properties();
 73  
 
 74  12
         String source = IdeUtils.getPluginSetting( config.getProject(), ARTIFACT_MAVEN_COMPILER_PLUGIN,
 75  
                                                    PROPERTY_SOURCE, null );
 76  12
         String target = IdeUtils.getPluginSetting( config.getProject(), ARTIFACT_MAVEN_COMPILER_PLUGIN,
 77  
                                                    PROPERTY_TARGET, null );
 78  
 
 79  12
         if ( source != null )
 80  
         {
 81  0
             coreSettings.put( PROP_JDT_CORE_COMPILER_SOURCE, source );
 82  0
             coreSettings.put( PROP_JDT_CORE_COMPILER_COMPLIANCE, source );
 83  
         }
 84  
 
 85  12
         if ( target != null && !JDK_1_2_SOURCES.equals( target ) )
 86  
         {
 87  0
             coreSettings.put( "org.eclipse.jdt.core.compiler.codegen.targetPlatform", target ); //$NON-NLS-1$
 88  
         }
 89  
 
 90  
         // write the settings, if needed
 91  12
         if ( !coreSettings.isEmpty() )
 92  
         {
 93  0
             File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_DOT_SETTINGS ); //$NON-NLS-1$
 94  
 
 95  0
             settingsDir.mkdirs();
 96  
 
 97  0
             coreSettings.put( PROP_ECLIPSE_PREFERENCES_VERSION, "1" ); //$NON-NLS-1$ 
 98  
 
 99  
             try
 100  
             {
 101  
                 File oldCoreSettingsFile;
 102  
 
 103  0
                 File coreSettingsFile = new File( settingsDir, FILE_ECLIPSE_JDT_CORE_PREFS );
 104  
 
 105  0
                 if ( coreSettingsFile.exists() )
 106  
                 {
 107  0
                     oldCoreSettingsFile = coreSettingsFile;
 108  
 
 109  0
                     Properties oldsettings = new Properties();
 110  0
                     oldsettings.load( new FileInputStream( oldCoreSettingsFile ) );
 111  
 
 112  0
                     Properties newsettings = (Properties) oldsettings.clone();
 113  0
                     newsettings.putAll( coreSettings );
 114  
 
 115  0
                     if ( !oldsettings.equals( newsettings ) )
 116  
                     {
 117  0
                         newsettings.store( new FileOutputStream( coreSettingsFile ), null );
 118  
                     }
 119  0
                 }
 120  
                 else
 121  
                 {
 122  0
                     coreSettings.store( new FileOutputStream( coreSettingsFile ), null );
 123  
 
 124  0
                     log.info( Messages.getString( "EclipseSettingsWriter.wrotesettings", //$NON-NLS-1$
 125  
                                                   coreSettingsFile.getCanonicalPath() ) );
 126  
                 }
 127  
             }
 128  0
             catch ( FileNotFoundException e )
 129  
             {
 130  0
                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.cannotcreatesettings" ), e ); //$NON-NLS-1$
 131  
             }
 132  0
             catch ( IOException e )
 133  
             {
 134  0
                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.errorwritingsettings" ), e ); //$NON-NLS-1$
 135  0
             }
 136  0
         }
 137  
         else
 138  
         {
 139  12
             log.info( Messages.getString( "EclipseSettingsWriter.usingdefaults" ) ); //$NON-NLS-1$
 140  
         }
 141  12
     }
 142  
 }