1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.execution;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import javax.inject.Singleton;
30 import org.apache.maven.artifact.InvalidRepositoryException;
31 import org.apache.maven.artifact.repository.ArtifactRepository;
32 import org.apache.maven.bridge.MavenRepositorySystem;
33 import org.apache.maven.repository.RepositorySystem;
34 import org.apache.maven.settings.Mirror;
35 import org.apache.maven.settings.Proxy;
36 import org.apache.maven.settings.Repository;
37 import org.apache.maven.settings.Server;
38 import org.apache.maven.settings.Settings;
39 import org.apache.maven.settings.SettingsUtils;
40 import org.apache.maven.toolchain.model.PersistedToolchains;
41 import org.apache.maven.toolchain.model.ToolchainModel;
42 import org.codehaus.plexus.util.StringUtils;
43
44
45
46
47 @Named
48 @Singleton
49 public class DefaultMavenExecutionRequestPopulator implements MavenExecutionRequestPopulator {
50
51 private final MavenRepositorySystem repositorySystem;
52
53 @Inject
54 public DefaultMavenExecutionRequestPopulator(MavenRepositorySystem repositorySystem) {
55 this.repositorySystem = repositorySystem;
56 }
57
58 @Override
59 public MavenExecutionRequest populateFromToolchains(MavenExecutionRequest request, PersistedToolchains toolchains)
60 throws MavenExecutionRequestPopulationException {
61 if (toolchains != null) {
62 Map<String, List<ToolchainModel>> groupedToolchains = new HashMap<>(2);
63
64 for (ToolchainModel model : toolchains.getToolchains()) {
65 if (!groupedToolchains.containsKey(model.getType())) {
66 groupedToolchains.put(model.getType(), new ArrayList<>());
67 }
68
69 groupedToolchains.get(model.getType()).add(model);
70 }
71
72 request.setToolchains(groupedToolchains);
73 }
74 return request;
75 }
76
77 @Override
78 public MavenExecutionRequest populateDefaults(MavenExecutionRequest request)
79 throws MavenExecutionRequestPopulationException {
80 baseDirectory(request);
81
82 localRepository(request);
83
84 populateDefaultPluginGroups(request);
85
86 injectDefaultRepositories(request);
87
88 injectDefaultPluginRepositories(request);
89
90 return request;
91 }
92
93
94
95
96
97 private void populateDefaultPluginGroups(MavenExecutionRequest request) {
98 request.addPluginGroup("org.apache.maven.plugins");
99 request.addPluginGroup("org.codehaus.mojo");
100 }
101
102 private void injectDefaultRepositories(MavenExecutionRequest request)
103 throws MavenExecutionRequestPopulationException {
104 Set<String> definedRepositories = repositorySystem.getRepoIds(request.getRemoteRepositories());
105
106 if (!definedRepositories.contains(RepositorySystem.DEFAULT_REMOTE_REPO_ID)) {
107 try {
108 request.addRemoteRepository(repositorySystem.createDefaultRemoteRepository(request));
109 } catch (Exception e) {
110 throw new MavenExecutionRequestPopulationException("Cannot create default remote repository.", e);
111 }
112 }
113 }
114
115 private void injectDefaultPluginRepositories(MavenExecutionRequest request)
116 throws MavenExecutionRequestPopulationException {
117 Set<String> definedRepositories = repositorySystem.getRepoIds(request.getPluginArtifactRepositories());
118
119 if (!definedRepositories.contains(RepositorySystem.DEFAULT_REMOTE_REPO_ID)) {
120 try {
121 request.addPluginArtifactRepository(repositorySystem.createDefaultRemoteRepository(request));
122 } catch (Exception e) {
123 throw new MavenExecutionRequestPopulationException("Cannot create default remote repository.", e);
124 }
125 }
126 }
127
128 private void localRepository(MavenExecutionRequest request) throws MavenExecutionRequestPopulationException {
129
130
131
132
133
134
135
136
137 if (request.getLocalRepository() == null) {
138 request.setLocalRepository(createLocalRepository(request));
139 }
140
141 if (request.getLocalRepositoryPath() == null) {
142 request.setLocalRepositoryPath(new File(request.getLocalRepository().getBasedir()).getAbsoluteFile());
143 }
144 }
145
146
147
148
149
150 private ArtifactRepository createLocalRepository(MavenExecutionRequest request)
151 throws MavenExecutionRequestPopulationException {
152 String localRepositoryPath = null;
153
154 if (request.getLocalRepositoryPath() != null) {
155 localRepositoryPath = request.getLocalRepositoryPath().getAbsolutePath();
156 }
157
158 if (StringUtils.isEmpty(localRepositoryPath)) {
159 localRepositoryPath = RepositorySystem.defaultUserLocalRepository.getAbsolutePath();
160 }
161
162 try {
163 return repositorySystem.createLocalRepository(request, new File(localRepositoryPath));
164 } catch (Exception e) {
165 throw new MavenExecutionRequestPopulationException("Cannot create local repository.", e);
166 }
167 }
168
169 private void baseDirectory(MavenExecutionRequest request) {
170 if (request.getBaseDirectory() == null && request.getPom() != null) {
171 request.setBaseDirectory(request.getPom().getAbsoluteFile().getParentFile());
172 }
173 }
174
175
176
177 @Override
178 @Deprecated
179 public MavenExecutionRequest populateFromSettings(MavenExecutionRequest request, Settings settings)
180 throws MavenExecutionRequestPopulationException {
181 if (settings == null) {
182 return request;
183 }
184
185 request.setOffline(settings.isOffline());
186
187 request.setInteractiveMode(settings.isInteractiveMode());
188
189 request.setPluginGroups(settings.getPluginGroups());
190
191 request.setLocalRepositoryPath(settings.getLocalRepository());
192
193 for (Server server : settings.getServers()) {
194 server = server.clone();
195
196 request.addServer(server);
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211 for (Proxy proxy : settings.getProxies()) {
212 if (!proxy.isActive()) {
213 continue;
214 }
215
216 proxy = proxy.clone();
217
218 request.addProxy(proxy);
219 }
220
221
222
223
224
225
226
227
228
229 for (Mirror mirror : settings.getMirrors()) {
230 mirror = mirror.clone();
231
232 request.addMirror(mirror);
233 }
234
235 request.setActiveProfiles(settings.getActiveProfiles());
236
237 for (org.apache.maven.settings.Profile rawProfile : settings.getProfiles()) {
238 request.addProfile(SettingsUtils.convertFromSettingsProfile(rawProfile.getDelegate()));
239
240 if (settings.getActiveProfiles().contains(rawProfile.getId())) {
241 List<Repository> remoteRepositories = rawProfile.getRepositories();
242 for (Repository remoteRepository : remoteRepositories) {
243 try {
244 request.addRemoteRepository(MavenRepositorySystem.buildArtifactRepository(remoteRepository));
245 } catch (InvalidRepositoryException e) {
246
247 }
248 }
249
250 List<Repository> pluginRepositories = rawProfile.getPluginRepositories();
251 for (Repository pluginRepo : pluginRepositories) {
252 try {
253 request.addPluginArtifactRepository(MavenRepositorySystem.buildArtifactRepository(pluginRepo));
254 } catch (InvalidRepositoryException e) {
255
256 }
257 }
258 }
259 }
260
261 return request;
262 }
263
264
265
266 }