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  import java.util.Iterator;
28  import java.util.List;
29  
30  import org.apache.maven.model.Resource;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugin.eclipse.Messages;
33  import org.apache.maven.plugin.ide.IdeUtils;
34  
35  /**
36   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
37   * @author <a href="mailto:kenney@neonics.com">Kenney Westerhof</a>
38   * @author <a href="mailto:fgiust@users.sourceforge.net">Fabrizio Giustina</a>
39   * @version $Id: EclipseSettingsWriter.java 825432 2009-10-15 08:25:55Z nicolas $
40   */
41  public class EclipseSettingsWriter
42      extends AbstractEclipseWriter
43  {
44  
45      private static final String JDK_1_2_SOURCES = "1.2"; //$NON-NLS-1$
46  
47      private static final String FILE_ECLIPSE_JDT_CORE_PREFS = "org.eclipse.jdt.core.prefs"; //$NON-NLS-1$
48  
49      private static final String PROP_ECLIPSE_PREFERENCES_VERSION = "eclipse.preferences.version"; //$NON-NLS-1$
50  
51      private static final String DIR_DOT_SETTINGS = ".settings"; //$NON-NLS-1$
52  
53      private static final String PROP_JDT_CORE_COMPILER_COMPLIANCE = "org.eclipse.jdt.core.compiler.compliance"; //$NON-NLS-1$
54  
55      private static final String PROP_JDT_CORE_COMPILER_SOURCE = "org.eclipse.jdt.core.compiler.source"; //$NON-NLS-1$
56  
57      private static final String PROP_JDT_CORE_COMPILER_ENCODING = "encoding/"; //$NON-NLS-1$
58  
59      /**
60       * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
61       */
62      public void write()
63          throws MojoExecutionException
64      {
65  
66          // check if it's necessary to create project specific settings
67          Properties coreSettings = new Properties();
68  
69          String source = IdeUtils.getCompilerSourceVersion( config.getProject() );
70          String encoding = IdeUtils.getCompilerSourceEncoding( config.getProject() );
71          String target = IdeUtils.getCompilerTargetVersion( config.getProject() );
72  
73          if ( source != null )
74          {
75              coreSettings.put( PROP_JDT_CORE_COMPILER_SOURCE, source );
76              coreSettings.put( PROP_JDT_CORE_COMPILER_COMPLIANCE, source );
77          }
78          
79          if ( encoding != null )
80          {
81              String basedir = config.getProject().getBasedir().getAbsolutePath();
82  			List compileSourceRoots = config.getProject().getCompileSourceRoots();
83  			if ( compileSourceRoots != null )
84  			{
85  				Iterator it = compileSourceRoots.iterator();
86  				while ( it.hasNext() )
87  				{
88  					String sourcePath = (String) it.next();
89  					String relativePath = sourcePath.substring( basedir.length() ).replace( '\\', '/' );
90  					coreSettings.put( PROP_JDT_CORE_COMPILER_ENCODING + relativePath, encoding );
91  				}
92  			}
93  			List testCompileSourceRoots = config.getProject().getTestCompileSourceRoots();
94              if ( testCompileSourceRoots != null )
95  			{
96  				Iterator it = testCompileSourceRoots.iterator();
97  				while ( it.hasNext() )
98  				{
99  					String sourcePath = (String) it.next();
100 					String relativePath = sourcePath.substring( basedir.length() ).replace( '\\', '/' );
101 					coreSettings.put( PROP_JDT_CORE_COMPILER_ENCODING + relativePath, encoding );
102 				}
103 			}
104 			List resources = config.getProject().getResources();
105             if ( resources != null )
106 			{
107 				Iterator it = resources.iterator();
108 				while ( it.hasNext() )
109 				{
110 					Resource resource = (Resource) it.next();
111 					String relativePath = resource.getDirectory().substring( basedir.length() ).replace( '\\', '/' );
112 					coreSettings.put( PROP_JDT_CORE_COMPILER_ENCODING + relativePath, encoding );
113 				}
114 			}
115 			List testResources = config.getProject().getTestResources();
116             if ( testResources != null )
117 			{
118 				Iterator it = testResources.iterator();
119 				while ( it.hasNext() )
120 				{
121 					Resource resource = (Resource) it.next();
122 					String relativePath = resource.getDirectory().substring( basedir.length() ).replace( '\\', '/' );
123 					coreSettings.put( PROP_JDT_CORE_COMPILER_ENCODING + relativePath, encoding );
124 				}
125 			}
126         }
127 
128         if ( target != null && !JDK_1_2_SOURCES.equals( target ) )
129         {
130             coreSettings.put( "org.eclipse.jdt.core.compiler.codegen.targetPlatform", target ); //$NON-NLS-1$
131         }
132 
133         // write the settings, if needed
134         if ( !coreSettings.isEmpty() )
135         {
136             File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_DOT_SETTINGS ); //$NON-NLS-1$
137 
138             settingsDir.mkdirs();
139 
140             coreSettings.put( PROP_ECLIPSE_PREFERENCES_VERSION, "1" ); //$NON-NLS-1$ 
141 
142             try
143             {
144                 File oldCoreSettingsFile;
145 
146                 File coreSettingsFile = new File( settingsDir, FILE_ECLIPSE_JDT_CORE_PREFS );
147 
148                 if ( coreSettingsFile.exists() )
149                 {
150                     oldCoreSettingsFile = coreSettingsFile;
151 
152                     Properties oldsettings = new Properties();
153                     oldsettings.load( new FileInputStream( oldCoreSettingsFile ) );
154 
155                     Properties newsettings = (Properties) oldsettings.clone();
156                     newsettings.putAll( coreSettings );
157 
158                     if ( !oldsettings.equals( newsettings ) )
159                     {
160                         newsettings.store( new FileOutputStream( coreSettingsFile ), null );
161                     }
162                 }
163                 else
164                 {
165                     coreSettings.store( new FileOutputStream( coreSettingsFile ), null );
166 
167                     log.info( Messages.getString( "EclipseSettingsWriter.wrotesettings", //$NON-NLS-1$
168                                                   coreSettingsFile.getCanonicalPath() ) );
169                 }
170             }
171             catch ( FileNotFoundException e )
172             {
173                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.cannotcreatesettings" ), e ); //$NON-NLS-1$
174             }
175             catch ( IOException e )
176             {
177                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.errorwritingsettings" ), e ); //$NON-NLS-1$
178             }
179         }
180         else
181         {
182             log.info( Messages.getString( "EclipseSettingsWriter.usingdefaults" ) ); //$NON-NLS-1$
183         }
184     }
185 }