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