Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-8084] Make the v4 api usable outside the Maven runtime #1441

Merged
merged 10 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.api.services;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

class PathSource implements Source {

private final Path path;

PathSource(Path path) {
this.path = path;
}

@Override
public Path getPath() {
return path;
}

@Override
public InputStream openStream() throws IOException {
return Files.newInputStream(path);
}

@Override
public String getLocation() {
return path.toString();
}

@Override
public Source resolve(String relative) {
return new PathSource(path.resolve(relative));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
package org.apache.maven.api.services;

import java.nio.file.Path;
import java.util.List;

import org.apache.maven.api.Service;
import org.apache.maven.api.Session;
import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.settings.Settings;

/**
* Builds the effective settings from a user settings file and/or a global settings file.
Expand Down Expand Up @@ -97,4 +99,37 @@ default SettingsBuilderResult build(
@Nonnull Path userSettingsPath) {
return build(SettingsBuilderRequest.build(session, globalSettingsPath, projectSettingsPath, userSettingsPath));
}

/**
* Validate the specified settings.
*
* @param settings The settings to validate, must not be {@code null}.
* @return The list of problems that were encountered, must not be {@code null}.
*/
@Nonnull
default List<BuilderProblem> validate(@Nonnull Settings settings) {
return validate(settings, false);
}

/**
* Validate the specified settings.
*
* @param settings The settings to validate, must not be {@code null}.
* @param isProjectSettings Boolean indicating if the validation is for project settings or user / global settings.
* @return The list of problems that were encountered, must not be {@code null}.
*/
@Nonnull
List<BuilderProblem> validate(@Nonnull Settings settings, boolean isProjectSettings);

/**
* Convert a model profile to a settings profile.
*/
@Nonnull
org.apache.maven.api.settings.Profile convert(@Nonnull org.apache.maven.api.model.Profile profile);

/**
* Convert a settings profile to a model profile.
*/
@Nonnull
org.apache.maven.api.model.Profile convert(@Nonnull org.apache.maven.api.settings.Profile profile);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.apache.maven.api.services;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.maven.api.annotations.Experimental;

/**
Expand All @@ -27,13 +30,24 @@
*/
@Experimental
public class SettingsBuilderException extends MavenException {

private final List<BuilderProblem> problems;

/**
* @param message the message to give
* @param e the {@link Exception}
*/
public SettingsBuilderException(String message, Exception e) {
super(message, e);
this.problems = List.of();
}

// TODO: add SettingsBuilderResult
public SettingsBuilderException(String message, List<BuilderProblem> problems) {
super(message + ": " + problems.stream().map(BuilderProblem::toString).collect(Collectors.joining(", ")), null);
this.problems = List.copyOf(problems);
}

public List<BuilderProblem> getProblems() {
return problems;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.maven.api.services;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;

Expand All @@ -40,14 +41,6 @@ public interface SettingsBuilderRequest {
@Nonnull
Session getSession();

/**
* Gets the global settings path.
*
* @return the global settings path or {@code null} if none
*/
@Nonnull
Optional<Path> getGlobalSettingsPath();

/**
* Gets the global settings source.
*
Expand All @@ -64,22 +57,6 @@ public interface SettingsBuilderRequest {
@Nonnull
Optional<Source> getProjectSettingsSource();

/**
* Gets the project settings path.
*
* @return the project settings path or {@code null} if none
*/
@Nonnull
Optional<Path> getProjectSettingsPath();

/**
* Gets the user settings path.
*
* @return the user settings path or {@code null} if none
*/
@Nonnull
Optional<Path> getUserSettingsPath();

/**
* Gets the user settings source.
*
Expand All @@ -97,34 +74,43 @@ static SettingsBuilderRequest build(
@Nonnull
static SettingsBuilderRequest build(
@Nonnull Session session, @Nonnull Path globalSettingsPath, @Nonnull Path userSettingsPath) {
return build(session, globalSettingsPath, null, userSettingsPath);
return build(session, Source.fromPath(globalSettingsPath), null, Source.fromPath(userSettingsPath));
}

@Nonnull
static SettingsBuilderRequest build(
@Nonnull Session session,
@Nonnull Source globalSettingsSource,
@Nonnull Source projectSettingsSource,
@Nonnull Source userSettingsSource) {
@Nullable Source globalSettingsSource,
@Nullable Source projectSettingsSource,
@Nullable Source userSettingsSource) {
return builder()
.session(nonNull(session, "session cannot be null"))
.globalSettingsSource(nonNull(globalSettingsSource, "globalSettingsSource cannot be null"))
.projectSettingsSource(nonNull(projectSettingsSource, "projectSettingsSource cannot be null"))
.userSettingsSource(nonNull(userSettingsSource, "userSettingsSource cannot be null"))
.globalSettingsSource(globalSettingsSource)
.projectSettingsSource(projectSettingsSource)
.userSettingsSource(userSettingsSource)
.build();
}

@Nonnull
static SettingsBuilderRequest build(
@Nonnull Session session,
@Nonnull Path globalSettingsPath,
@Nonnull Path projectSettingsPath,
@Nonnull Path userSettingsPath) {
@Nullable Path globalSettingsPath,
@Nullable Path projectSettingsPath,
@Nullable Path userSettingsPath) {
return builder()
.session(nonNull(session, "session cannot be null"))
.globalSettingsPath(nonNull(globalSettingsPath, "globalSettingsPath cannot be null"))
.projectSettingsPath(nonNull(projectSettingsPath, "projectSettingsPath cannot be null"))
.userSettingsPath(nonNull(userSettingsPath, "userSettingsPath cannot be null"))
.globalSettingsSource(
globalSettingsPath != null && Files.exists(globalSettingsPath)
? Source.fromPath(globalSettingsPath)
: null)
.projectSettingsSource(
projectSettingsPath != null && Files.exists(projectSettingsPath)
? Source.fromPath(projectSettingsPath)
: null)
.userSettingsSource(
userSettingsPath != null && Files.exists(userSettingsPath)
? Source.fromPath(userSettingsPath)
: null)
.build();
}

Expand All @@ -136,115 +122,64 @@ static SettingsBuilderRequestBuilder builder() {
@NotThreadSafe
class SettingsBuilderRequestBuilder {
Session session;
Path globalSettingsPath;
Source globalSettingsSource;
Path projectSettingsPath;
Source projectSettingsSource;
Path userSettingsPath;
Source userSettingsSource;

public SettingsBuilderRequestBuilder session(Session session) {
this.session = session;
return this;
}

public SettingsBuilderRequestBuilder globalSettingsPath(Path globalSettingsPath) {
this.globalSettingsPath = globalSettingsPath;
return this;
}

public SettingsBuilderRequestBuilder globalSettingsSource(Source globalSettingsSource) {
this.globalSettingsSource = globalSettingsSource;
return this;
}

public SettingsBuilderRequestBuilder projectSettingsPath(Path projectSettingsPath) {
this.projectSettingsPath = projectSettingsPath;
return this;
}

public SettingsBuilderRequestBuilder projectSettingsSource(Source projectSettingsSource) {
this.projectSettingsSource = projectSettingsSource;
return this;
}

public SettingsBuilderRequestBuilder userSettingsPath(Path userSettingsPath) {
this.userSettingsPath = userSettingsPath;
return this;
}

public SettingsBuilderRequestBuilder userSettingsSource(Source userSettingsSource) {
this.userSettingsSource = userSettingsSource;
return this;
}

public SettingsBuilderRequest build() {
return new DefaultSettingsBuilderRequest(
session,
globalSettingsPath,
globalSettingsSource,
projectSettingsPath,
projectSettingsSource,
userSettingsPath,
userSettingsSource);
session, globalSettingsSource, projectSettingsSource, userSettingsSource);
}

private static class DefaultSettingsBuilderRequest extends BaseRequest implements SettingsBuilderRequest {
private final Path globalSettingsPath;
private final Source globalSettingsSource;
private final Path projectSettingsPath;
private final Source projectSettingsSource;
private final Path userSettingsPath;
private final Source userSettingsSource;

@SuppressWarnings("checkstyle:ParameterNumber")
DefaultSettingsBuilderRequest(
@Nonnull Session session,
@Nullable Path globalSettingsPath,
@Nullable Source globalSettingsSource,
@Nullable Path projectSettingsPath,
@Nullable Source projectSettingsSource,
@Nullable Path userSettingsPath,
@Nullable Source userSettingsSource) {
super(session);
this.globalSettingsPath = globalSettingsPath;
this.globalSettingsSource = globalSettingsSource;
this.projectSettingsPath = projectSettingsPath;
this.projectSettingsSource = projectSettingsSource;
this.userSettingsPath = userSettingsPath;
this.userSettingsSource = userSettingsSource;
}

@Nonnull
@Override
public Optional<Path> getGlobalSettingsPath() {
return Optional.ofNullable(globalSettingsPath);
}

@Nonnull
@Override
public Optional<Source> getGlobalSettingsSource() {
return Optional.ofNullable(globalSettingsSource);
}

@Nonnull
@Override
public Optional<Path> getProjectSettingsPath() {
return Optional.ofNullable(projectSettingsPath);
}

@Nonnull
@Override
public Optional<Source> getProjectSettingsSource() {
return Optional.ofNullable(projectSettingsSource);
}

@Nonnull
@Override
public Optional<Path> getUserSettingsPath() {
return Optional.ofNullable(userSettingsPath);
}

@Nonnull
@Override
public Optional<Source> getUserSettingsSource() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;

import static org.apache.maven.api.services.BaseRequest.nonNull;

/**
* Provides access to the contents of a source independently of the
* backing store (e.g. file system, database, memory).
Expand Down Expand Up @@ -86,4 +88,12 @@ public interface Source {
* @return related source or <code>null</code> if no such source
*/
Source resolve(String relative);

/**
* Creates a Source for the following Path
*/
@Nonnull
static Source fromPath(@Nonnull Path path) {
return new PathSource(nonNull(path, "path cannot be null"));
}
}
Loading