View Javadoc

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  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          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 ); //$NON-NLS-1$
76          }
77  
78          // write the settings, if needed
79          if ( !coreSettings.isEmpty() )
80          {
81              File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_DOT_SETTINGS ); //$NON-NLS-1$
82  
83              settingsDir.mkdirs();
84  
85              coreSettings.put( PROP_ECLIPSE_PREFERENCES_VERSION, "1" ); //$NON-NLS-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", //$NON-NLS-1$
113                                                   coreSettingsFile.getCanonicalPath() ) );
114                 }
115             }
116             catch ( FileNotFoundException e )
117             {
118                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.cannotcreatesettings" ), e ); //$NON-NLS-1$
119             }
120             catch ( IOException e )
121             {
122                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.errorwritingsettings" ), e ); //$NON-NLS-1$
123             }
124         }
125         else
126         {
127             log.info( Messages.getString( "EclipseSettingsWriter.usingdefaults" ) ); //$NON-NLS-1$
128         }
129     }
130 }