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