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.List;
27  import java.util.Properties;
28  
29  import org.apache.maven.model.Resource;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugin.eclipse.Messages;
32  import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
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 1517906 2013-08-27 18:25:03Z krosenvold $
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 PROP_ECLIPSE_PREFERENCES_VERSION = "eclipse.preferences.version"; //$NON-NLS-1$
48  
49      private static final String PROP_JDT_CORE_COMPILER_COMPLIANCE = "org.eclipse.jdt.core.compiler.compliance"; //$NON-NLS-1$
50  
51      private static final String PROP_JDT_CORE_COMPILER_SOURCE = "org.eclipse.jdt.core.compiler.source"; //$NON-NLS-1$
52  
53      private static final String PROP_JDT_CORE_COMPILER_ENCODING = "encoding/"; //$NON-NLS-1$
54  
55      /**
56       * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
57       */
58      public void write()
59          throws MojoExecutionException
60      {
61  
62          // check if it's necessary to create project specific settings
63          Properties coreSettings = new Properties();
64  
65          String source = IdeUtils.getCompilerSourceVersion( config.getProject() );
66          String encoding = IdeUtils.getCompilerSourceEncoding( config.getProject() );
67          String target = IdeUtils.getCompilerTargetVersion( config.getProject() );
68  
69          if ( source != null )
70          {
71              coreSettings.put( PROP_JDT_CORE_COMPILER_SOURCE, source );
72              coreSettings.put( PROP_JDT_CORE_COMPILER_COMPLIANCE, source );
73          }
74          
75          if ( encoding != null )
76          {
77              File basedir = config.getProject().getBasedir();
78  			List compileSourceRoots = config.getProject().getCompileSourceRoots();
79  			if ( compileSourceRoots != null )
80  			{
81                  for (Object compileSourceRoot : compileSourceRoots) {
82                      String sourcePath = (String) compileSourceRoot;
83                      String relativePath = IdeUtils.toRelativeAndFixSeparator(basedir, new File(sourcePath), false);
84                      coreSettings.put(PROP_JDT_CORE_COMPILER_ENCODING + relativePath, encoding);
85                  }
86  			}
87  			List testCompileSourceRoots = config.getProject().getTestCompileSourceRoots();
88              if ( testCompileSourceRoots != null )
89  			{
90                  for (Object testCompileSourceRoot : testCompileSourceRoots) {
91                      String sourcePath = (String) testCompileSourceRoot;
92                      String relativePath = IdeUtils.toRelativeAndFixSeparator(basedir, new File(sourcePath), false);
93                      coreSettings.put(PROP_JDT_CORE_COMPILER_ENCODING + relativePath, encoding);
94                  }
95  			}
96  			List resources = config.getProject().getResources();
97              if ( resources != null )
98  			{
99                  for (Object resource1 : resources) {
100                     Resource resource = (Resource) resource1;
101                     String relativePath =
102                             IdeUtils.toRelativeAndFixSeparator(basedir, new File(resource.getDirectory()), false);
103                     coreSettings.put(PROP_JDT_CORE_COMPILER_ENCODING + relativePath, encoding);
104                 }
105 			}
106 			List testResources = config.getProject().getTestResources();
107             if ( testResources != null )
108 			{
109                 for (Object testResource : testResources) {
110                     Resource resource = (Resource) testResource;
111                     String relativePath =
112                             IdeUtils.toRelativeAndFixSeparator(basedir, new File(resource.getDirectory()), false);
113                     coreSettings.put(PROP_JDT_CORE_COMPILER_ENCODING + relativePath, encoding);
114                 }
115 			}
116         }
117 
118         if ( target != null && !JDK_1_2_SOURCES.equals( target ) )
119         {
120             coreSettings.put( "org.eclipse.jdt.core.compiler.codegen.targetPlatform", target ); //$NON-NLS-1$
121         }
122 
123         // write the settings, if needed
124         if ( !coreSettings.isEmpty() )
125         {
126             File settingsDir = new File( config.getEclipseProjectDirectory(), EclipseWorkspaceWriter.DIR_DOT_SETTINGS ); //$NON-NLS-1$
127 
128             settingsDir.mkdirs();
129 
130             coreSettings.put( PROP_ECLIPSE_PREFERENCES_VERSION, "1" ); //$NON-NLS-1$ 
131 
132             try
133             {
134                 File oldCoreSettingsFile;
135 
136                 File coreSettingsFile = new File( settingsDir, EclipseWorkspaceWriter.ECLIPSE_JDT_CORE_PREFS_FILE );
137 
138                 if ( coreSettingsFile.exists() )
139                 {
140                     oldCoreSettingsFile = coreSettingsFile;
141 
142                     Properties oldsettings = new Properties();
143                     oldsettings.load( new FileInputStream( oldCoreSettingsFile ) );
144 
145                     Properties newsettings = (Properties) oldsettings.clone();
146                     newsettings.putAll( coreSettings );
147 
148                     if ( !oldsettings.equals( newsettings ) )
149                     {
150                         newsettings.store( new FileOutputStream( coreSettingsFile ), null );
151                     }
152                 }
153                 else
154                 {
155                     coreSettings.store( new FileOutputStream( coreSettingsFile ), null );
156 
157                     log.info( Messages.getString( "EclipseSettingsWriter.wrotesettings", //$NON-NLS-1$
158                                                   coreSettingsFile.getCanonicalPath() ) );
159                 }
160             }
161             catch ( FileNotFoundException e )
162             {
163                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.cannotcreatesettings" ), e ); //$NON-NLS-1$
164             }
165             catch ( IOException e )
166             {
167                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.errorwritingsettings" ), e ); //$NON-NLS-1$
168             }
169         }
170         else
171         {
172             log.info( Messages.getString( "EclipseSettingsWriter.usingdefaults" ) ); //$NON-NLS-1$
173         }
174     }
175 }