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