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