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