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.artifact.InvalidRepositoryException;
32 import org.apache.maven.artifact.repository.ArtifactRepository;
33 import org.apache.maven.bridge.MavenRepositorySystem;
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
43
44
45
46 @Named
47 @Singleton
48 public class DefaultMavenExecutionRequestPopulator implements MavenExecutionRequestPopulator {
49
50 private final MavenRepositorySystem repositorySystem;
51
52 @Inject
53 public DefaultMavenExecutionRequestPopulator(MavenRepositorySystem repositorySystem) {
54 this.repositorySystem = repositorySystem;
55 }
56
57 @Override
58 public MavenExecutionRequest populateFromToolchains(MavenExecutionRequest request, PersistedToolchains toolchains)
59 throws MavenExecutionRequestPopulationException {
60 if (toolchains != null) {
61 Map<String, List<ToolchainModel>> groupedToolchains = new HashMap<>(2);
62
63 for (ToolchainModel model : toolchains.getToolchains()) {
64 if (!groupedToolchains.containsKey(model.getType())) {
65 groupedToolchains.put(model.getType(), new ArrayList<>());
66 }
67
68 groupedToolchains.get(model.getType()).add(model);
69 }
70
71 request.setToolchains(groupedToolchains);
72 }
73 return request;
74 }
75
76 @Override
77 public MavenExecutionRequest populateDefaults(MavenExecutionRequest request)
78 throws MavenExecutionRequestPopulationException {
79 baseDirectory(request);
80
81 localRepository(request);
82
83 populateDefaultPluginGroups(request);
84
85 return request;
86 }
87
88
89
90
91
92 private void populateDefaultPluginGroups(MavenExecutionRequest request) {
93 request.addPluginGroup("org.apache.maven.plugins");
94 request.addPluginGroup("org.codehaus.mojo");
95 }
96
97 private void localRepository(MavenExecutionRequest request) throws MavenExecutionRequestPopulationException {
98
99
100
101
102
103
104
105
106 if (request.getLocalRepository() == null) {
107 request.setLocalRepository(createLocalRepository(request));
108 }
109
110 if (request.getLocalRepositoryPath() == null) {
111 request.setLocalRepositoryPath(new File(request.getLocalRepository().getBasedir()).getAbsoluteFile());
112 }
113 }
114
115
116
117
118
119 private ArtifactRepository createLocalRepository(MavenExecutionRequest request)
120 throws MavenExecutionRequestPopulationException {
121 String localRepositoryPath = null;
122
123 if (request.getLocalRepositoryPath() != null) {
124 localRepositoryPath = request.getLocalRepositoryPath().getAbsolutePath();
125 }
126
127 if (localRepositoryPath == null || localRepositoryPath.isEmpty()) {
128 localRepositoryPath = new File(System.getProperty("user.home"), ".m2/repository").getAbsolutePath();
129 }
130
131 try {
132 return repositorySystem.createLocalRepository(new File(localRepositoryPath));
133 } catch (Exception e) {
134 throw new MavenExecutionRequestPopulationException("Cannot create local repository.", e);
135 }
136 }
137
138 private void baseDirectory(MavenExecutionRequest request) {
139 if (request.getBaseDirectory() == null && request.getPom() != null) {
140 request.setBaseDirectory(request.getPom().getAbsoluteFile().getParentFile());
141 }
142 }
143
144
145
146 @Override
147 @Deprecated
148 public MavenExecutionRequest populateFromSettings(MavenExecutionRequest request, Settings settings)
149 throws MavenExecutionRequestPopulationException {
150 if (settings == null) {
151 return request;
152 }
153
154 request.setOffline(settings.isOffline());
155
156 request.setInteractiveMode(settings.isInteractiveMode());
157
158 request.setPluginGroups(settings.getPluginGroups());
159
160 request.setLocalRepositoryPath(settings.getLocalRepository());
161
162 for (Server server : settings.getServers()) {
163 server = server.clone();
164
165 request.addServer(server);
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179
180 for (Proxy proxy : settings.getProxies()) {
181 if (!proxy.isActive()) {
182 continue;
183 }
184
185 proxy = proxy.clone();
186
187 request.addProxy(proxy);
188 }
189
190
191
192
193
194
195
196
197
198 for (Mirror mirror : settings.getMirrors()) {
199 mirror = mirror.clone();
200
201 request.addMirror(mirror);
202 }
203
204 request.setActiveProfiles(settings.getActiveProfiles());
205
206 for (org.apache.maven.settings.Profile rawProfile : settings.getProfiles()) {
207 request.addProfile(SettingsUtils.convertFromSettingsProfile(rawProfile));
208
209 if (settings.getActiveProfiles().contains(rawProfile.getId())) {
210 List<Repository> remoteRepositories = rawProfile.getRepositories();
211 for (Repository remoteRepository : remoteRepositories) {
212 try {
213 request.addRemoteRepository(MavenRepositorySystem.buildArtifactRepository(remoteRepository));
214 } catch (InvalidRepositoryException e) {
215
216 }
217 }
218
219 List<Repository> pluginRepositories = rawProfile.getPluginRepositories();
220 for (Repository pluginRepo : pluginRepositories) {
221 try {
222 request.addPluginArtifactRepository(MavenRepositorySystem.buildArtifactRepository(pluginRepo));
223 } catch (InvalidRepositoryException e) {
224
225 }
226 }
227 }
228 }
229
230 return request;
231 }
232
233
234
235 }