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 public class ProjectTool
73 {
74
75 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 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 ArtifactRepository localRepository = repositoryTool
125 .createLocalArtifactRepositoryInstance( localRepositoryBasedir );
126
127 ProjectBuildingRequest request = new DefaultProjectBuildingRequest();
128 return projectBuilder.build( pomFile, request ).getProject();
129 }
130 catch ( ProjectBuildingException e )
131 {
132 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 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 ArtifactRepository localRepository = repositoryTool
164 .createLocalArtifactRepositoryInstance( localRepositoryBasedir );
165
166 ProjectBuildingRequest request = new DefaultProjectBuildingRequest();
167 return projectBuilder.build( pomFile, request ).getProject();
168 }
169 catch ( ProjectBuildingException e )
170 {
171 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 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 PomInfo pomInfo = manglePomForTesting( pomFile, testVersion, skipUnitTests );
225
226 Properties properties = new Properties();
227
228 List goals = new ArrayList();
229 goals.add( "package" );
230
231 File buildLog = logFile == null ? pomInfo.getBuildLogFile() : logFile;
232 System.out.println( "Now Building test version of the plugin...\nUsing staged plugin-pom: "
233 + pomInfo.getPomFile().getAbsolutePath() );
234
235 buildTool.executeMaven( pomInfo.getPomFile(), properties, goals, buildLog );
236
237 File artifactFile = new File( pomInfo.getPomFile().getParentFile(),
238 pomInfo.getBuildDirectory() + "/" + pomInfo.getFinalName() );
239 System.out.println( "Using IT Plugin Jar: " + artifactFile.getAbsolutePath() );
240 try
241 {
242 ProjectBuildingRequest request = new DefaultProjectBuildingRequest();
243 request.setLocalRepository( artifactRepositoryFactory.createArtifactRepository( "local", new File( "target/localrepo" ).getCanonicalFile().toURL().toExternalForm(), "default", null, null ) );
244 request.setRepositorySession( new MavenRepositorySystemSession() );
245 MavenProject project = projectBuilder.build( pomInfo.getPomFile(), request ).getProject();
246
247 Artifact artifact = artifactFactory.createArtifact( project.getGroupId(), project.getArtifactId(), project
248 .getVersion(), null, project.getPackaging() );
249
250 artifact.setFile( artifactFile );
251 artifact.addMetadata( new ProjectArtifactMetadata( artifact, project.getFile() ) );
252
253 project.setArtifact( artifact );
254
255 return project;
256 }
257 catch ( ProjectBuildingException e )
258 {
259 throw new TestToolsException(
260 "Error building MavenProject instance from test pom: " + pomInfo.getPomFile(),
261 e );
262 }
263 catch ( UnknownRepositoryLayoutException e )
264 {
265 throw new TestToolsException(
266 "Error building ArtifactRepository instance from test pom: " + pomInfo.getPomFile(),
267 e );
268 }
269 catch ( IOException e )
270 {
271 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 File input = pomFile;
293
294 File output = new File( pomFile.getParentFile(), "pom-" + testVersion + ".xml" );
295 output.deleteOnExit();
296
297 Reader reader = null;
298 Writer writer = null;
299
300 Model model = null;
301 String finalName = null;
302 String buildDirectory = null;
303
304 try
305 {
306 reader = ReaderFactory.newXmlReader( input );
307
308 model = new MavenXpp3Reader().read( reader );
309 }
310 catch ( IOException e )
311 {
312 throw new TestToolsException( "Error creating test-time version of POM for: " + input, e );
313 }
314 catch ( XmlPullParserException e )
315 {
316 throw new TestToolsException( "Error creating test-time version of POM for: " + input, e );
317 }
318 finally
319 {
320 IOUtil.close( reader );
321 }
322
323 try
324 {
325 model.setVersion( testVersion );
326
327 Build build = model.getBuild();
328 if ( build == null )
329 {
330 build = new Build();
331 }
332 buildDirectory = build.getDirectory();
333
334 if ( buildDirectory == null )
335 {
336 buildDirectory = "target";
337 }
338
339 buildDirectory = ( buildDirectory + File.separatorChar + "it-build-target" );
340 build.setDirectory( buildDirectory );
341 build.setOutputDirectory( buildDirectory + File.separatorChar + "classes" );
342 System.out.println( "Using " + build.getDirectory() + " and " + build.getOutputDirectory()
343 + " to build IT version of plugin" );
344 model.setBuild( build );
345
346 finalName = build.getFinalName();
347
348 if ( finalName == null )
349 {
350 ArtifactHandler handler = artifactHandlerManager.getArtifactHandler( model.getPackaging() );
351
352 String ext = handler.getExtension();
353
354 finalName = model.getArtifactId() + "-" + model.getVersion() + "." + ext;
355 }
356
357 DistributionManagement distMgmt = new DistributionManagement();
358
359 DeploymentRepository deployRepo = new DeploymentRepository();
360
361 deployRepo.setId( "integration-test.output" );
362
363 File tmpDir = FileUtils.createTempFile( "integration-test-repo", "", null );
364 String tmpUrl = tmpDir.toURL().toExternalForm();
365
366 deployRepo.setUrl( tmpUrl );
367
368 distMgmt.setRepository( deployRepo );
369 distMgmt.setSnapshotRepository( deployRepo );
370
371 Repository localAsRemote = new Repository();
372 localAsRemote.setId( "testing.mainLocalAsRemote" );
373
374 File localRepoDir = repositoryTool.findLocalRepositoryDirectory();
375 localAsRemote.setUrl( localRepoDir.toURL().toExternalForm() );
376
377 model.addRepository( localAsRemote );
378 model.addPluginRepository( localAsRemote );
379
380 Site site = new Site();
381
382 site.setId( "integration-test.output" );
383 site.setUrl( tmpUrl );
384
385 distMgmt.setSite( site );
386
387 model.setDistributionManagement( distMgmt );
388
389 model.addProperty( INTEGRATION_TEST_DEPLOYMENT_REPO_URL, tmpUrl );
390
391 if ( skipUnitTests )
392 {
393 List plugins = build.getPlugins();
394 Plugin plugin = null;
395 for ( Iterator iter = plugins.iterator(); iter.hasNext(); )
396 {
397 Plugin plug = (Plugin) iter.next();
398
399 if ( "maven-surefire-plugin".equals( plug.getArtifactId() ) )
400 {
401 plugin = plug;
402 break;
403 }
404 }
405
406 if ( plugin == null )
407 {
408 plugin = new Plugin();
409 plugin.setArtifactId( "maven-surefire-plugin" );
410 build.addPlugin( plugin );
411 }
412
413 Xpp3Dom configDom = (Xpp3Dom) plugin.getConfiguration();
414 if ( configDom == null )
415 {
416 configDom = new Xpp3Dom( "configuration" );
417 plugin.setConfiguration( configDom );
418 }
419
420 Xpp3Dom skipDom = new Xpp3Dom( "skip" );
421 skipDom.setValue( "true" );
422
423 configDom.addChild( skipDom );
424 }
425
426 writer = WriterFactory.newXmlWriter( output );
427
428 new MavenXpp3Writer().write( writer, model );
429 }
430 catch ( IOException e )
431 {
432 throw new TestToolsException( "Error creating test-time version of POM for: " + input, e );
433 }
434 finally
435 {
436 IOUtil.close( writer );
437 }
438
439 return new PomInfo( output, model.getGroupId(), model.getArtifactId(), model.getVersion(),
440 model.getBuild().getDirectory(), model.getBuild().getOutputDirectory(), finalName );
441 }
442
443 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 {
462 this.pomFile = pomFile;
463 this.groupId = groupId;
464 this.artifactId = artifactId;
465 this.version = version;
466 this.buildDirectory = buildDirectory;
467 this.buildOutputDirectory = buildOutputDirectory;
468 this.finalName = finalName;
469 }
470
471 File getPomFile()
472 {
473 return pomFile;
474 }
475
476 String getBuildDirectory()
477 {
478 return buildDirectory;
479 }
480
481 String getBuildOutputDirectory()
482 {
483 return buildOutputDirectory;
484 }
485
486 String getFinalName()
487 {
488 return finalName;
489 }
490
491 File getBuildLogFile()
492 {
493 return new File( buildDirectory + "/test-build-logs/" + groupId + "_" + artifactId + "_" + version
494 + ".build.log" );
495 }
496 }
497 }