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