1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
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  
34  
35  
36  
37  
38  public class EclipseSettingsWriter
39      extends AbstractEclipseWriter
40  {
41  
42      private static final String JDK_1_2_SOURCES = "1.2"; 
43  
44      private static final String FILE_ECLIPSE_JDT_CORE_PREFS = "org.eclipse.jdt.core.prefs"; 
45  
46      private static final String PROP_ECLIPSE_PREFERENCES_VERSION = "eclipse.preferences.version"; 
47  
48      private static final String DIR_DOT_SETTINGS = ".settings"; 
49  
50      private static final String PROP_JDT_CORE_COMPILER_COMPLIANCE = "org.eclipse.jdt.core.compiler.compliance"; 
51  
52      private static final String PROP_JDT_CORE_COMPILER_SOURCE = "org.eclipse.jdt.core.compiler.source"; 
53  
54      
55  
56  
57      public void write()
58          throws MojoExecutionException
59      {
60  
61          
62          Properties coreSettings = new Properties();
63  
64          String source = IdeUtils.getCompilerSourceVersion( config.getProject() );
65          String target = IdeUtils.getCompilerTargetVersion( config.getProject() );
66  
67          if ( source != null )
68          {
69              coreSettings.put( PROP_JDT_CORE_COMPILER_SOURCE, source );
70              coreSettings.put( PROP_JDT_CORE_COMPILER_COMPLIANCE, source );
71          }
72  
73          if ( target != null && !JDK_1_2_SOURCES.equals( target ) )
74          {
75              coreSettings.put( "org.eclipse.jdt.core.compiler.codegen.targetPlatform", target ); 
76          }
77  
78          
79          if ( !coreSettings.isEmpty() )
80          {
81              File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_DOT_SETTINGS ); 
82  
83              settingsDir.mkdirs();
84  
85              coreSettings.put( PROP_ECLIPSE_PREFERENCES_VERSION, "1" ); 
86  
87              try
88              {
89                  File oldCoreSettingsFile;
90  
91                  File coreSettingsFile = new File( settingsDir, FILE_ECLIPSE_JDT_CORE_PREFS );
92  
93                  if ( coreSettingsFile.exists() )
94                  {
95                      oldCoreSettingsFile = coreSettingsFile;
96  
97                      Properties oldsettings = new Properties();
98                      oldsettings.load( new FileInputStream( oldCoreSettingsFile ) );
99  
100                     Properties newsettings = (Properties) oldsettings.clone();
101                     newsettings.putAll( coreSettings );
102 
103                     if ( !oldsettings.equals( newsettings ) )
104                     {
105                         newsettings.store( new FileOutputStream( coreSettingsFile ), null );
106                     }
107                 }
108                 else
109                 {
110                     coreSettings.store( new FileOutputStream( coreSettingsFile ), null );
111 
112                     log.info( Messages.getString( "EclipseSettingsWriter.wrotesettings", 
113                                                   coreSettingsFile.getCanonicalPath() ) );
114                 }
115             }
116             catch ( FileNotFoundException e )
117             {
118                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.cannotcreatesettings" ), e ); 
119             }
120             catch ( IOException e )
121             {
122                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.errorwritingsettings" ), e ); 
123             }
124         }
125         else
126         {
127             log.info( Messages.getString( "EclipseSettingsWriter.usingdefaults" ) ); 
128         }
129     }
130 }