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
66 @Override
67 public MavenExecutionRequest populateFromToolchains( MavenExecutionRequest request, PersistedToolchains toolchains )
68 throws MavenExecutionRequestPopulationException
69 {
70 if ( toolchains != null )
71 {
72 Map<String, List<ToolchainModel>> groupedToolchains = new HashMap<String, List<ToolchainModel>>( 2 );
73
74 for ( ToolchainModel model : toolchains.getToolchains() )
75 {
76 if ( !groupedToolchains.containsKey( model.getType() ) )
77 {
78 groupedToolchains.put( model.getType(), new ArrayList<ToolchainModel>() );
79 }
80
81 groupedToolchains.get( model.getType() ).add( model );
82 }
83
84 request.setToolchains( groupedToolchains );
85 }
86 return request;
87 }
88
89 @Override
90 public MavenExecutionRequest populateDefaults( MavenExecutionRequest request )
91 throws MavenExecutionRequestPopulationException
92 {
93 baseDirectory( request );
94
95 localRepository( request );
96
97 populateDefaultPluginGroups( request );
98
99 injectDefaultRepositories( request );
100
101 injectDefaultPluginRepositories( request );
102
103 return request;
104 }
105
106
107
108
109
110 private void populateDefaultPluginGroups( MavenExecutionRequest request )
111 {
112 request.addPluginGroup( "org.apache.maven.plugins" );
113 request.addPluginGroup( "org.codehaus.mojo" );
114 }
115
116 private void injectDefaultRepositories( MavenExecutionRequest request )
117 throws MavenExecutionRequestPopulationException
118 {
119 Set<String> definedRepositories = repositorySystem.getRepoIds( request.getRemoteRepositories() );
120
121 if ( !definedRepositories.contains( RepositorySystem.DEFAULT_REMOTE_REPO_ID ) )
122 {
123 try
124 {
125 request.addRemoteRepository( repositorySystem.createDefaultRemoteRepository( request ) );
126 }
127 catch ( Exception e )
128 {
129 throw new MavenExecutionRequestPopulationException( "Cannot create default remote repository.", e );
130 }
131 }
132 }
133
134 private void injectDefaultPluginRepositories( MavenExecutionRequest request )
135 throws MavenExecutionRequestPopulationException
136 {
137 Set<String> definedRepositories = repositorySystem.getRepoIds( request.getPluginArtifactRepositories() );
138
139 if ( !definedRepositories.contains( RepositorySystem.DEFAULT_REMOTE_REPO_ID ) )
140 {
141 try
142 {
143 request.addPluginArtifactRepository( repositorySystem.createDefaultRemoteRepository( request ) );
144 }
145 catch ( Exception e )
146 {
147 throw new MavenExecutionRequestPopulationException( "Cannot create default remote repository.", e );
148 }
149 }
150 }
151
152 private void localRepository( MavenExecutionRequest request )
153 throws MavenExecutionRequestPopulationException
154 {
155
156
157
158
159
160
161
162
163 if ( request.getLocalRepository() == null )
164 {
165 request.setLocalRepository( createLocalRepository( request ) );
166 }
167
168 if ( request.getLocalRepositoryPath() == null )
169 {
170 request.setLocalRepositoryPath( new File( request.getLocalRepository().getBasedir() ).getAbsoluteFile() );
171 }
172 }
173
174
175
176
177
178 private ArtifactRepository createLocalRepository( MavenExecutionRequest request )
179 throws MavenExecutionRequestPopulationException
180 {
181 String localRepositoryPath = null;
182
183 if ( request.getLocalRepositoryPath() != null )
184 {
185 localRepositoryPath = request.getLocalRepositoryPath().getAbsolutePath();
186 }
187
188 if ( StringUtils.isEmpty( localRepositoryPath ) )
189 {
190 localRepositoryPath = RepositorySystem.defaultUserLocalRepository.getAbsolutePath();
191 }
192
193 try
194 {
195 return repositorySystem.createLocalRepository( request, new File( localRepositoryPath ) );
196 }
197 catch ( Exception e )
198 {
199 throw new MavenExecutionRequestPopulationException( "Cannot create local repository.", e );
200 }
201 }
202
203 private void baseDirectory( MavenExecutionRequest request )
204 {
205 if ( request.getBaseDirectory() == null && request.getPom() != null )
206 {
207 request.setBaseDirectory( request.getPom().getAbsoluteFile().getParentFile() );
208 }
209 }
210
211
212
213 @Override
214 @Deprecated
215 public MavenExecutionRequest populateFromSettings( MavenExecutionRequest request, Settings settings )
216 throws MavenExecutionRequestPopulationException
217 {
218 if ( settings == null )
219 {
220 return request;
221 }
222
223 request.setOffline( settings.isOffline() );
224
225 request.setInteractiveMode( settings.isInteractiveMode() );
226
227 request.setPluginGroups( settings.getPluginGroups() );
228
229 request.setLocalRepositoryPath( settings.getLocalRepository() );
230
231 for ( Server server : settings.getServers() )
232 {
233 server = server.clone();
234
235 request.addServer( server );
236 }
237
238
239
240
241
242
243
244
245
246
247
248
249
250 for ( Proxy proxy : settings.getProxies() )
251 {
252 if ( !proxy.isActive() )
253 {
254 continue;
255 }
256
257 proxy = proxy.clone();
258
259 request.addProxy( proxy );
260 }
261
262
263
264
265
266
267
268
269
270 for ( Mirror mirror : settings.getMirrors() )
271 {
272 mirror = mirror.clone();
273
274 request.addMirror( mirror );
275 }
276
277 request.setActiveProfiles( settings.getActiveProfiles() );
278
279 for ( org.apache.maven.settings.Profile rawProfile : settings.getProfiles() )
280 {
281 request.addProfile( SettingsUtils.convertFromSettingsProfile( rawProfile ) );
282
283 if ( settings.getActiveProfiles().contains( rawProfile.getId() ) )
284 {
285 List<Repository> remoteRepositories = rawProfile.getRepositories();
286 for ( Repository remoteRepository : remoteRepositories )
287 {
288 try
289 {
290 request.addRemoteRepository( repositorySystem.buildArtifactRepository( remoteRepository ) );
291 }
292 catch ( InvalidRepositoryException e )
293 {
294
295 }
296 }
297
298 List<Repository> pluginRepositories = rawProfile.getPluginRepositories();
299 for ( Repository pluginRepo : pluginRepositories )
300 {
301 try
302 {
303 request.addPluginArtifactRepository( repositorySystem.buildArtifactRepository( pluginRepo ) );
304 }
305 catch ( InvalidRepositoryException e )
306 {
307
308 }
309 }
310 }
311 }
312
313 return request;
314 }
315
316
317
318 }