1 | |
package org.apache.maven.shared.test.plugin; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.io.Reader; |
25 | |
import java.io.Writer; |
26 | |
import java.util.ArrayList; |
27 | |
import java.util.Iterator; |
28 | |
import java.util.List; |
29 | |
import java.util.Properties; |
30 | |
|
31 | |
import org.apache.maven.artifact.Artifact; |
32 | |
import org.apache.maven.artifact.UnknownRepositoryLayoutException; |
33 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
34 | |
import org.apache.maven.artifact.handler.ArtifactHandler; |
35 | |
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; |
36 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
37 | |
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; |
38 | |
import org.apache.maven.model.Build; |
39 | |
import org.apache.maven.model.DeploymentRepository; |
40 | |
import org.apache.maven.model.DistributionManagement; |
41 | |
import org.apache.maven.model.Model; |
42 | |
import org.apache.maven.model.Plugin; |
43 | |
import org.apache.maven.model.Repository; |
44 | |
import org.apache.maven.model.Site; |
45 | |
import org.apache.maven.model.io.xpp3.MavenXpp3Reader; |
46 | |
import org.apache.maven.model.io.xpp3.MavenXpp3Writer; |
47 | |
import org.apache.maven.project.DefaultProjectBuildingRequest; |
48 | |
import org.apache.maven.project.MavenProject; |
49 | |
import org.apache.maven.project.ProjectBuilder; |
50 | |
import org.apache.maven.project.ProjectBuildingException; |
51 | |
import org.apache.maven.project.ProjectBuildingRequest; |
52 | |
import org.apache.maven.project.artifact.ProjectArtifactMetadata; |
53 | |
import org.apache.maven.repository.internal.MavenRepositorySystemSession; |
54 | |
import org.codehaus.plexus.component.annotations.Component; |
55 | |
import org.codehaus.plexus.component.annotations.Requirement; |
56 | |
import org.codehaus.plexus.util.FileUtils; |
57 | |
import org.codehaus.plexus.util.IOUtil; |
58 | |
import org.codehaus.plexus.util.ReaderFactory; |
59 | |
import org.codehaus.plexus.util.WriterFactory; |
60 | |
import org.codehaus.plexus.util.xml.Xpp3Dom; |
61 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
@Component( role = ProjectTool.class ) |
72 | 6 | public class ProjectTool |
73 | |
{ |
74 | |
|
75 | 2 | public static final String ROLE = ProjectTool.class.getName(); |
76 | |
|
77 | |
public static final String INTEGRATION_TEST_DEPLOYMENT_REPO_URL = "integration-test.deployment.repo.url"; |
78 | |
|
79 | |
@Requirement |
80 | |
private BuildTool buildTool; |
81 | |
|
82 | |
@Requirement |
83 | |
private RepositoryTool repositoryTool; |
84 | |
|
85 | |
@Requirement |
86 | |
private ProjectBuilder projectBuilder; |
87 | |
|
88 | |
@Requirement |
89 | |
private ArtifactHandlerManager artifactHandlerManager; |
90 | |
|
91 | |
@Requirement |
92 | |
private ArtifactFactory artifactFactory; |
93 | |
|
94 | |
@Requirement |
95 | |
private ArtifactRepositoryFactory artifactRepositoryFactory; |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public MavenProject readProject( File pomFile ) |
105 | |
throws TestToolsException |
106 | |
{ |
107 | 0 | return readProject( pomFile, repositoryTool.findLocalRepositoryDirectory() ); |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
public MavenProject readProject( File pomFile, File localRepositoryBasedir ) |
120 | |
throws TestToolsException |
121 | |
{ |
122 | |
try |
123 | |
{ |
124 | 0 | ArtifactRepository localRepository = repositoryTool |
125 | |
.createLocalArtifactRepositoryInstance( localRepositoryBasedir ); |
126 | |
|
127 | 0 | ProjectBuildingRequest request = new DefaultProjectBuildingRequest(); |
128 | 0 | return projectBuilder.build( pomFile, request ).getProject(); |
129 | |
} |
130 | 0 | catch ( ProjectBuildingException e ) |
131 | |
{ |
132 | 0 | throw new TestToolsException( "Error building MavenProject instance from test pom: " + pomFile, e ); |
133 | |
} |
134 | |
} |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
public MavenProject readProjectWithDependencies( File pomFile ) |
144 | |
throws TestToolsException |
145 | |
{ |
146 | 0 | return readProjectWithDependencies( pomFile, repositoryTool.findLocalRepositoryDirectory() ); |
147 | |
} |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public MavenProject readProjectWithDependencies( File pomFile, File localRepositoryBasedir ) |
159 | |
throws TestToolsException |
160 | |
{ |
161 | |
try |
162 | |
{ |
163 | 0 | ArtifactRepository localRepository = repositoryTool |
164 | |
.createLocalArtifactRepositoryInstance( localRepositoryBasedir ); |
165 | |
|
166 | 0 | ProjectBuildingRequest request = new DefaultProjectBuildingRequest(); |
167 | 0 | return projectBuilder.build( pomFile, request ).getProject(); |
168 | |
} |
169 | 0 | catch ( ProjectBuildingException e ) |
170 | |
{ |
171 | 0 | throw new TestToolsException( "Error building MavenProject instance from test pom: " + pomFile, e ); |
172 | |
} |
173 | |
} |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
public MavenProject packageProjectArtifact( File pomFile, String testVersion, boolean skipUnitTests ) |
195 | |
throws TestToolsException |
196 | |
{ |
197 | 4 | return packageProjectArtifact( pomFile, testVersion, skipUnitTests, null ); |
198 | |
} |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
public MavenProject packageProjectArtifact( File pomFile, String testVersion, boolean skipUnitTests, File logFile ) |
222 | |
throws TestToolsException |
223 | |
{ |
224 | 4 | PomInfo pomInfo = manglePomForTesting( pomFile, testVersion, skipUnitTests ); |
225 | |
|
226 | 4 | Properties properties = new Properties(); |
227 | |
|
228 | 4 | List goals = new ArrayList(); |
229 | 4 | goals.add( "package" ); |
230 | |
|
231 | 4 | File buildLog = logFile == null ? pomInfo.getBuildLogFile() : logFile; |
232 | 4 | System.out.println( "Now Building test version of the plugin...\nUsing staged plugin-pom: " |
233 | |
+ pomInfo.getPomFile().getAbsolutePath() ); |
234 | |
|
235 | 4 | buildTool.executeMaven( pomInfo.getPomFile(), properties, goals, buildLog ); |
236 | |
|
237 | 4 | File artifactFile = new File( pomInfo.getPomFile().getParentFile(), |
238 | |
pomInfo.getBuildDirectory() + "/" + pomInfo.getFinalName() ); |
239 | 4 | System.out.println( "Using IT Plugin Jar: " + artifactFile.getAbsolutePath() ); |
240 | |
try |
241 | |
{ |
242 | 4 | ProjectBuildingRequest request = new DefaultProjectBuildingRequest(); |
243 | 4 | request.setLocalRepository( artifactRepositoryFactory.createArtifactRepository( "local", new File( "target/localrepo" ).getCanonicalFile().toURL().toExternalForm(), "default", null, null ) ); |
244 | 4 | request.setRepositorySession( new MavenRepositorySystemSession() ); |
245 | 4 | MavenProject project = projectBuilder.build( pomInfo.getPomFile(), request ).getProject(); |
246 | |
|
247 | 4 | Artifact artifact = artifactFactory.createArtifact( project.getGroupId(), project.getArtifactId(), project |
248 | |
.getVersion(), null, project.getPackaging() ); |
249 | |
|
250 | 4 | artifact.setFile( artifactFile ); |
251 | 4 | artifact.addMetadata( new ProjectArtifactMetadata( artifact, project.getFile() ) ); |
252 | |
|
253 | 4 | project.setArtifact( artifact ); |
254 | |
|
255 | 4 | return project; |
256 | |
} |
257 | 0 | catch ( ProjectBuildingException e ) |
258 | |
{ |
259 | 0 | throw new TestToolsException( |
260 | |
"Error building MavenProject instance from test pom: " + pomInfo.getPomFile(), |
261 | |
e ); |
262 | |
} |
263 | 0 | catch ( UnknownRepositoryLayoutException e ) |
264 | |
{ |
265 | 0 | throw new TestToolsException( |
266 | |
"Error building ArtifactRepository instance from test pom: " + pomInfo.getPomFile(), |
267 | |
e ); |
268 | |
} |
269 | 0 | catch ( IOException e ) |
270 | |
{ |
271 | 0 | throw new TestToolsException( |
272 | |
"Error building ArtifactRepository instance from test pom: " + pomInfo.getPomFile(), |
273 | |
e ); |
274 | |
} |
275 | |
} |
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
protected PomInfo manglePomForTesting( File pomFile, String testVersion, boolean skipUnitTests ) |
290 | |
throws TestToolsException |
291 | |
{ |
292 | 6 | File input = pomFile; |
293 | |
|
294 | 6 | File output = new File( pomFile.getParentFile(), "pom-" + testVersion + ".xml" ); |
295 | 6 | output.deleteOnExit(); |
296 | |
|
297 | 6 | Reader reader = null; |
298 | 6 | Writer writer = null; |
299 | |
|
300 | 6 | Model model = null; |
301 | 6 | String finalName = null; |
302 | 6 | String buildDirectory = null; |
303 | |
|
304 | |
try |
305 | |
{ |
306 | 6 | reader = ReaderFactory.newXmlReader( input ); |
307 | |
|
308 | 6 | model = new MavenXpp3Reader().read( reader ); |
309 | |
} |
310 | 0 | catch ( IOException e ) |
311 | |
{ |
312 | 0 | throw new TestToolsException( "Error creating test-time version of POM for: " + input, e ); |
313 | |
} |
314 | 0 | catch ( XmlPullParserException e ) |
315 | |
{ |
316 | 0 | throw new TestToolsException( "Error creating test-time version of POM for: " + input, e ); |
317 | |
} |
318 | |
finally |
319 | |
{ |
320 | 6 | IOUtil.close( reader ); |
321 | 6 | } |
322 | |
|
323 | |
try |
324 | |
{ |
325 | 6 | model.setVersion( testVersion ); |
326 | |
|
327 | 6 | Build build = model.getBuild(); |
328 | 6 | if ( build == null ) |
329 | |
{ |
330 | 6 | build = new Build(); |
331 | |
} |
332 | 6 | buildDirectory = build.getDirectory(); |
333 | |
|
334 | 6 | if ( buildDirectory == null ) |
335 | |
{ |
336 | 6 | buildDirectory = "target"; |
337 | |
} |
338 | |
|
339 | 6 | buildDirectory = ( buildDirectory + File.separatorChar + "it-build-target" ); |
340 | 6 | build.setDirectory( buildDirectory ); |
341 | 6 | build.setOutputDirectory( buildDirectory + File.separatorChar + "classes" ); |
342 | 6 | System.out.println( "Using " + build.getDirectory() + " and " + build.getOutputDirectory() |
343 | |
+ " to build IT version of plugin" ); |
344 | 6 | model.setBuild( build ); |
345 | |
|
346 | 6 | finalName = build.getFinalName(); |
347 | |
|
348 | 6 | if ( finalName == null ) |
349 | |
{ |
350 | 6 | ArtifactHandler handler = artifactHandlerManager.getArtifactHandler( model.getPackaging() ); |
351 | |
|
352 | 6 | String ext = handler.getExtension(); |
353 | |
|
354 | 6 | finalName = model.getArtifactId() + "-" + model.getVersion() + "." + ext; |
355 | |
} |
356 | |
|
357 | 6 | DistributionManagement distMgmt = new DistributionManagement(); |
358 | |
|
359 | 6 | DeploymentRepository deployRepo = new DeploymentRepository(); |
360 | |
|
361 | 6 | deployRepo.setId( "integration-test.output" ); |
362 | |
|
363 | 6 | File tmpDir = FileUtils.createTempFile( "integration-test-repo", "", null ); |
364 | 6 | String tmpUrl = tmpDir.toURL().toExternalForm(); |
365 | |
|
366 | 6 | deployRepo.setUrl( tmpUrl ); |
367 | |
|
368 | 6 | distMgmt.setRepository( deployRepo ); |
369 | 6 | distMgmt.setSnapshotRepository( deployRepo ); |
370 | |
|
371 | 6 | Repository localAsRemote = new Repository(); |
372 | 6 | localAsRemote.setId( "testing.mainLocalAsRemote" ); |
373 | |
|
374 | 6 | File localRepoDir = repositoryTool.findLocalRepositoryDirectory(); |
375 | 6 | localAsRemote.setUrl( localRepoDir.toURL().toExternalForm() ); |
376 | |
|
377 | 6 | model.addRepository( localAsRemote ); |
378 | 6 | model.addPluginRepository( localAsRemote ); |
379 | |
|
380 | 6 | Site site = new Site(); |
381 | |
|
382 | 6 | site.setId( "integration-test.output" ); |
383 | 6 | site.setUrl( tmpUrl ); |
384 | |
|
385 | 6 | distMgmt.setSite( site ); |
386 | |
|
387 | 6 | model.setDistributionManagement( distMgmt ); |
388 | |
|
389 | 6 | model.addProperty( INTEGRATION_TEST_DEPLOYMENT_REPO_URL, tmpUrl ); |
390 | |
|
391 | 6 | if ( skipUnitTests ) |
392 | |
{ |
393 | 6 | List plugins = build.getPlugins(); |
394 | 6 | Plugin plugin = null; |
395 | 6 | for ( Iterator iter = plugins.iterator(); iter.hasNext(); ) |
396 | |
{ |
397 | 0 | Plugin plug = (Plugin) iter.next(); |
398 | |
|
399 | 0 | if ( "maven-surefire-plugin".equals( plug.getArtifactId() ) ) |
400 | |
{ |
401 | 0 | plugin = plug; |
402 | 0 | break; |
403 | |
} |
404 | 0 | } |
405 | |
|
406 | 6 | if ( plugin == null ) |
407 | |
{ |
408 | 6 | plugin = new Plugin(); |
409 | 6 | plugin.setArtifactId( "maven-surefire-plugin" ); |
410 | 6 | build.addPlugin( plugin ); |
411 | |
} |
412 | |
|
413 | 6 | Xpp3Dom configDom = (Xpp3Dom) plugin.getConfiguration(); |
414 | 6 | if ( configDom == null ) |
415 | |
{ |
416 | 6 | configDom = new Xpp3Dom( "configuration" ); |
417 | 6 | plugin.setConfiguration( configDom ); |
418 | |
} |
419 | |
|
420 | 6 | Xpp3Dom skipDom = new Xpp3Dom( "skip" ); |
421 | 6 | skipDom.setValue( "true" ); |
422 | |
|
423 | 6 | configDom.addChild( skipDom ); |
424 | |
} |
425 | |
|
426 | 6 | writer = WriterFactory.newXmlWriter( output ); |
427 | |
|
428 | 6 | new MavenXpp3Writer().write( writer, model ); |
429 | |
} |
430 | 0 | catch ( IOException e ) |
431 | |
{ |
432 | 0 | throw new TestToolsException( "Error creating test-time version of POM for: " + input, e ); |
433 | |
} |
434 | |
finally |
435 | |
{ |
436 | 6 | IOUtil.close( writer ); |
437 | 6 | } |
438 | |
|
439 | 6 | return new PomInfo( output, model.getGroupId(), model.getArtifactId(), model.getVersion(), |
440 | |
model.getBuild().getDirectory(), model.getBuild().getOutputDirectory(), finalName ); |
441 | |
} |
442 | |
|
443 | 6 | static final class PomInfo |
444 | |
{ |
445 | |
private final File pomFile; |
446 | |
|
447 | |
private final String groupId; |
448 | |
|
449 | |
private final String artifactId; |
450 | |
|
451 | |
private final String version; |
452 | |
|
453 | |
private final String finalName; |
454 | |
|
455 | |
private final String buildDirectory; |
456 | |
|
457 | |
private final String buildOutputDirectory; |
458 | |
|
459 | |
PomInfo( File pomFile, String groupId, String artifactId, String version, String buildDirectory, |
460 | |
String buildOutputDirectory, String finalName ) |
461 | 6 | { |
462 | 6 | this.pomFile = pomFile; |
463 | 6 | this.groupId = groupId; |
464 | 6 | this.artifactId = artifactId; |
465 | 6 | this.version = version; |
466 | 6 | this.buildDirectory = buildDirectory; |
467 | 6 | this.buildOutputDirectory = buildOutputDirectory; |
468 | 6 | this.finalName = finalName; |
469 | 6 | } |
470 | |
|
471 | |
File getPomFile() |
472 | |
{ |
473 | 16 | return pomFile; |
474 | |
} |
475 | |
|
476 | |
String getBuildDirectory() |
477 | |
{ |
478 | 6 | return buildDirectory; |
479 | |
} |
480 | |
|
481 | |
String getBuildOutputDirectory() |
482 | |
{ |
483 | 2 | return buildOutputDirectory; |
484 | |
} |
485 | |
|
486 | |
String getFinalName() |
487 | |
{ |
488 | 6 | return finalName; |
489 | |
} |
490 | |
|
491 | |
File getBuildLogFile() |
492 | |
{ |
493 | 4 | return new File( buildDirectory + "/test-build-logs/" + groupId + "_" + artifactId + "_" + version |
494 | |
+ ".build.log" ); |
495 | |
} |
496 | |
} |
497 | |
} |