1 | |
package org.apache.maven.plugin.eclipse.writers.workspace; |
2 | |
|
3 | |
import java.io.File; |
4 | |
import java.io.FileInputStream; |
5 | |
import java.io.FileNotFoundException; |
6 | |
import java.io.FileOutputStream; |
7 | |
import java.io.IOException; |
8 | |
import java.io.OutputStream; |
9 | |
import java.util.Properties; |
10 | |
|
11 | |
import org.apache.maven.plugin.MojoExecutionException; |
12 | |
import org.apache.maven.plugin.eclipse.Messages; |
13 | |
import org.apache.maven.plugin.eclipse.WorkspaceConfiguration; |
14 | |
import org.apache.maven.plugin.logging.Log; |
15 | |
import org.codehaus.plexus.util.IOUtil; |
16 | |
|
17 | 0 | public class EclipseWorkspaceWriter |
18 | |
implements WorkspaceWriter |
19 | |
{ |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
public static final String ECLIPSE_PLUGINS_METADATA_DIR = ".metadata/.plugins"; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public static final String ECLIPSE_CORE_RUNTIME_SETTINGS_DIR = |
30 | |
ECLIPSE_PLUGINS_METADATA_DIR + "/org.eclipse.core.runtime/.settings"; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public static final String ECLIPSE_JDT_CORE_PREFS_FILE = "org.eclipse.jdt.core.prefs"; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public static final String CLASSPATH_VARIABLE_M2_REPO = "org.eclipse.jdt.core.classpathVariable.M2_REPO"; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public static final String ECLIPSE_JDT_UI_PREFS_FILE = "org.eclipse.jdt.ui.prefs"; |
46 | |
|
47 | |
private WorkspaceConfiguration config; |
48 | |
|
49 | |
private Log logger; |
50 | |
|
51 | |
private File workDir; |
52 | |
|
53 | |
public WorkspaceWriter init( Log logger, WorkspaceConfiguration config ) |
54 | |
{ |
55 | 0 | this.logger = logger; |
56 | 0 | this.config = config; |
57 | |
|
58 | 0 | workDir = new File( config.getWorkspaceDirectory(), ECLIPSE_CORE_RUNTIME_SETTINGS_DIR ); |
59 | 0 | workDir.mkdirs(); |
60 | |
|
61 | 0 | return this; |
62 | |
} |
63 | |
|
64 | |
public void write() |
65 | |
throws MojoExecutionException |
66 | |
{ |
67 | 0 | this.writeLocalRepositoryConfiguration(); |
68 | |
|
69 | 0 | if ( config.getCodeStylesURL() != null ) |
70 | |
{ |
71 | 0 | this.writeCodeStyleConfiguration(); |
72 | |
} |
73 | 0 | } |
74 | |
|
75 | |
private void writeCodeStyleConfiguration() |
76 | |
throws MojoExecutionException |
77 | |
{ |
78 | 0 | File f = new File( workDir, ECLIPSE_JDT_UI_PREFS_FILE ); |
79 | |
|
80 | 0 | Properties props = loadProperties( f ); |
81 | |
|
82 | 0 | EclipseCodeFormatterProfile codeFormatter = |
83 | |
new EclipseCodeFormatterProfile().init( config.getCodeStylesURL(), config.getActiveStyleProfileName() ); |
84 | |
|
85 | 0 | if ( codeFormatter.getProfileName() != null ) |
86 | |
{ |
87 | 0 | logger.info( "Set active code style profile name: " + codeFormatter.getProfileName() ); |
88 | 0 | props.setProperty( "formatter_profile", "_" + codeFormatter.getProfileName() ); |
89 | |
} |
90 | |
|
91 | 0 | props.setProperty( "org.eclipse.jdt.ui.formatterprofiles", codeFormatter.getContent() ); |
92 | |
|
93 | 0 | storeProperties( props, f ); |
94 | 0 | } |
95 | |
|
96 | |
private void writeLocalRepositoryConfiguration() |
97 | |
throws MojoExecutionException |
98 | |
{ |
99 | 0 | File f = new File( workDir, ECLIPSE_JDT_CORE_PREFS_FILE ); |
100 | |
|
101 | 0 | Properties props = loadProperties( f ); |
102 | |
|
103 | 0 | props.put( CLASSPATH_VARIABLE_M2_REPO, config.getLocalRepository().getBasedir() ); |
104 | |
|
105 | 0 | storeProperties( props, f ); |
106 | 0 | } |
107 | |
|
108 | |
private static Properties loadProperties( File f ) |
109 | |
throws MojoExecutionException |
110 | |
{ |
111 | 0 | Properties props = new Properties(); |
112 | |
|
113 | |
|
114 | 0 | if ( f.exists() ) |
115 | |
{ |
116 | |
try |
117 | |
{ |
118 | 0 | props.load( new FileInputStream( f ) ); |
119 | |
} |
120 | 0 | catch ( FileNotFoundException e ) |
121 | |
{ |
122 | 0 | throw new MojoExecutionException( |
123 | |
Messages.getString( "EclipsePlugin.cantreadfile", f.getAbsolutePath() ), e ); |
124 | |
} |
125 | 0 | catch ( IOException e ) |
126 | |
{ |
127 | 0 | throw new MojoExecutionException( |
128 | |
Messages.getString( "EclipsePlugin.cantreadfile", f.getAbsolutePath() ), e ); |
129 | 0 | } |
130 | |
} |
131 | |
|
132 | 0 | return props; |
133 | |
} |
134 | |
|
135 | |
private static void storeProperties( Properties props, File f ) |
136 | |
throws MojoExecutionException |
137 | |
{ |
138 | 0 | OutputStream os = null; |
139 | |
|
140 | |
try |
141 | |
{ |
142 | 0 | os = new FileOutputStream( f ); |
143 | 0 | props.store( os, null ); |
144 | |
} |
145 | 0 | catch ( IOException ioe ) |
146 | |
{ |
147 | 0 | throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantwritetofile", |
148 | |
f.getAbsolutePath() ) ); |
149 | |
} |
150 | |
finally |
151 | |
{ |
152 | 0 | IOUtil.close( os ); |
153 | 0 | } |
154 | 0 | } |
155 | |
} |