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