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