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 javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.io.File;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.apache.maven.api.Constants;
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.settings.Mirror;
36 import org.apache.maven.settings.Proxy;
37 import org.apache.maven.settings.Repository;
38 import org.apache.maven.settings.Server;
39 import org.apache.maven.settings.Settings;
40 import org.apache.maven.settings.SettingsUtils;
41 import org.apache.maven.toolchain.model.PersistedToolchains;
42 import org.apache.maven.toolchain.model.ToolchainModel;
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 return request;
87 }
88
89
90
91
92
93 private void populateDefaultPluginGroups(MavenExecutionRequest request) {
94 request.addPluginGroup("org.apache.maven.plugins");
95 request.addPluginGroup("org.codehaus.mojo");
96 }
97
98 private void localRepository(MavenExecutionRequest request) throws MavenExecutionRequestPopulationException {
99
100
101
102
103
104
105
106
107 if (request.getLocalRepository() == null) {
108 request.setLocalRepository(createLocalRepository(request));
109 }
110
111 if (request.getLocalRepositoryPath() == null) {
112 request.setLocalRepositoryPath(new File(request.getLocalRepository().getBasedir()).getAbsoluteFile());
113 }
114 }
115
116
117
118
119
120 private ArtifactRepository createLocalRepository(MavenExecutionRequest request)
121 throws MavenExecutionRequestPopulationException {
122 String localRepositoryPath = null;
123
124 if (request.getLocalRepositoryPath() != null) {
125 localRepositoryPath = request.getLocalRepositoryPath().getAbsolutePath();
126 }
127
128 if (localRepositoryPath == null || localRepositoryPath.isEmpty()) {
129 String path = request.getUserProperties().getProperty(Constants.MAVEN_USER_CONF);
130 if (path == null) {
131 path = request.getSystemProperties().getProperty("user.home") + File.separator + ".m2";
132 }
133 localRepositoryPath = new File(path, "repository").getAbsolutePath();
134 }
135
136 try {
137 return repositorySystem.createLocalRepository(new File(localRepositoryPath));
138 } catch (Exception e) {
139 throw new MavenExecutionRequestPopulationException("Cannot create local repository.", e);
140 }
141 }
142
143 private void baseDirectory(MavenExecutionRequest request) {
144 if (request.getBaseDirectory() == null && request.getPom() != null) {
145 request.setBaseDirectory(request.getPom().getAbsoluteFile().getParentFile());
146 }
147 }
148
149
150
151 @Override
152 @Deprecated
153 public MavenExecutionRequest populateFromSettings(MavenExecutionRequest request, Settings settings)
154 throws MavenExecutionRequestPopulationException {
155 if (settings == null) {
156 return request;
157 }
158
159 request.setOffline(settings.isOffline());
160
161 request.setInteractiveMode(settings.isInteractiveMode());
162
163 request.setPluginGroups(settings.getPluginGroups());
164
165 request.setLocalRepositoryPath(settings.getLocalRepository());
166
167 for (Server server : settings.getServers()) {
168 server = server.clone();
169
170 request.addServer(server);
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184
185 for (Proxy proxy : settings.getProxies()) {
186 if (!proxy.isActive()) {
187 continue;
188 }
189
190 proxy = proxy.clone();
191
192 request.addProxy(proxy);
193 }
194
195
196
197
198
199
200
201
202
203 for (Mirror mirror : settings.getMirrors()) {
204 mirror = mirror.clone();
205
206 request.addMirror(mirror);
207 }
208
209 request.setActiveProfiles(settings.getActiveProfiles());
210
211 for (org.apache.maven.settings.Profile rawProfile : settings.getProfiles()) {
212 request.addProfile(SettingsUtils.convertFromSettingsProfile(rawProfile));
213
214 if (settings.getActiveProfiles().contains(rawProfile.getId())) {
215 List<Repository> remoteRepositories = rawProfile.getRepositories();
216 for (Repository remoteRepository : remoteRepositories) {
217 try {
218 request.addRemoteRepository(MavenRepositorySystem.buildArtifactRepository(remoteRepository));
219 } catch (InvalidRepositoryException e) {
220
221 }
222 }
223
224 List<Repository> pluginRepositories = rawProfile.getPluginRepositories();
225 for (Repository pluginRepo : pluginRepositories) {
226 try {
227 request.addPluginArtifactRepository(MavenRepositorySystem.buildArtifactRepository(pluginRepo));
228 } catch (InvalidRepositoryException e) {
229
230 }
231 }
232 }
233 }
234
235 return request;
236 }
237
238
239
240 }