Skip to content

Commit 003a5bc

Browse files
authored
[MNG-8084] Make the v4 api usable outside the Maven runtime (#1441)
1 parent 52d453c commit 003a5bc

108 files changed

Lines changed: 3095 additions & 1628 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.api.services;
20+
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
26+
class PathSource implements Source {
27+
28+
private final Path path;
29+
30+
PathSource(Path path) {
31+
this.path = path;
32+
}
33+
34+
@Override
35+
public Path getPath() {
36+
return path;
37+
}
38+
39+
@Override
40+
public InputStream openStream() throws IOException {
41+
return Files.newInputStream(path);
42+
}
43+
44+
@Override
45+
public String getLocation() {
46+
return path.toString();
47+
}
48+
49+
@Override
50+
public Source resolve(String relative) {
51+
return new PathSource(path.resolve(relative));
52+
}
53+
}

api/maven-api-core/src/main/java/org/apache/maven/api/services/SettingsBuilder.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
package org.apache.maven.api.services;
2020

2121
import java.nio.file.Path;
22+
import java.util.List;
2223

2324
import org.apache.maven.api.Service;
2425
import org.apache.maven.api.Session;
2526
import org.apache.maven.api.annotations.Experimental;
2627
import org.apache.maven.api.annotations.Nonnull;
28+
import org.apache.maven.api.settings.Settings;
2729

2830
/**
2931
* Builds the effective settings from a user settings file and/or a global settings file.
@@ -97,4 +99,37 @@ default SettingsBuilderResult build(
9799
@Nonnull Path userSettingsPath) {
98100
return build(SettingsBuilderRequest.build(session, globalSettingsPath, projectSettingsPath, userSettingsPath));
99101
}
102+
103+
/**
104+
* Validate the specified settings.
105+
*
106+
* @param settings The settings to validate, must not be {@code null}.
107+
* @return The list of problems that were encountered, must not be {@code null}.
108+
*/
109+
@Nonnull
110+
default List<BuilderProblem> validate(@Nonnull Settings settings) {
111+
return validate(settings, false);
112+
}
113+
114+
/**
115+
* Validate the specified settings.
116+
*
117+
* @param settings The settings to validate, must not be {@code null}.
118+
* @param isProjectSettings Boolean indicating if the validation is for project settings or user / global settings.
119+
* @return The list of problems that were encountered, must not be {@code null}.
120+
*/
121+
@Nonnull
122+
List<BuilderProblem> validate(@Nonnull Settings settings, boolean isProjectSettings);
123+
124+
/**
125+
* Convert a model profile to a settings profile.
126+
*/
127+
@Nonnull
128+
org.apache.maven.api.settings.Profile convert(@Nonnull org.apache.maven.api.model.Profile profile);
129+
130+
/**
131+
* Convert a settings profile to a model profile.
132+
*/
133+
@Nonnull
134+
org.apache.maven.api.model.Profile convert(@Nonnull org.apache.maven.api.settings.Profile profile);
100135
}

api/maven-api-core/src/main/java/org/apache/maven/api/services/SettingsBuilderException.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
package org.apache.maven.api.services;
2020

21+
import java.util.List;
22+
import java.util.stream.Collectors;
23+
2124
import org.apache.maven.api.annotations.Experimental;
2225

2326
/**
@@ -27,13 +30,24 @@
2730
*/
2831
@Experimental
2932
public class SettingsBuilderException extends MavenException {
33+
34+
private final List<BuilderProblem> problems;
35+
3036
/**
3137
* @param message the message to give
3238
* @param e the {@link Exception}
3339
*/
3440
public SettingsBuilderException(String message, Exception e) {
3541
super(message, e);
42+
this.problems = List.of();
3643
}
3744

38-
// TODO: add SettingsBuilderResult
45+
public SettingsBuilderException(String message, List<BuilderProblem> problems) {
46+
super(message + ": " + problems.stream().map(BuilderProblem::toString).collect(Collectors.joining(", ")), null);
47+
this.problems = List.copyOf(problems);
48+
}
49+
50+
public List<BuilderProblem> getProblems() {
51+
return problems;
52+
}
3953
}

api/maven-api-core/src/main/java/org/apache/maven/api/services/SettingsBuilderRequest.java

Lines changed: 24 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.apache.maven.api.services;
2020

21+
import java.nio.file.Files;
2122
import java.nio.file.Path;
2223
import java.util.Optional;
2324

@@ -40,14 +41,6 @@ public interface SettingsBuilderRequest {
4041
@Nonnull
4142
Session getSession();
4243

43-
/**
44-
* Gets the global settings path.
45-
*
46-
* @return the global settings path or {@code null} if none
47-
*/
48-
@Nonnull
49-
Optional<Path> getGlobalSettingsPath();
50-
5144
/**
5245
* Gets the global settings source.
5346
*
@@ -64,22 +57,6 @@ public interface SettingsBuilderRequest {
6457
@Nonnull
6558
Optional<Source> getProjectSettingsSource();
6659

67-
/**
68-
* Gets the project settings path.
69-
*
70-
* @return the project settings path or {@code null} if none
71-
*/
72-
@Nonnull
73-
Optional<Path> getProjectSettingsPath();
74-
75-
/**
76-
* Gets the user settings path.
77-
*
78-
* @return the user settings path or {@code null} if none
79-
*/
80-
@Nonnull
81-
Optional<Path> getUserSettingsPath();
82-
8360
/**
8461
* Gets the user settings source.
8562
*
@@ -97,34 +74,43 @@ static SettingsBuilderRequest build(
9774
@Nonnull
9875
static SettingsBuilderRequest build(
9976
@Nonnull Session session, @Nonnull Path globalSettingsPath, @Nonnull Path userSettingsPath) {
100-
return build(session, globalSettingsPath, null, userSettingsPath);
77+
return build(session, Source.fromPath(globalSettingsPath), null, Source.fromPath(userSettingsPath));
10178
}
10279

10380
@Nonnull
10481
static SettingsBuilderRequest build(
10582
@Nonnull Session session,
106-
@Nonnull Source globalSettingsSource,
107-
@Nonnull Source projectSettingsSource,
108-
@Nonnull Source userSettingsSource) {
83+
@Nullable Source globalSettingsSource,
84+
@Nullable Source projectSettingsSource,
85+
@Nullable Source userSettingsSource) {
10986
return builder()
11087
.session(nonNull(session, "session cannot be null"))
111-
.globalSettingsSource(nonNull(globalSettingsSource, "globalSettingsSource cannot be null"))
112-
.projectSettingsSource(nonNull(projectSettingsSource, "projectSettingsSource cannot be null"))
113-
.userSettingsSource(nonNull(userSettingsSource, "userSettingsSource cannot be null"))
88+
.globalSettingsSource(globalSettingsSource)
89+
.projectSettingsSource(projectSettingsSource)
90+
.userSettingsSource(userSettingsSource)
11491
.build();
11592
}
11693

11794
@Nonnull
11895
static SettingsBuilderRequest build(
11996
@Nonnull Session session,
120-
@Nonnull Path globalSettingsPath,
121-
@Nonnull Path projectSettingsPath,
122-
@Nonnull Path userSettingsPath) {
97+
@Nullable Path globalSettingsPath,
98+
@Nullable Path projectSettingsPath,
99+
@Nullable Path userSettingsPath) {
123100
return builder()
124101
.session(nonNull(session, "session cannot be null"))
125-
.globalSettingsPath(nonNull(globalSettingsPath, "globalSettingsPath cannot be null"))
126-
.projectSettingsPath(nonNull(projectSettingsPath, "projectSettingsPath cannot be null"))
127-
.userSettingsPath(nonNull(userSettingsPath, "userSettingsPath cannot be null"))
102+
.globalSettingsSource(
103+
globalSettingsPath != null && Files.exists(globalSettingsPath)
104+
? Source.fromPath(globalSettingsPath)
105+
: null)
106+
.projectSettingsSource(
107+
projectSettingsPath != null && Files.exists(projectSettingsPath)
108+
? Source.fromPath(projectSettingsPath)
109+
: null)
110+
.userSettingsSource(
111+
userSettingsPath != null && Files.exists(userSettingsPath)
112+
? Source.fromPath(userSettingsPath)
113+
: null)
128114
.build();
129115
}
130116

@@ -136,115 +122,64 @@ static SettingsBuilderRequestBuilder builder() {
136122
@NotThreadSafe
137123
class SettingsBuilderRequestBuilder {
138124
Session session;
139-
Path globalSettingsPath;
140125
Source globalSettingsSource;
141-
Path projectSettingsPath;
142126
Source projectSettingsSource;
143-
Path userSettingsPath;
144127
Source userSettingsSource;
145128

146129
public SettingsBuilderRequestBuilder session(Session session) {
147130
this.session = session;
148131
return this;
149132
}
150133

151-
public SettingsBuilderRequestBuilder globalSettingsPath(Path globalSettingsPath) {
152-
this.globalSettingsPath = globalSettingsPath;
153-
return this;
154-
}
155-
156134
public SettingsBuilderRequestBuilder globalSettingsSource(Source globalSettingsSource) {
157135
this.globalSettingsSource = globalSettingsSource;
158136
return this;
159137
}
160138

161-
public SettingsBuilderRequestBuilder projectSettingsPath(Path projectSettingsPath) {
162-
this.projectSettingsPath = projectSettingsPath;
163-
return this;
164-
}
165-
166139
public SettingsBuilderRequestBuilder projectSettingsSource(Source projectSettingsSource) {
167140
this.projectSettingsSource = projectSettingsSource;
168141
return this;
169142
}
170143

171-
public SettingsBuilderRequestBuilder userSettingsPath(Path userSettingsPath) {
172-
this.userSettingsPath = userSettingsPath;
173-
return this;
174-
}
175-
176144
public SettingsBuilderRequestBuilder userSettingsSource(Source userSettingsSource) {
177145
this.userSettingsSource = userSettingsSource;
178146
return this;
179147
}
180148

181149
public SettingsBuilderRequest build() {
182150
return new DefaultSettingsBuilderRequest(
183-
session,
184-
globalSettingsPath,
185-
globalSettingsSource,
186-
projectSettingsPath,
187-
projectSettingsSource,
188-
userSettingsPath,
189-
userSettingsSource);
151+
session, globalSettingsSource, projectSettingsSource, userSettingsSource);
190152
}
191153

192154
private static class DefaultSettingsBuilderRequest extends BaseRequest implements SettingsBuilderRequest {
193-
private final Path globalSettingsPath;
194155
private final Source globalSettingsSource;
195-
private final Path projectSettingsPath;
196156
private final Source projectSettingsSource;
197-
private final Path userSettingsPath;
198157
private final Source userSettingsSource;
199158

200159
@SuppressWarnings("checkstyle:ParameterNumber")
201160
DefaultSettingsBuilderRequest(
202161
@Nonnull Session session,
203-
@Nullable Path globalSettingsPath,
204162
@Nullable Source globalSettingsSource,
205-
@Nullable Path projectSettingsPath,
206163
@Nullable Source projectSettingsSource,
207-
@Nullable Path userSettingsPath,
208164
@Nullable Source userSettingsSource) {
209165
super(session);
210-
this.globalSettingsPath = globalSettingsPath;
211166
this.globalSettingsSource = globalSettingsSource;
212-
this.projectSettingsPath = projectSettingsPath;
213167
this.projectSettingsSource = projectSettingsSource;
214-
this.userSettingsPath = userSettingsPath;
215168
this.userSettingsSource = userSettingsSource;
216169
}
217170

218-
@Nonnull
219-
@Override
220-
public Optional<Path> getGlobalSettingsPath() {
221-
return Optional.ofNullable(globalSettingsPath);
222-
}
223-
224171
@Nonnull
225172
@Override
226173
public Optional<Source> getGlobalSettingsSource() {
227174
return Optional.ofNullable(globalSettingsSource);
228175
}
229176

230-
@Nonnull
231-
@Override
232-
public Optional<Path> getProjectSettingsPath() {
233-
return Optional.ofNullable(projectSettingsPath);
234-
}
235-
236177
@Nonnull
237178
@Override
238179
public Optional<Source> getProjectSettingsSource() {
239180
return Optional.ofNullable(projectSettingsSource);
240181
}
241182

242-
@Nonnull
243-
@Override
244-
public Optional<Path> getUserSettingsPath() {
245-
return Optional.ofNullable(userSettingsPath);
246-
}
247-
248183
@Nonnull
249184
@Override
250185
public Optional<Source> getUserSettingsSource() {

api/maven-api-core/src/main/java/org/apache/maven/api/services/Source.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.apache.maven.api.annotations.Nonnull;
2828
import org.apache.maven.api.annotations.Nullable;
2929

30+
import static org.apache.maven.api.services.BaseRequest.nonNull;
31+
3032
/**
3133
* Provides access to the contents of a source independently of the
3234
* backing store (e.g. file system, database, memory).
@@ -86,4 +88,12 @@ public interface Source {
8688
* @return related source or <code>null</code> if no such source
8789
*/
8890
Source resolve(String relative);
91+
92+
/**
93+
* Creates a Source for the following Path
94+
*/
95+
@Nonnull
96+
static Source fromPath(@Nonnull Path path) {
97+
return new PathSource(nonNull(path, "path cannot be null"));
98+
}
8999
}

0 commit comments

Comments
 (0)