1 |
|
package org.apache.maven.plugin.eclipse.writers; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
import java.io.File; |
20 |
|
import java.io.FileInputStream; |
21 |
|
import java.io.FileNotFoundException; |
22 |
|
import java.io.FileOutputStream; |
23 |
|
import java.io.IOException; |
24 |
|
import java.util.Properties; |
25 |
|
|
26 |
|
import org.apache.maven.plugin.MojoExecutionException; |
27 |
|
import org.apache.maven.plugin.eclipse.Messages; |
28 |
|
import org.apache.maven.plugin.ide.IdeUtils; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
12 |
public class EclipseSettingsWriter |
37 |
|
extends AbstractEclipseWriter |
38 |
|
{ |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
private static final String PROPERTY_TARGET = "target"; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
private static final String PROPERTY_SOURCE = "source"; |
49 |
|
|
50 |
|
private static final String JDK_1_2_SOURCES = "1.2"; |
51 |
|
|
52 |
|
private static final String FILE_ECLIPSE_JDT_CORE_PREFS = "org.eclipse.jdt.core.prefs"; |
53 |
|
|
54 |
|
private static final String PROP_ECLIPSE_PREFERENCES_VERSION = "eclipse.preferences.version"; |
55 |
|
|
56 |
|
private static final String DIR_DOT_SETTINGS = ".settings"; |
57 |
|
|
58 |
|
private static final String PROP_JDT_CORE_COMPILER_COMPLIANCE = "org.eclipse.jdt.core.compiler.compliance"; |
59 |
|
|
60 |
|
private static final String PROP_JDT_CORE_COMPILER_SOURCE = "org.eclipse.jdt.core.compiler.source"; |
61 |
|
|
62 |
|
private static final String ARTIFACT_MAVEN_COMPILER_PLUGIN = "maven-compiler-plugin"; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
public void write() |
68 |
|
throws MojoExecutionException |
69 |
|
{ |
70 |
|
|
71 |
|
|
72 |
12 |
Properties coreSettings = new Properties(); |
73 |
|
|
74 |
12 |
String source = IdeUtils.getPluginSetting( config.getProject(), ARTIFACT_MAVEN_COMPILER_PLUGIN, |
75 |
|
PROPERTY_SOURCE, null ); |
76 |
12 |
String target = IdeUtils.getPluginSetting( config.getProject(), ARTIFACT_MAVEN_COMPILER_PLUGIN, |
77 |
|
PROPERTY_TARGET, null ); |
78 |
|
|
79 |
12 |
if ( source != null ) |
80 |
|
{ |
81 |
0 |
coreSettings.put( PROP_JDT_CORE_COMPILER_SOURCE, source ); |
82 |
0 |
coreSettings.put( PROP_JDT_CORE_COMPILER_COMPLIANCE, source ); |
83 |
|
} |
84 |
|
|
85 |
12 |
if ( target != null && !JDK_1_2_SOURCES.equals( target ) ) |
86 |
|
{ |
87 |
0 |
coreSettings.put( "org.eclipse.jdt.core.compiler.codegen.targetPlatform", target ); |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
12 |
if ( !coreSettings.isEmpty() ) |
92 |
|
{ |
93 |
0 |
File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_DOT_SETTINGS ); |
94 |
|
|
95 |
0 |
settingsDir.mkdirs(); |
96 |
|
|
97 |
0 |
coreSettings.put( PROP_ECLIPSE_PREFERENCES_VERSION, "1" ); |
98 |
|
|
99 |
|
try |
100 |
|
{ |
101 |
|
File oldCoreSettingsFile; |
102 |
|
|
103 |
0 |
File coreSettingsFile = new File( settingsDir, FILE_ECLIPSE_JDT_CORE_PREFS ); |
104 |
|
|
105 |
0 |
if ( coreSettingsFile.exists() ) |
106 |
|
{ |
107 |
0 |
oldCoreSettingsFile = coreSettingsFile; |
108 |
|
|
109 |
0 |
Properties oldsettings = new Properties(); |
110 |
0 |
oldsettings.load( new FileInputStream( oldCoreSettingsFile ) ); |
111 |
|
|
112 |
0 |
Properties newsettings = (Properties) oldsettings.clone(); |
113 |
0 |
newsettings.putAll( coreSettings ); |
114 |
|
|
115 |
0 |
if ( !oldsettings.equals( newsettings ) ) |
116 |
|
{ |
117 |
0 |
newsettings.store( new FileOutputStream( coreSettingsFile ), null ); |
118 |
|
} |
119 |
0 |
} |
120 |
|
else |
121 |
|
{ |
122 |
0 |
coreSettings.store( new FileOutputStream( coreSettingsFile ), null ); |
123 |
|
|
124 |
0 |
log.info( Messages.getString( "EclipseSettingsWriter.wrotesettings", |
125 |
|
coreSettingsFile.getCanonicalPath() ) ); |
126 |
|
} |
127 |
|
} |
128 |
0 |
catch ( FileNotFoundException e ) |
129 |
|
{ |
130 |
0 |
throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.cannotcreatesettings" ), e ); |
131 |
|
} |
132 |
0 |
catch ( IOException e ) |
133 |
|
{ |
134 |
0 |
throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.errorwritingsettings" ), e ); |
135 |
0 |
} |
136 |
0 |
} |
137 |
|
else |
138 |
|
{ |
139 |
12 |
log.info( Messages.getString( "EclipseSettingsWriter.usingdefaults" ) ); |
140 |
|
} |
141 |
12 |
} |
142 |
|
} |