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