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