1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.settings.building;
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.io.IOException;
27 import java.io.InputStream;
28 import java.lang.reflect.Method;
29 import java.nio.file.Path;
30 import java.util.List;
31 import java.util.stream.Collectors;
32
33 import org.apache.maven.api.Session;
34 import org.apache.maven.api.services.BuilderProblem;
35 import org.apache.maven.api.services.SettingsBuilderException;
36 import org.apache.maven.api.services.SettingsBuilderRequest;
37 import org.apache.maven.api.services.SettingsBuilderResult;
38 import org.apache.maven.api.services.xml.SettingsXmlFactory;
39 import org.apache.maven.building.FileSource;
40 import org.apache.maven.building.Source;
41 import org.apache.maven.settings.Settings;
42
43
44
45
46
47 @Named
48 @Singleton
49 public class DefaultSettingsBuilder implements SettingsBuilder {
50
51 private final org.apache.maven.internal.impl.DefaultSettingsBuilder builder;
52 private final org.apache.maven.internal.impl.DefaultSettingsXmlFactory settingsXmlFactory;
53
54 @Inject
55 public DefaultSettingsBuilder(
56 org.apache.maven.internal.impl.DefaultSettingsBuilder builder,
57 org.apache.maven.internal.impl.DefaultSettingsXmlFactory settingsXmlFactory) {
58 this.builder = builder;
59 this.settingsXmlFactory = settingsXmlFactory;
60 }
61
62 @Override
63 public SettingsBuildingResult build(SettingsBuildingRequest request) throws SettingsBuildingException {
64
65 try {
66 SettingsBuilderResult result = builder.build(SettingsBuilderRequest.builder()
67 .session((Session) java.lang.reflect.Proxy.newProxyInstance(
68 Session.class.getClassLoader(),
69 new Class[] {Session.class},
70 (Object proxy, Method method, Object[] args) -> {
71 if ("getSystemProperties".equals(method.getName())) {
72 return request.getSystemProperties().entrySet().stream()
73 .collect(Collectors.toMap(
74 e -> e.getKey().toString(),
75 e -> e.getValue().toString()));
76 } else if ("getUserProperties".equals(method.getName())) {
77 return request.getUserProperties().entrySet().stream()
78 .collect(Collectors.toMap(
79 e -> e.getKey().toString(),
80 e -> e.getValue().toString()));
81 } else if ("getService".equals(method.getName())) {
82 if (args[0] == SettingsXmlFactory.class) {
83 return settingsXmlFactory;
84 }
85 }
86 return null;
87 }))
88 .globalSettingsSource(toSource(request.getGlobalSettingsFile(), request.getGlobalSettingsSource()))
89 .projectSettingsSource(
90 toSource(request.getProjectSettingsFile(), request.getProjectSettingsSource()))
91 .userSettingsSource(toSource(request.getUserSettingsFile(), request.getUserSettingsSource()))
92 .build());
93
94 return new DefaultSettingsBuildingResult(
95 new Settings(result.getEffectiveSettings()), convert(result.getProblems()));
96 } catch (SettingsBuilderException e) {
97 throw new SettingsBuildingException(convert(e.getProblems()));
98 }
99 }
100
101 private org.apache.maven.api.services.Source toSource(File file, Source source) {
102 if (file != null && file.exists()) {
103 return org.apache.maven.api.services.Source.fromPath(file.toPath());
104 } else if (source instanceof FileSource fs) {
105 return org.apache.maven.api.services.Source.fromPath(fs.getPath());
106 } else if (source != null) {
107 return new org.apache.maven.api.services.Source() {
108 @Override
109 public Path getPath() {
110 return null;
111 }
112
113 @Override
114 public InputStream openStream() throws IOException {
115 return source.getInputStream();
116 }
117
118 @Override
119 public String getLocation() {
120 return source.getLocation();
121 }
122
123 @Override
124 public org.apache.maven.api.services.Source resolve(String relative) {
125 return null;
126 }
127 };
128 } else {
129 return null;
130 }
131 }
132
133 private List<SettingsProblem> convert(List<BuilderProblem> problems) {
134 return problems.stream().map(this::convert).toList();
135 }
136
137 private SettingsProblem convert(BuilderProblem problem) {
138 return new DefaultSettingsProblem(
139 problem.getMessage(),
140 SettingsProblem.Severity.valueOf(problem.getSeverity().name()),
141 problem.getSource(),
142 problem.getLineNumber(),
143 problem.getColumnNumber(),
144 problem.getException());
145 }
146 }