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