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.HashSet;
24 import java.util.List;
25 import java.util.Set;
26
27 import org.apache.maven.artifact.InvalidRepositoryException;
28 import org.apache.maven.artifact.repository.ArtifactRepository;
29 import org.apache.maven.bridge.MavenRepositorySystem;
30 import org.apache.maven.repository.RepositorySystem;
31 import org.apache.maven.settings.Mirror;
32 import org.apache.maven.settings.Proxy;
33 import org.apache.maven.settings.Repository;
34 import org.apache.maven.settings.Server;
35 import org.apache.maven.settings.Settings;
36 import org.apache.maven.settings.SettingsUtils;
37 import org.codehaus.plexus.component.annotations.Component;
38 import org.codehaus.plexus.component.annotations.Requirement;
39 import org.codehaus.plexus.util.StringUtils;
40
41 @Component( role = MavenExecutionRequestPopulator.class )
42 public class DefaultMavenExecutionRequestPopulator
43 implements MavenExecutionRequestPopulator
44 {
45
46 @Requirement
47 private RepositorySystem repositorySystem;
48
49 public MavenExecutionRequest populateFromSettings( MavenExecutionRequest request, Settings settings )
50 throws MavenExecutionRequestPopulationException
51 {
52 if ( settings == null )
53 {
54 return request;
55 }
56
57 request.setOffline( settings.isOffline() );
58
59 request.setInteractiveMode( settings.isInteractiveMode() );
60
61 request.setPluginGroups( settings.getPluginGroups() );
62
63 request.setLocalRepositoryPath( settings.getLocalRepository() );
64
65 for ( Server server : settings.getServers() )
66 {
67 server = server.clone();
68
69 request.addServer( server );
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84 for ( Proxy proxy : settings.getProxies() )
85 {
86 if ( !proxy.isActive() )
87 {
88 continue;
89 }
90
91 proxy = proxy.clone();
92
93 request.addProxy( proxy );
94 }
95
96
97
98
99
100
101
102
103
104 for ( Mirror mirror : settings.getMirrors() )
105 {
106 mirror = mirror.clone();
107
108 request.addMirror( mirror );
109 }
110
111 request.setActiveProfiles( settings.getActiveProfiles() );
112
113 for ( org.apache.maven.settings.Profile rawProfile : settings.getProfiles() )
114 {
115 request.addProfile( SettingsUtils.convertFromSettingsProfile( rawProfile ) );
116
117 if ( settings.getActiveProfiles().contains( rawProfile.getId() ) )
118 {
119 List<Repository> remoteRepositories = rawProfile.getRepositories();
120 for ( Repository remoteRepository : remoteRepositories )
121 {
122 try
123 {
124 request.addRemoteRepository( MavenRepositorySystem.buildArtifactRepository( remoteRepository ) );
125 }
126 catch ( InvalidRepositoryException e )
127 {
128
129 }
130 }
131 }
132 }
133
134 return request;
135 }
136
137 private void populateDefaultPluginGroups( MavenExecutionRequest request )
138 {
139 request.addPluginGroup( "org.apache.maven.plugins" );
140 request.addPluginGroup( "org.codehaus.mojo" );
141 }
142
143 private void injectDefaultRepositories( MavenExecutionRequest request )
144 throws MavenExecutionRequestPopulationException
145 {
146 Set<String> definedRepositories = getRepoIds( request.getRemoteRepositories() );
147
148 if ( !definedRepositories.contains( RepositorySystem.DEFAULT_REMOTE_REPO_ID ) )
149 {
150 try
151 {
152 request.addRemoteRepository( repositorySystem.createDefaultRemoteRepository() );
153 }
154 catch ( InvalidRepositoryException e )
155 {
156 throw new MavenExecutionRequestPopulationException( "Cannot create default remote repository.", e );
157 }
158 }
159 }
160
161 private void injectDefaultPluginRepositories( MavenExecutionRequest request )
162 throws MavenExecutionRequestPopulationException
163 {
164 Set<String> definedRepositories = getRepoIds( request.getPluginArtifactRepositories() );
165
166 if ( !definedRepositories.contains( RepositorySystem.DEFAULT_REMOTE_REPO_ID ) )
167 {
168 try
169 {
170 request.addPluginArtifactRepository( repositorySystem.createDefaultRemoteRepository() );
171 }
172 catch ( InvalidRepositoryException e )
173 {
174 throw new MavenExecutionRequestPopulationException( "Cannot create default remote repository.", e );
175 }
176 }
177 }
178
179 private Set<String> getRepoIds( List<ArtifactRepository> repositories )
180 {
181 Set<String> repoIds = new HashSet<String>();
182
183 if ( repositories != null )
184 {
185 for ( ArtifactRepository repository : repositories )
186 {
187 repoIds.add( repository.getId() );
188 }
189 }
190
191 return repoIds;
192 }
193
194 private void processRepositoriesInSettings( MavenExecutionRequest request )
195 throws MavenExecutionRequestPopulationException
196 {
197 repositorySystem.injectMirror( request.getRemoteRepositories(), request.getMirrors() );
198 repositorySystem.injectProxy( request.getRemoteRepositories(), request.getProxies() );
199 repositorySystem.injectAuthentication( request.getRemoteRepositories(), request.getServers() );
200
201 request.setRemoteRepositories( repositorySystem.getEffectiveRepositories( request.getRemoteRepositories() ) );
202
203 repositorySystem.injectMirror( request.getPluginArtifactRepositories(), request.getMirrors() );
204 repositorySystem.injectProxy( request.getPluginArtifactRepositories(), request.getProxies() );
205 repositorySystem.injectAuthentication( request.getPluginArtifactRepositories(), request.getServers() );
206
207 request.setPluginArtifactRepositories( repositorySystem.getEffectiveRepositories( request.getPluginArtifactRepositories() ) );
208 }
209
210 private void localRepository( MavenExecutionRequest request )
211 throws MavenExecutionRequestPopulationException
212 {
213
214
215
216
217
218
219
220
221 if ( request.getLocalRepository() == null )
222 {
223 request.setLocalRepository( createLocalRepository( request ) );
224 }
225
226 if ( request.getLocalRepositoryPath() == null )
227 {
228 request.setLocalRepositoryPath( new File( request.getLocalRepository().getBasedir() ).getAbsoluteFile() );
229 }
230 }
231
232
233
234
235
236 public ArtifactRepository createLocalRepository( MavenExecutionRequest request )
237 throws MavenExecutionRequestPopulationException
238 {
239 String localRepositoryPath = null;
240
241 if ( request.getLocalRepositoryPath() != null )
242 {
243 localRepositoryPath = request.getLocalRepositoryPath().getAbsolutePath();
244 }
245
246 if ( StringUtils.isEmpty( localRepositoryPath ) )
247 {
248 localRepositoryPath = RepositorySystem.defaultUserLocalRepository.getAbsolutePath();
249 }
250
251 try
252 {
253 return repositorySystem.createLocalRepository( new File( localRepositoryPath ) );
254 }
255 catch ( InvalidRepositoryException e )
256 {
257 throw new MavenExecutionRequestPopulationException( "Cannot create local repository.", e );
258 }
259 }
260
261 private void baseDirectory( MavenExecutionRequest request )
262 {
263 if ( request.getBaseDirectory() == null && request.getPom() != null )
264 {
265 request.setBaseDirectory( request.getPom().getAbsoluteFile().getParentFile() );
266 }
267 }
268
269 public MavenExecutionRequest populateDefaults( MavenExecutionRequest request )
270 throws MavenExecutionRequestPopulationException
271 {
272 baseDirectory( request );
273
274 localRepository( request );
275
276 populateDefaultPluginGroups( request );
277
278 injectDefaultRepositories( request );
279
280 injectDefaultPluginRepositories( request );
281
282 processRepositoriesInSettings( request );
283
284 return request;
285 }
286
287 }