1 package org.apache.maven.execution;
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.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import javax.inject.Inject;
30 import javax.inject.Named;
31
32 import org.apache.maven.artifact.InvalidRepositoryException;
33 import org.apache.maven.artifact.repository.ArtifactRepository;
34 import org.apache.maven.bridge.MavenRepositorySystem;
35 import org.apache.maven.repository.RepositorySystem;
36
37
38
39 import org.apache.maven.settings.Mirror;
40 import org.apache.maven.settings.Proxy;
41 import org.apache.maven.settings.Repository;
42 import org.apache.maven.settings.Server;
43 import org.apache.maven.settings.Settings;
44 import org.apache.maven.settings.SettingsUtils;
45
46
47
48 import org.apache.maven.toolchain.model.PersistedToolchains;
49 import org.apache.maven.toolchain.model.ToolchainModel;
50 import org.codehaus.plexus.util.StringUtils;
51
52 @Named
53 public class DefaultMavenExecutionRequestPopulator
54 implements MavenExecutionRequestPopulator
55 {
56
57 private final MavenRepositorySystem repositorySystem;
58
59 @Inject
60 public DefaultMavenExecutionRequestPopulator( MavenRepositorySystem repositorySystem )
61 {
62 this.repositorySystem = repositorySystem;
63 }
64
65 @Override
66 public MavenExecutionRequest populateFromSettings( MavenExecutionRequest request, Settings settings )
67 throws MavenExecutionRequestPopulationException
68 {
69 if ( settings == null )
70 {
71 return request;
72 }
73
74 request.setOffline( settings.isOffline() );
75
76 request.setInteractiveMode( settings.isInteractiveMode() );
77
78 request.setPluginGroups( settings.getPluginGroups() );
79
80 request.setLocalRepositoryPath( settings.getLocalRepository() );
81
82 for ( Server server : settings.getServers() )
83 {
84 server = server.clone();
85
86 request.addServer( server );
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100
101 for ( Proxy proxy : settings.getProxies() )
102 {
103 if ( !proxy.isActive() )
104 {
105 continue;
106 }
107
108 proxy = proxy.clone();
109
110 request.addProxy( proxy );
111 }
112
113
114
115
116
117
118
119
120
121 for ( Mirror mirror : settings.getMirrors() )
122 {
123 mirror = mirror.clone();
124
125 request.addMirror( mirror );
126 }
127
128 request.setActiveProfiles( settings.getActiveProfiles() );
129
130 for ( org.apache.maven.settings.Profile rawProfile : settings.getProfiles() )
131 {
132 request.addProfile( SettingsUtils.convertFromSettingsProfile( rawProfile ) );
133
134 if ( settings.getActiveProfiles().contains( rawProfile.getId() ) )
135 {
136 List<Repository> remoteRepositories = rawProfile.getRepositories();
137 for ( Repository remoteRepository : remoteRepositories )
138 {
139 try
140 {
141 request.addRemoteRepository( repositorySystem.buildArtifactRepository( remoteRepository ) );
142 }
143 catch ( InvalidRepositoryException e )
144 {
145
146 }
147 }
148
149 List<Repository> pluginRepositories = rawProfile.getPluginRepositories();
150 for ( Repository pluginRepo : pluginRepositories )
151 {
152 try
153 {
154 request.addPluginArtifactRepository( repositorySystem.buildArtifactRepository( pluginRepo ) );
155 }
156 catch ( InvalidRepositoryException e )
157 {
158
159 }
160 }
161 }
162 }
163
164 return request;
165 }
166
167 @Override
168 public MavenExecutionRequest populateFromToolchains( MavenExecutionRequest request, PersistedToolchains toolchains )
169 throws MavenExecutionRequestPopulationException
170 {
171 if ( toolchains != null )
172 {
173 Map<String, List<ToolchainModel>> groupedToolchains = new HashMap<String, List<ToolchainModel>>( 2 );
174
175 for ( ToolchainModel model : toolchains.getToolchains() )
176 {
177 if ( !groupedToolchains.containsKey( model.getType() ) )
178 {
179 groupedToolchains.put( model.getType(), new ArrayList<ToolchainModel>() );
180 }
181
182 groupedToolchains.get( model.getType() ).add( model );
183 }
184
185 request.setToolchains( groupedToolchains );
186 }
187 return request;
188 }
189
190 @Override
191 public MavenExecutionRequest populateDefaults( MavenExecutionRequest request )
192 throws MavenExecutionRequestPopulationException
193 {
194 baseDirectory( request );
195
196 localRepository( request );
197
198 populateDefaultPluginGroups( request );
199
200 injectDefaultRepositories( request );
201
202 injectDefaultPluginRepositories( request );
203
204 processRepositoriesInSettings( request );
205
206 return request;
207 }
208
209
210
211
212
213 private void populateDefaultPluginGroups( MavenExecutionRequest request )
214 {
215 request.addPluginGroup( "org.apache.maven.plugins" );
216 request.addPluginGroup( "org.codehaus.mojo" );
217 }
218
219 private void injectDefaultRepositories( MavenExecutionRequest request )
220 throws MavenExecutionRequestPopulationException
221 {
222 Set<String> definedRepositories = repositorySystem.getRepoIds( request.getRemoteRepositories() );
223
224 if ( !definedRepositories.contains( RepositorySystem.DEFAULT_REMOTE_REPO_ID ) )
225 {
226 try
227 {
228 request.addRemoteRepository( repositorySystem.createDefaultRemoteRepository( request ) );
229 }
230 catch ( Exception e )
231 {
232 throw new MavenExecutionRequestPopulationException( "Cannot create default remote repository.", e );
233 }
234 }
235 }
236
237 private void injectDefaultPluginRepositories( MavenExecutionRequest request )
238 throws MavenExecutionRequestPopulationException
239 {
240 Set<String> definedRepositories = repositorySystem.getRepoIds( request.getPluginArtifactRepositories() );
241
242 if ( !definedRepositories.contains( RepositorySystem.DEFAULT_REMOTE_REPO_ID ) )
243 {
244 try
245 {
246 request.addPluginArtifactRepository( repositorySystem.createDefaultRemoteRepository( request ) );
247 }
248 catch ( Exception e )
249 {
250 throw new MavenExecutionRequestPopulationException( "Cannot create default remote repository.", e );
251 }
252 }
253 }
254
255 private void processRepositoriesInSettings( MavenExecutionRequest request )
256 throws MavenExecutionRequestPopulationException
257 {
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299 repositorySystem.injectMirror( request.getRemoteRepositories(), request.getMirrors() );
300 repositorySystem.injectMirror( request.getPluginArtifactRepositories(), request.getMirrors() );
301 }
302
303 private void localRepository( MavenExecutionRequest request )
304 throws MavenExecutionRequestPopulationException
305 {
306
307
308
309
310
311
312
313
314 if ( request.getLocalRepository() == null )
315 {
316 request.setLocalRepository( createLocalRepository( request ) );
317 }
318
319 if ( request.getLocalRepositoryPath() == null )
320 {
321 request.setLocalRepositoryPath( new File( request.getLocalRepository().getBasedir() ).getAbsoluteFile() );
322 }
323 }
324
325
326
327
328
329 private ArtifactRepository createLocalRepository( MavenExecutionRequest request )
330 throws MavenExecutionRequestPopulationException
331 {
332 String localRepositoryPath = null;
333
334 if ( request.getLocalRepositoryPath() != null )
335 {
336 localRepositoryPath = request.getLocalRepositoryPath().getAbsolutePath();
337 }
338
339 if ( StringUtils.isEmpty( localRepositoryPath ) )
340 {
341 localRepositoryPath = RepositorySystem.defaultUserLocalRepository.getAbsolutePath();
342 }
343
344 try
345 {
346 return repositorySystem.createLocalRepository( request, new File( localRepositoryPath ) );
347 }
348 catch ( Exception e )
349 {
350 throw new MavenExecutionRequestPopulationException( "Cannot create local repository.", e );
351 }
352 }
353
354 private void baseDirectory( MavenExecutionRequest request )
355 {
356 if ( request.getBaseDirectory() == null && request.getPom() != null )
357 {
358 request.setBaseDirectory( request.getPom().getAbsoluteFile().getParentFile() );
359 }
360 }
361 }