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 session.setAllProjects( session.getProjects() );
161
162 return session;
163 }
164
165 protected void initRepoSession( ProjectBuildingRequest request )
166 throws Exception
167 {
168 File localRepoDir = new File( request.getLocalRepository().getBasedir() );
169 LocalRepository localRepo = new LocalRepository( localRepoDir );
170 DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
171 session.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( session, localRepo ) );
172 request.setRepositorySession( session );
173 }
174
175 protected MavenProject createStubMavenProject()
176 {
177 Model model = new Model();
178 model.setGroupId( "org.apache.maven.test" );
179 model.setArtifactId( "maven-test" );
180 model.setVersion( "1.0" );
181 return new MavenProject( model );
182 }
183
184 protected List<ArtifactRepository> getRemoteRepositories()
185 throws InvalidRepositoryException
186 {
187 File repoDir = new File( getBasedir(), "src/test/remote-repo" ).getAbsoluteFile();
188
189 RepositoryPolicy policy = new RepositoryPolicy();
190 policy.setEnabled( true );
191 policy.setChecksumPolicy( "ignore" );
192 policy.setUpdatePolicy( "always" );
193
194 Repository repository = new Repository();
195 repository.setId( RepositorySystem.DEFAULT_REMOTE_REPO_ID );
196 repository.setUrl( "file://" + repoDir.toURI().getPath() );
197 repository.setReleases( policy );
198 repository.setSnapshots( policy );
199
200 return Arrays.asList( repositorySystem.buildArtifactRepository( repository ) );
201 }
202
203 protected List<ArtifactRepository> getPluginArtifactRepositories()
204 throws InvalidRepositoryException
205 {
206 return getRemoteRepositories();
207 }
208
209 protected ArtifactRepository getLocalRepository()
210 throws InvalidRepositoryException
211 {
212 File repoDir = new File( getBasedir(), "target/local-repo" ).getAbsoluteFile();
213
214 return repositorySystem.createLocalRepository( repoDir );
215 }
216
217 protected class ProjectBuilder
218 {
219 private MavenProject project;
220
221 public ProjectBuilder( MavenProject project )
222 {
223 this.project = project;
224 }
225
226 public ProjectBuilder( String groupId, String artifactId, String version )
227 {
228 Model model = new Model();
229 model.setModelVersion( "4.0.0" );
230 model.setGroupId( groupId );
231 model.setArtifactId( artifactId );
232 model.setVersion( version );
233 model.setBuild( new Build() );
234 project = new MavenProject( model );
235 }
236
237 public ProjectBuilder setGroupId( String groupId )
238 {
239 project.setGroupId( groupId );
240 return this;
241 }
242
243 public ProjectBuilder setArtifactId( String artifactId )
244 {
245 project.setArtifactId( artifactId );
246 return this;
247 }
248
249 public ProjectBuilder setVersion( String version )
250 {
251 project.setVersion( version );
252 return this;
253 }
254
255
256
257 public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope )
258 {
259 return addDependency( groupId, artifactId, version, scope, (Exclusion)null );
260 }
261
262 public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, Exclusion exclusion )
263 {
264 return addDependency( groupId, artifactId, version, scope, null, exclusion );
265 }
266
267 public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath )
268 {
269 return addDependency( groupId, artifactId, version, scope, systemPath, null );
270 }
271
272 public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath, Exclusion exclusion )
273 {
274 Dependency d = new Dependency();
275 d.setGroupId( groupId );
276 d.setArtifactId( artifactId );
277 d.setVersion( version );
278 d.setScope( scope );
279
280 if ( systemPath != null && scope.equals( Artifact.SCOPE_SYSTEM ) )
281 {
282 d.setSystemPath( systemPath );
283 }
284
285 if ( exclusion != null )
286 {
287 d.addExclusion( exclusion );
288 }
289
290 project.getDependencies().add( d );
291
292 return this;
293 }
294
295
296
297 public ProjectBuilder addPlugin( Plugin plugin )
298 {
299 project.getBuildPlugins().add( plugin );
300 return this;
301 }
302
303 public MavenProject get()
304 {
305 return project;
306 }
307 }
308
309 protected class PluginBuilder
310 {
311 private Plugin plugin;
312
313 public PluginBuilder( String groupId, String artifactId, String version )
314 {
315 plugin = new Plugin();
316 plugin.setGroupId( groupId );
317 plugin.setArtifactId( artifactId );
318 plugin.setVersion( version );
319 }
320
321
322
323 public PluginBuilder addDependency( String groupId, String artifactId, String version, String scope, Exclusion exclusion )
324 {
325 return addDependency( groupId, artifactId, version, scope, exclusion );
326 }
327
328 public PluginBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath )
329 {
330 return addDependency( groupId, artifactId, version, scope, systemPath, null );
331 }
332
333 public PluginBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath, Exclusion exclusion )
334 {
335 Dependency d = new Dependency();
336 d.setGroupId( groupId );
337 d.setArtifactId( artifactId );
338 d.setVersion( version );
339 d.setScope( scope );
340
341 if ( systemPath != null && scope.equals( Artifact.SCOPE_SYSTEM ) )
342 {
343 d.setSystemPath( systemPath );
344 }
345
346 if ( exclusion != null )
347 {
348 d.addExclusion( exclusion );
349 }
350
351 plugin.getDependencies().add( d );
352
353 return this;
354 }
355
356 public Plugin get()
357 {
358 return plugin;
359 }
360 }
361 }