1 | |
package org.apache.maven.model.converter; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.model.Model; |
23 | |
import org.apache.maven.model.converter.plugins.PluginConfigurationConverter; |
24 | |
import org.apache.maven.model.converter.relocators.PluginRelocator; |
25 | |
import org.apache.maven.model.converter.relocators.PluginRelocatorManager; |
26 | |
import org.apache.maven.model.io.xpp3.MavenXpp3Writer; |
27 | |
import org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader; |
28 | |
import org.codehaus.plexus.logging.AbstractLogEnabled; |
29 | |
import org.codehaus.plexus.util.IOUtil; |
30 | |
import org.dom4j.Document; |
31 | |
import org.dom4j.Element; |
32 | |
import org.dom4j.io.SAXReader; |
33 | |
|
34 | |
import java.io.File; |
35 | |
import java.io.FileInputStream; |
36 | |
import java.io.FileReader; |
37 | |
import java.io.FileWriter; |
38 | |
import java.io.IOException; |
39 | |
import java.io.InputStream; |
40 | |
import java.io.Writer; |
41 | |
import java.util.ArrayList; |
42 | |
import java.util.Collection; |
43 | |
import java.util.Collections; |
44 | |
import java.util.Iterator; |
45 | |
import java.util.List; |
46 | |
import java.util.Properties; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 0 | public class Maven1Converter |
57 | |
extends AbstractLogEnabled |
58 | |
{ |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
private List converters; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
private PluginRelocatorManager pluginRelocatorManager; |
72 | |
|
73 | |
private File basedir; |
74 | |
|
75 | |
private File outputdir; |
76 | |
|
77 | 0 | private String fileName = "project.xml"; |
78 | |
|
79 | 0 | private List listeners = new ArrayList(); |
80 | |
|
81 | |
public void execute() |
82 | |
throws ProjectConverterException |
83 | |
{ |
84 | 0 | File projectxml = new File( basedir, fileName ); |
85 | |
|
86 | 0 | if ( !projectxml.exists() ) |
87 | |
{ |
88 | 0 | throw new ProjectConverterException( "Missing " + fileName + " in " + basedir.getAbsolutePath() ); |
89 | |
} |
90 | |
|
91 | 0 | PomV3ToV4Translator translator = new PomV3ToV4Translator(); |
92 | |
|
93 | |
org.apache.maven.model.v3_0_0.Model v3Model; |
94 | |
try |
95 | |
{ |
96 | 0 | v3Model = loadV3Pom( projectxml ); |
97 | |
} |
98 | 0 | catch ( Exception e ) |
99 | |
{ |
100 | 0 | throw new ProjectConverterException( "Exception caught while loading " + fileName + ". " + e.getMessage(), |
101 | |
e ); |
102 | 0 | } |
103 | |
|
104 | |
Model v4Model; |
105 | |
try |
106 | |
{ |
107 | 0 | v4Model = translator.translate( v3Model ); |
108 | 0 | removeDistributionManagementStatus( v4Model ); |
109 | |
} |
110 | 0 | catch ( Exception e ) |
111 | |
{ |
112 | 0 | throw new ProjectConverterException( |
113 | |
"Exception caught while converting " + fileName + ". " + e.getMessage(), e ); |
114 | 0 | } |
115 | |
|
116 | 0 | Properties properties = new Properties(); |
117 | |
|
118 | 0 | if ( v3Model.getExtend() != null ) |
119 | |
{ |
120 | 0 | loadProperties( properties, new File( new File( basedir, v3Model.getExtend() ).getParentFile(), |
121 | |
"project.properties" ) ); |
122 | |
} |
123 | |
|
124 | 0 | loadProperties( properties, new File( basedir, "project.properties" ) ); |
125 | |
|
126 | 0 | for ( Iterator i = converters.iterator(); i.hasNext(); ) |
127 | |
{ |
128 | 0 | PluginConfigurationConverter converter = (PluginConfigurationConverter) i.next(); |
129 | 0 | converter.addListeners( listeners ); |
130 | 0 | converter.convertConfiguration( v4Model, v3Model, properties ); |
131 | |
} |
132 | |
|
133 | |
|
134 | 0 | Collection pluginRelocators = pluginRelocatorManager.getPluginRelocators(); |
135 | 0 | sendInfoMessage( "There are " + pluginRelocators.size() + " plugin relocators available" ); |
136 | |
PluginRelocator pluginRelocator; |
137 | 0 | Iterator iterator = pluginRelocators.iterator(); |
138 | 0 | while ( iterator.hasNext() ) |
139 | |
{ |
140 | 0 | pluginRelocator = (PluginRelocator) iterator.next(); |
141 | 0 | pluginRelocator.addListeners( listeners ); |
142 | 0 | pluginRelocator.relocate( v4Model ); |
143 | |
} |
144 | |
|
145 | |
|
146 | 0 | if ( v4Model.getBuild() != null ) |
147 | |
{ |
148 | 0 | Collections.sort( v4Model.getBuild().getPlugins(), new PluginComparator() ); |
149 | |
} |
150 | 0 | if ( v4Model.getReporting() != null ) |
151 | |
{ |
152 | 0 | Collections.sort( v4Model.getReporting().getPlugins(), new ReportPluginComparator() ); |
153 | |
} |
154 | |
|
155 | |
try |
156 | |
{ |
157 | 0 | writeV4Pom( v4Model ); |
158 | |
} |
159 | 0 | catch ( IOException e ) |
160 | |
{ |
161 | 0 | throw new ProjectConverterException( "Failed to write the pom.xml.", e ); |
162 | 0 | } |
163 | 0 | } |
164 | |
|
165 | |
private boolean isEmpty( String value ) |
166 | |
{ |
167 | 0 | return value == null || value.trim().length() == 0; |
168 | |
} |
169 | |
|
170 | |
private void loadProperties( Properties properties, File propertiesFile ) |
171 | |
{ |
172 | 0 | if ( propertiesFile.exists() ) |
173 | |
{ |
174 | 0 | InputStream is = null; |
175 | |
try |
176 | |
{ |
177 | 0 | is = new FileInputStream( propertiesFile ); |
178 | 0 | properties.load( is ); |
179 | |
} |
180 | 0 | catch ( IOException e ) |
181 | |
{ |
182 | 0 | sendWarnMessage( "Unable to read " + propertiesFile.getAbsolutePath() + ", ignoring." ); |
183 | |
} |
184 | |
finally |
185 | |
{ |
186 | 0 | IOUtil.close( is ); |
187 | 0 | } |
188 | |
} |
189 | 0 | } |
190 | |
|
191 | |
private org.apache.maven.model.v3_0_0.Model loadV3Pom( File inputFile ) |
192 | |
throws Exception |
193 | |
{ |
194 | 0 | MavenXpp3Reader v3Reader = new MavenXpp3Reader(); |
195 | |
|
196 | |
org.apache.maven.model.v3_0_0.Model model; |
197 | |
|
198 | 0 | model = v3Reader.read( new FileReader( inputFile ) ); |
199 | |
|
200 | 0 | SAXReader r = new SAXReader(); |
201 | |
|
202 | 0 | Document d = r.read( new FileReader( inputFile ) ); |
203 | |
|
204 | 0 | Element root = d.getRootElement(); |
205 | |
|
206 | 0 | Element idElement = root.element( "id" ); |
207 | |
|
208 | 0 | String id = null; |
209 | |
|
210 | 0 | if ( idElement != null ) |
211 | |
{ |
212 | 0 | id = idElement.getText(); |
213 | |
} |
214 | |
|
215 | |
|
216 | 0 | String groupId = model.getGroupId(); |
217 | |
|
218 | 0 | String artifactId = model.getArtifactId(); |
219 | |
|
220 | 0 | if ( !isEmpty( id ) ) |
221 | |
{ |
222 | 0 | int i = id.indexOf( "+" ); |
223 | |
|
224 | 0 | int j = id.indexOf( ":" ); |
225 | |
|
226 | 0 | if ( i > 0 ) |
227 | |
{ |
228 | 0 | model.setGroupId( id.substring( 0, i ) ); |
229 | |
|
230 | 0 | model.setArtifactId( id.replace( '+', '-' ) ); |
231 | |
} |
232 | 0 | else if ( j > 0 ) |
233 | |
{ |
234 | 0 | model.setGroupId( id.substring( 0, j ) ); |
235 | |
|
236 | 0 | model.setArtifactId( id.substring( j + 1 ) ); |
237 | |
} |
238 | |
else |
239 | |
{ |
240 | 0 | model.setGroupId( id ); |
241 | |
|
242 | 0 | model.setArtifactId( id ); |
243 | |
} |
244 | |
|
245 | 0 | if ( !isEmpty( groupId ) ) |
246 | |
{ |
247 | 0 | sendWarnMessage( "Both <id> and <groupId> is set, using <groupId>." ); |
248 | |
|
249 | 0 | model.setGroupId( groupId ); |
250 | |
} |
251 | |
|
252 | 0 | if ( !isEmpty( artifactId ) ) |
253 | |
{ |
254 | 0 | sendWarnMessage( "Both <id> and <artifactId> is set, using <artifactId>." ); |
255 | |
|
256 | 0 | model.setArtifactId( artifactId ); |
257 | |
} |
258 | |
} |
259 | |
|
260 | 0 | return model; |
261 | |
} |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
private void removeDistributionManagementStatus( Model v4Model ) |
268 | |
{ |
269 | 0 | if ( v4Model.getDistributionManagement() != null ) |
270 | |
{ |
271 | 0 | if ( "converted".equals( v4Model.getDistributionManagement().getStatus() ) ) |
272 | |
{ |
273 | 0 | v4Model.getDistributionManagement().setStatus( null ); |
274 | |
} |
275 | |
} |
276 | 0 | } |
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
private void writeV4Pom( Model v4Model ) |
286 | |
throws ProjectConverterException, IOException |
287 | |
{ |
288 | 0 | if ( outputdir == null ) |
289 | |
{ |
290 | 0 | outputdir = basedir; |
291 | |
} |
292 | |
|
293 | 0 | if ( !outputdir.exists() && !outputdir.mkdirs() ) |
294 | |
{ |
295 | 0 | throw new IOException( "Failed to create directory " + outputdir ); |
296 | |
} |
297 | |
|
298 | 0 | File pomxml = new File( outputdir, "pom.xml" ); |
299 | 0 | boolean alreadyExist = false; |
300 | |
|
301 | 0 | if ( pomxml.exists() ) |
302 | |
{ |
303 | 0 | sendWarnMessage( "pom.xml in " + outputdir.getAbsolutePath() + " already exists, overwriting" ); |
304 | 0 | alreadyExist = true; |
305 | |
} |
306 | |
|
307 | 0 | MavenXpp3Writer v4Writer = new MavenXpp3Writer(); |
308 | |
|
309 | |
|
310 | 0 | sendInfoMessage( "Writing new pom to: " + pomxml.getAbsolutePath() ); |
311 | |
|
312 | 0 | Writer output = null; |
313 | |
try |
314 | |
{ |
315 | 0 | output = new FileWriter( pomxml ); |
316 | 0 | v4Writer.write( output, v4Model ); |
317 | 0 | output.close(); |
318 | 0 | fireSavePomEvent( pomxml, alreadyExist ); |
319 | |
} |
320 | 0 | catch ( IOException e ) |
321 | |
{ |
322 | 0 | throw new ProjectConverterException( "Unable to write pom.xml. " + e.getMessage(), e ); |
323 | |
} |
324 | |
finally |
325 | |
{ |
326 | 0 | IOUtil.close( output ); |
327 | 0 | } |
328 | 0 | } |
329 | |
|
330 | |
public File getBasedir() |
331 | |
{ |
332 | 0 | return basedir; |
333 | |
} |
334 | |
|
335 | |
public void setBasedir( File basedir ) |
336 | |
{ |
337 | 0 | this.basedir = basedir; |
338 | 0 | } |
339 | |
|
340 | |
public String getProjectFileName() |
341 | |
{ |
342 | 0 | return fileName; |
343 | |
} |
344 | |
|
345 | |
public void setProjectFileName( String projectFileName ) |
346 | |
{ |
347 | 0 | this.fileName = projectFileName; |
348 | 0 | } |
349 | |
|
350 | |
public void setProjectFile( File projectFile ) |
351 | |
{ |
352 | 0 | if ( projectFile != null ) |
353 | |
{ |
354 | 0 | basedir = projectFile.getParentFile(); |
355 | 0 | fileName = projectFile.getName(); |
356 | |
} |
357 | 0 | } |
358 | |
|
359 | |
public File getOutputdir() |
360 | |
{ |
361 | 0 | return outputdir; |
362 | |
} |
363 | |
|
364 | |
public void setOutputdir( File outputdir ) |
365 | |
{ |
366 | 0 | this.outputdir = outputdir; |
367 | 0 | } |
368 | |
|
369 | |
public void addListener( ConverterListener listener ) |
370 | |
{ |
371 | 0 | if ( !listeners.contains( listener ) ) |
372 | |
{ |
373 | 0 | listeners.add( listener ); |
374 | |
} |
375 | 0 | } |
376 | |
|
377 | |
private void sendInfoMessage( String message ) |
378 | |
{ |
379 | 0 | getLogger().info( message ); |
380 | |
|
381 | 0 | for ( Iterator i = listeners.iterator(); i.hasNext(); ) |
382 | |
{ |
383 | 0 | ConverterListener listener = (ConverterListener) i.next(); |
384 | 0 | listener.info( message ); |
385 | |
} |
386 | 0 | } |
387 | |
|
388 | |
private void sendWarnMessage( String message ) |
389 | |
{ |
390 | 0 | getLogger().warn( message ); |
391 | |
|
392 | 0 | for ( Iterator i = listeners.iterator(); i.hasNext(); ) |
393 | |
{ |
394 | 0 | ConverterListener listener = (ConverterListener) i.next(); |
395 | 0 | listener.warn( message ); |
396 | |
} |
397 | 0 | } |
398 | |
|
399 | |
private void fireSavePomEvent( File pomFile, boolean alreadyExist ) |
400 | |
{ |
401 | 0 | for ( Iterator i = listeners.iterator(); i.hasNext(); ) |
402 | |
{ |
403 | 0 | ConverterListener listener = (ConverterListener) i.next(); |
404 | 0 | listener.savePomEvent( pomFile, alreadyExist ); |
405 | |
} |
406 | 0 | } |
407 | |
} |