1 package org.apache.maven;
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.util.Arrays;
24 import java.util.List;
25 import java.util.Properties;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.InvalidRepositoryException;
29 import org.apache.maven.artifact.repository.ArtifactRepository;
30 import org.apache.maven.execution.DefaultMavenExecutionRequest;
31 import org.apache.maven.execution.DefaultMavenExecutionResult;
32 import org.apache.maven.execution.MavenExecutionRequest;
33 import org.apache.maven.execution.MavenSession;
34 import org.apache.maven.model.Build;
35 import org.apache.maven.model.Dependency;
36 import org.apache.maven.model.Exclusion;
37 import org.apache.maven.model.Model;
38 import org.apache.maven.model.Plugin;
39 import org.apache.maven.model.Repository;
40 import org.apache.maven.model.RepositoryPolicy;
41 import org.apache.maven.project.DefaultProjectBuildingRequest;
42 import org.apache.maven.project.MavenProject;
43 import org.apache.maven.project.ProjectBuildingRequest;
44 import org.apache.maven.repository.RepositorySystem;
45 import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
46 import org.codehaus.plexus.ContainerConfiguration;
47 import org.codehaus.plexus.PlexusConstants;
48 import org.codehaus.plexus.PlexusTestCase;
49 import org.codehaus.plexus.component.annotations.Requirement;
50 import org.codehaus.plexus.util.FileUtils;
51 import org.eclipse.aether.DefaultRepositorySystemSession;
52 import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
53 import org.eclipse.aether.repository.LocalRepository;
54
55 public abstract class AbstractCoreMavenComponentTestCase
56 extends PlexusTestCase
57 {
58 @Requirement
59 protected RepositorySystem repositorySystem;
60
61 @Requirement
62 protected org.apache.maven.project.ProjectBuilder projectBuilder;
63
64 protected void setUp()
65 throws Exception
66 {
67 repositorySystem = lookup( RepositorySystem.class );
68 projectBuilder = lookup( org.apache.maven.project.ProjectBuilder.class );
69 }
70
71 @Override
72 protected void tearDown()
73 throws Exception
74 {
75 repositorySystem = null;
76 projectBuilder = null;
77 super.tearDown();
78 }
79
80 abstract protected String getProjectsDirectory();
81
82 protected File getProject( String name )
83 throws Exception
84 {
85 File source = new File( new File( getBasedir(), getProjectsDirectory() ), name );
86 File target = new File( new File( getBasedir(), "target" ), name );
87 FileUtils.copyDirectoryStructureIfModified( source, target );
88 return new File( target, "pom.xml" );
89 }
90
91
92
93
94
95
96
97
98
99
100 protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration )
101 {
102 containerConfiguration.setAutoWiring( true ).setClassPathScanning( PlexusConstants.SCANNING_INDEX );
103 }
104
105 protected MavenExecutionRequest createMavenExecutionRequest( File pom )
106 throws Exception
107 {
108 MavenExecutionRequest request = new DefaultMavenExecutionRequest()
109 .setPom( pom )
110 .setProjectPresent( true )
111 .setShowErrors( true )
112 .setPluginGroups( Arrays.asList( "org.apache.maven.plugins" ) )
113 .setLocalRepository( getLocalRepository() )
114 .setRemoteRepositories( getRemoteRepositories() )
115 .setPluginArtifactRepositories( getPluginArtifactRepositories() )
116 .setGoals( Arrays.asList( "package" ) );
117
118 return request;
119 }
120
121
122
123
124 protected MavenSession createMavenSession( File pom )
125 throws Exception
126 {
127 return createMavenSession( pom, new Properties() );
128 }
129
130 protected MavenSession createMavenSession( File pom, Properties executionProperties )
131 throws Exception
132 {
133 MavenExecutionRequest request = createMavenExecutionRequest( pom );
134
135 ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest()
136 .setLocalRepository( request.getLocalRepository() )
137 .setRemoteRepositories( request.getRemoteRepositories() )
138 .setPluginArtifactRepositories( request.getPluginArtifactRepositories() )
139 .setSystemProperties( executionProperties );
140
141 MavenProject project = null;
142
143 if ( pom != null )
144 {
145 project = projectBuilder.build( pom, configuration ).getProject();
146 }
147 else
148 {
149 project = createStubMavenProject();
150 project.setRemoteArtifactRepositories( request.getRemoteRepositories() );
151 project.setPluginArtifactRepositories( request.getPluginArtifactRepositories() );
152 }
153
154 initRepoSession( configuration );
155
156 MavenSession session =
157 new MavenSession( getContainer(), configuration.getRepositorySession(), request,
158 new DefaultMavenExecutionResult() );
159 session.setProjects( Arrays.asList( project ) );
160
161 return session;
162 }
163
164 protected void initRepoSession( ProjectBuildingRequest request )
165 throws Exception
166 {
167 File localRepoDir = new File( request.getLocalRepository().getBasedir() );
168 LocalRepository localRepo = new LocalRepository( localRepoDir );
169 DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
170 session.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( session, localRepo ) );
171 request.setRepositorySession( session );
172 }
173
174 protected MavenProject createStubMavenProject()
175 {
176 Model model = new Model();
177 model.setGroupId( "org.apache.maven.test" );
178 model.setArtifactId( "maven-test" );
179 model.setVersion( "1.0" );
180 return new MavenProject( model );
181 }
182
183 protected List<ArtifactRepository> getRemoteRepositories()
184 throws InvalidRepositoryException
185 {
186 File repoDir = new File( getBasedir(), "src/test/remote-repo" ).getAbsoluteFile();
187
188 RepositoryPolicy policy = new RepositoryPolicy();
189 policy.setEnabled( true );
190 policy.setChecksumPolicy( "ignore" );
191 policy.setUpdatePolicy( "always" );
192
193 Repository repository = new Repository();
194 repository.setId( RepositorySystem.DEFAULT_REMOTE_REPO_ID );
195 repository.setUrl( "file://" + repoDir.toURI().getPath() );
196 repository.setReleases( policy );
197 repository.setSnapshots( policy );
198
199 return Arrays.asList( repositorySystem.buildArtifactRepository( repository ) );
200 }
201
202 protected List<ArtifactRepository> getPluginArtifactRepositories()
203 throws InvalidRepositoryException
204 {
205 return getRemoteRepositories();
206 }
207
208 protected ArtifactRepository getLocalRepository()
209 throws InvalidRepositoryException
210 {
211 File repoDir = new File( getBasedir(), "target/local-repo" ).getAbsoluteFile();
212
213 return repositorySystem.createLocalRepository( repoDir );
214 }
215
216 protected class ProjectBuilder
217 {
218 private MavenProject project;
219
220 public ProjectBuilder( MavenProject project )
221 {
222 this.project = project;
223 }
224
225 public ProjectBuilder( String groupId, String artifactId, String version )
226 {
227 Model model = new Model();
228 model.setModelVersion( "4.0.0" );
229 model.setGroupId( groupId );
230 model.setArtifactId( artifactId );
231 model.setVersion( version );
232 model.setBuild( new Build() );
233 project = new MavenProject( model );
234 }
235
236 public ProjectBuilder setGroupId( String groupId )
237 {
238 project.setGroupId( groupId );
239 return this;
240 }
241
242 public ProjectBuilder setArtifactId( String artifactId )
243 {
244 project.setArtifactId( artifactId );
245 return this;
246 }
247
248 public ProjectBuilder setVersion( String version )
249 {
250 project.setVersion( version );
251 return this;
252 }
253
254
255
256 public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope )
257 {
258 return addDependency( groupId, artifactId, version, scope, (Exclusion)null );
259 }
260
261 public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, Exclusion exclusion )
262 {
263 return addDependency( groupId, artifactId, version, scope, null, exclusion );
264 }
265
266 public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath )
267 {
268 return addDependency( groupId, artifactId, version, scope, systemPath, null );
269 }
270
271 public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath, Exclusion exclusion )
272 {
273 Dependency d = new Dependency();
274 d.setGroupId( groupId );
275 d.setArtifactId( artifactId );
276 d.setVersion( version );
277 d.setScope( scope );
278
279 if ( systemPath != null && scope.equals( Artifact.SCOPE_SYSTEM ) )
280 {
281 d.setSystemPath( systemPath );
282 }
283
284 if ( exclusion != null )
285 {
286 d.addExclusion( exclusion );
287 }
288
289 project.getDependencies().add( d );
290
291 return this;
292 }
293
294
295
296 public ProjectBuilder addPlugin( Plugin plugin )
297 {
298 project.getBuildPlugins().add( plugin );
299 return this;
300 }
301
302 public MavenProject get()
303 {
304 return project;
305 }
306 }
307
308 protected class PluginBuilder
309 {
310 private Plugin plugin;
311
312 public PluginBuilder( String groupId, String artifactId, String version )
313 {
314 plugin = new Plugin();
315 plugin.setGroupId( groupId );
316 plugin.setArtifactId( artifactId );
317 plugin.setVersion( version );
318 }
319
320
321
322 public PluginBuilder addDependency( String groupId, String artifactId, String version, String scope, Exclusion exclusion )
323 {
324 return addDependency( groupId, artifactId, version, scope, exclusion );
325 }
326
327 public PluginBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath )
328 {
329 return addDependency( groupId, artifactId, version, scope, systemPath, null );
330 }
331
332 public PluginBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath, Exclusion exclusion )
333 {
334 Dependency d = new Dependency();
335 d.setGroupId( groupId );
336 d.setArtifactId( artifactId );
337 d.setVersion( version );
338 d.setScope( scope );
339
340 if ( systemPath != null && scope.equals( Artifact.SCOPE_SYSTEM ) )
341 {
342 d.setSystemPath( systemPath );
343 }
344
345 if ( exclusion != null )
346 {
347 d.addExclusion( exclusion );
348 }
349
350 plugin.getDependencies().add( d );
351
352 return this;
353 }
354
355 public Plugin get()
356 {
357 return plugin;
358 }
359 }
360 }