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.FileOutputStream; |
24 | |
import java.io.IOException; |
25 | |
import java.io.InputStreamReader; |
26 | |
import java.io.OutputStreamWriter; |
27 | |
import java.io.Reader; |
28 | |
import java.io.Writer; |
29 | |
import java.util.ArrayList; |
30 | |
import java.util.Iterator; |
31 | |
import java.util.LinkedHashSet; |
32 | |
import java.util.List; |
33 | |
import java.util.Set; |
34 | |
|
35 | |
import org.apache.maven.model.Resource; |
36 | |
import org.apache.maven.plugin.MojoExecutionException; |
37 | |
import org.apache.maven.plugin.eclipse.BuildCommand; |
38 | |
import org.apache.maven.plugin.eclipse.Messages; |
39 | |
import org.apache.maven.plugin.ide.IdeDependency; |
40 | |
import org.apache.maven.plugin.ide.IdeUtils; |
41 | |
import org.codehaus.plexus.util.IOUtil; |
42 | |
import org.codehaus.plexus.util.StringUtils; |
43 | |
import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; |
44 | |
import org.codehaus.plexus.util.xml.XMLWriter; |
45 | |
import org.codehaus.plexus.util.xml.Xpp3Dom; |
46 | |
import org.codehaus.plexus.util.xml.Xpp3DomBuilder; |
47 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | 0 | public class EclipseProjectWriter |
58 | |
extends AbstractEclipseWriter |
59 | |
{ |
60 | |
private static final String ELT_NAME = "name"; |
61 | |
|
62 | |
private static final String ELT_BUILD_COMMAND = "buildCommand"; |
63 | |
|
64 | |
private static final String ELT_BUILD_SPEC = "buildSpec"; |
65 | |
|
66 | |
private static final String ELT_NATURE = "nature"; |
67 | |
|
68 | |
private static final String ELT_NATURES = "natures"; |
69 | |
|
70 | |
private static final String FILE_DOT_PROJECT = ".project"; |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
private static final int LINK_TYPE_FILE = 1; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
private static final int LINK_TYPE_DIRECTORY = 2; |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public void write() |
86 | |
throws MojoExecutionException |
87 | |
{ |
88 | |
|
89 | 0 | Set projectnatures = new LinkedHashSet(); |
90 | 0 | Set buildCommands = new LinkedHashSet(); |
91 | |
|
92 | 0 | File dotProject = new File( config.getEclipseProjectDirectory(), FILE_DOT_PROJECT ); |
93 | |
|
94 | 0 | if ( dotProject.exists() ) |
95 | |
{ |
96 | |
|
97 | 0 | log.info( Messages.getString( "EclipsePlugin.keepexisting", dotProject.getAbsolutePath() ) ); |
98 | |
|
99 | |
|
100 | 0 | Reader reader = null; |
101 | |
try |
102 | |
{ |
103 | 0 | reader = new InputStreamReader( new FileInputStream( dotProject ), "UTF-8" ); |
104 | 0 | Xpp3Dom dom = Xpp3DomBuilder.build( reader ); |
105 | |
|
106 | 0 | Xpp3Dom naturesElement = dom.getChild( ELT_NATURES ); |
107 | 0 | if ( naturesElement != null ) |
108 | |
{ |
109 | 0 | Xpp3Dom[] existingNatures = naturesElement.getChildren( ELT_NATURE ); |
110 | 0 | for ( int j = 0; j < existingNatures.length; j++ ) |
111 | |
{ |
112 | |
|
113 | 0 | projectnatures.add( existingNatures[j].getValue() ); |
114 | |
} |
115 | |
} |
116 | |
|
117 | 0 | Xpp3Dom buildSpec = dom.getChild( ELT_BUILD_SPEC ); |
118 | 0 | if ( buildSpec != null ) |
119 | |
{ |
120 | 0 | Xpp3Dom[] existingBuildCommands = buildSpec.getChildren( ELT_BUILD_COMMAND ); |
121 | 0 | for ( int j = 0; j < existingBuildCommands.length; j++ ) |
122 | |
{ |
123 | 0 | Xpp3Dom buildCommandName = existingBuildCommands[j].getChild( ELT_NAME ); |
124 | 0 | if ( buildCommandName != null ) |
125 | |
{ |
126 | 0 | buildCommands.add( new BuildCommand( existingBuildCommands[j] ) ); |
127 | |
} |
128 | |
} |
129 | |
} |
130 | |
} |
131 | 0 | catch ( XmlPullParserException e ) |
132 | |
{ |
133 | 0 | log.warn( Messages.getString( "EclipsePlugin.cantparseexisting", dotProject.getAbsolutePath() ) ); |
134 | |
} |
135 | 0 | catch ( IOException e ) |
136 | |
{ |
137 | 0 | log.warn( Messages.getString( "EclipsePlugin.cantparseexisting", dotProject.getAbsolutePath() ) ); |
138 | |
} |
139 | |
finally |
140 | |
{ |
141 | 0 | IOUtil.close( reader ); |
142 | 0 | } |
143 | |
} |
144 | |
|
145 | |
|
146 | 0 | for ( Iterator iter = config.getProjectnatures().iterator(); iter.hasNext(); ) |
147 | |
{ |
148 | 0 | projectnatures.add( iter.next() ); |
149 | |
} |
150 | |
|
151 | 0 | for ( Iterator iter = config.getBuildCommands().iterator(); iter.hasNext(); ) |
152 | |
{ |
153 | 0 | buildCommands.add( (BuildCommand) iter.next() ); |
154 | |
} |
155 | |
|
156 | |
Writer w; |
157 | |
|
158 | |
try |
159 | |
{ |
160 | 0 | w = new OutputStreamWriter( new FileOutputStream( dotProject ), "UTF-8" ); |
161 | |
} |
162 | 0 | catch ( IOException ex ) |
163 | |
{ |
164 | 0 | throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); |
165 | 0 | } |
166 | |
|
167 | 0 | XMLWriter writer = new PrettyPrintXMLWriter( w ); |
168 | |
|
169 | 0 | writer.startElement( "projectDescription" ); |
170 | |
|
171 | 0 | writer.startElement( ELT_NAME ); |
172 | 0 | writer.writeText( config.getEclipseProjectName() ); |
173 | 0 | writer.endElement(); |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | 0 | writer.startElement( "comment" ); |
179 | |
|
180 | 0 | if ( config.getProject().getDescription() != null ) |
181 | |
{ |
182 | 0 | writer.writeText( config.getProject().getDescription() ); |
183 | |
} |
184 | |
|
185 | 0 | writer.endElement(); |
186 | |
|
187 | 0 | writer.startElement( "projects" ); |
188 | |
|
189 | |
|
190 | 0 | if ( !config.isPde() ) |
191 | |
{ |
192 | 0 | List duplicates = new ArrayList(); |
193 | 0 | for ( int j = 0; j < config.getDepsOrdered().length; j++ ) |
194 | |
{ |
195 | 0 | IdeDependency dep = config.getDepsOrdered()[j]; |
196 | |
|
197 | |
|
198 | 0 | if ( dep.isReferencedProject() && !duplicates.contains( dep.getEclipseProjectName() ) ) |
199 | |
{ |
200 | 0 | writer.startElement( "project" ); |
201 | 0 | writer.writeText( dep.getEclipseProjectName() ); |
202 | 0 | writer.endElement(); |
203 | 0 | duplicates.add( dep.getEclipseProjectName() ); |
204 | |
} |
205 | |
} |
206 | |
} |
207 | |
|
208 | 0 | writer.endElement(); |
209 | |
|
210 | 0 | writer.startElement( ELT_BUILD_SPEC ); |
211 | |
|
212 | 0 | for ( Iterator it = buildCommands.iterator(); it.hasNext(); ) |
213 | |
{ |
214 | 0 | ( (BuildCommand) it.next() ).print( writer ); |
215 | |
} |
216 | |
|
217 | 0 | writer.endElement(); |
218 | |
|
219 | 0 | writer.startElement( ELT_NATURES ); |
220 | |
|
221 | 0 | for ( Iterator it = projectnatures.iterator(); it.hasNext(); ) |
222 | |
{ |
223 | 0 | writer.startElement( ELT_NATURE ); |
224 | 0 | writer.writeText( (String) it.next() ); |
225 | 0 | writer.endElement(); |
226 | |
} |
227 | |
|
228 | 0 | writer.endElement(); |
229 | |
|
230 | 0 | boolean addLinks = !config.getProjectBaseDir().equals( config.getEclipseProjectDirectory() ); |
231 | |
|
232 | 0 | if ( addLinks || ( config.isPde() && config.getDepsOrdered().length > 0 ) ) |
233 | |
{ |
234 | 0 | writer.startElement( "linkedResources" ); |
235 | |
|
236 | 0 | if ( addLinks ) |
237 | |
{ |
238 | |
|
239 | 0 | addFileLink( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(), |
240 | |
config.getProject().getFile() ); |
241 | |
|
242 | 0 | addSourceLinks( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(), |
243 | |
config.getProject().getCompileSourceRoots() ); |
244 | 0 | addResourceLinks( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(), |
245 | |
config.getProject().getBuild().getResources() ); |
246 | |
|
247 | 0 | addSourceLinks( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(), |
248 | |
config.getProject().getTestCompileSourceRoots() ); |
249 | 0 | addResourceLinks( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(), |
250 | |
config.getProject().getBuild().getTestResources() ); |
251 | |
|
252 | |
} |
253 | |
|
254 | 0 | if ( config.isPde() ) |
255 | |
{ |
256 | 0 | for ( int j = 0; j < config.getDepsOrdered().length; j++ ) |
257 | |
{ |
258 | 0 | IdeDependency dep = config.getDepsOrdered()[j]; |
259 | |
|
260 | 0 | if ( dep.isAddedToClasspath() && !dep.isProvided() && !dep.isReferencedProject() |
261 | |
&& !dep.isTestDependency() && !dep.isOsgiBundle() ) |
262 | |
{ |
263 | 0 | String name = dep.getFile().getName(); |
264 | 0 | addLink( writer, name, StringUtils.replace( IdeUtils.getCanonicalPath( dep.getFile() ), "\\", |
265 | |
"/" ), LINK_TYPE_FILE ); |
266 | |
} |
267 | |
} |
268 | |
} |
269 | |
|
270 | 0 | writer.endElement(); |
271 | |
} |
272 | |
|
273 | 0 | writer.endElement(); |
274 | |
|
275 | 0 | IOUtil.close( w ); |
276 | 0 | } |
277 | |
|
278 | |
private void addFileLink( XMLWriter writer, File projectBaseDir, File basedir, File file ) |
279 | |
throws MojoExecutionException |
280 | |
{ |
281 | 0 | if ( file.isFile() ) |
282 | |
{ |
283 | 0 | String name = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, file, true ); |
284 | 0 | String location = IdeUtils.getCanonicalPath( file ).replaceAll( "\\\\", "/" ); |
285 | |
|
286 | 0 | addLink( writer, name, location, LINK_TYPE_FILE ); |
287 | |
} |
288 | |
else |
289 | |
{ |
290 | 0 | log.warn( Messages.getString( "EclipseProjectWriter.notafile", file ) ); |
291 | |
} |
292 | 0 | } |
293 | |
|
294 | |
private void addSourceLinks( XMLWriter writer, File projectBaseDir, File basedir, List sourceRoots ) |
295 | |
throws MojoExecutionException |
296 | |
{ |
297 | 0 | for ( Iterator it = sourceRoots.iterator(); it.hasNext(); ) |
298 | |
{ |
299 | 0 | String sourceRootString = (String) it.next(); |
300 | 0 | File sourceRoot = new File( sourceRootString ); |
301 | |
|
302 | 0 | if ( sourceRoot.isDirectory() ) |
303 | |
{ |
304 | 0 | String name = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, sourceRoot, true ); |
305 | 0 | String location = IdeUtils.getCanonicalPath( sourceRoot ).replaceAll( "\\\\", "/" ); |
306 | |
|
307 | 0 | addLink( writer, name, location, LINK_TYPE_DIRECTORY ); |
308 | |
} |
309 | |
} |
310 | 0 | } |
311 | |
|
312 | |
private void addResourceLinks( XMLWriter writer, File projectBaseDir, File basedir, List sourceRoots ) |
313 | |
throws MojoExecutionException |
314 | |
{ |
315 | 0 | for ( Iterator it = sourceRoots.iterator(); it.hasNext(); ) |
316 | |
{ |
317 | 0 | String resourceDirString = ( (Resource) it.next() ).getDirectory(); |
318 | 0 | File resourceDir = new File( resourceDirString ); |
319 | |
|
320 | 0 | if ( resourceDir.isDirectory() ) |
321 | |
{ |
322 | 0 | String name = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, resourceDir, true ); |
323 | 0 | String location = IdeUtils.getCanonicalPath( resourceDir ).replaceAll( "\\\\", "/" ); |
324 | |
|
325 | 0 | addLink( writer, name, location, LINK_TYPE_DIRECTORY ); |
326 | |
} |
327 | |
} |
328 | 0 | } |
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
private void addLink( XMLWriter writer, String name, String location, int type ) |
336 | |
{ |
337 | 0 | writer.startElement( "link" ); |
338 | |
|
339 | 0 | writer.startElement( ELT_NAME ); |
340 | 0 | writer.writeText( name ); |
341 | 0 | writer.endElement(); |
342 | |
|
343 | 0 | writer.startElement( "type" ); |
344 | 0 | writer.writeText( Integer.toString( type ) ); |
345 | 0 | writer.endElement(); |
346 | |
|
347 | 0 | writer.startElement( "location" ); |
348 | |
|
349 | 0 | writer.writeText( location ); |
350 | |
|
351 | 0 | writer.endElement(); |
352 | |
|
353 | 0 | writer.endElement(); |
354 | 0 | } |
355 | |
} |