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 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 DIR_DOT_SETTINGS = ".settings";
36
37
38
39
40 public static final String ECLIPSE_JDT_CORE_PREFS_FILE = "org.eclipse.jdt.core.prefs";
41
42
43
44
45 public static final String CLASSPATH_VARIABLE_M2_REPO = "org.eclipse.jdt.core.classpathVariable.M2_REPO";
46
47
48
49
50 public static final String ECLIPSE_JDT_UI_PREFS_FILE = "org.eclipse.jdt.ui.prefs";
51
52 private WorkspaceConfiguration config;
53
54 private Log logger;
55
56 private File workDir;
57
58 public WorkspaceWriter init( Log logger, WorkspaceConfiguration config )
59 {
60 this.logger = logger;
61 this.config = config;
62
63 workDir = new File( config.getWorkspaceDirectory(), ECLIPSE_CORE_RUNTIME_SETTINGS_DIR );
64 workDir.mkdirs();
65
66 return this;
67 }
68
69 public void write()
70 throws MojoExecutionException
71 {
72 this.writeLocalRepositoryConfiguration();
73
74 if ( config.getCodeStylesURL() != null )
75 {
76 this.writeCodeStyleConfiguration();
77 }
78 }
79
80 private void writeCodeStyleConfiguration()
81 throws MojoExecutionException
82 {
83 File f = new File( workDir, ECLIPSE_JDT_UI_PREFS_FILE );
84
85 Properties props = loadProperties( f );
86
87 EclipseCodeFormatterProfile codeFormatter =
88 new EclipseCodeFormatterProfile().init( config.getCodeStylesURL(), config.getActiveStyleProfileName() );
89
90 if ( codeFormatter.getProfileName() != null )
91 {
92 logger.info( "Set active code style profile name: " + codeFormatter.getProfileName() );
93 props.setProperty( "formatter_profile", "_" + codeFormatter.getProfileName() );
94 }
95
96 props.setProperty( "org.eclipse.jdt.ui.formatterprofiles", codeFormatter.getContent() );
97
98 storeProperties( props, f );
99 }
100
101 private void writeLocalRepositoryConfiguration()
102 throws MojoExecutionException
103 {
104 File f = new File( workDir, ECLIPSE_JDT_CORE_PREFS_FILE );
105
106 Properties props = loadProperties( f );
107
108 props.put( CLASSPATH_VARIABLE_M2_REPO, config.getLocalRepository().getBasedir() );
109
110 storeProperties( props, f );
111 }
112
113 private static Properties loadProperties( File f )
114 throws MojoExecutionException
115 {
116 Properties props = new Properties();
117
118
119 if ( f.exists() )
120 {
121 try
122 {
123 props.load( new FileInputStream( f ) );
124 }
125 catch ( FileNotFoundException e )
126 {
127 throw new MojoExecutionException(
128 Messages.getString( "EclipsePlugin.cantreadfile", f.getAbsolutePath() ), e );
129 }
130 catch ( IOException e )
131 {
132 throw new MojoExecutionException(
133 Messages.getString( "EclipsePlugin.cantreadfile", f.getAbsolutePath() ), e );
134 }
135 }
136
137 return props;
138 }
139
140 private static void storeProperties( Properties props, File f )
141 throws MojoExecutionException
142 {
143 OutputStream os = null;
144
145 try
146 {
147 os = new FileOutputStream( f );
148 props.store( os, null );
149 }
150 catch ( IOException ioe )
151 {
152 throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantwritetofile",
153 f.getAbsolutePath() ) );
154 }
155 finally
156 {
157 IOUtil.close( os );
158 }
159 }
160 }