diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/SettingsBuilder.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/SettingsBuilder.java index 86568510a7..bc216c9d78 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/SettingsBuilder.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/SettingsBuilder.java @@ -48,7 +48,7 @@ public interface SettingsBuilder extends Service { @Nonnull default SettingsBuilderResult build( @Nonnull Session session, @Nonnull Source globalSettingsSource, @Nonnull Source userSettingsSource) { - return build(SettingsBuilderRequest.build(session, globalSettingsSource, userSettingsSource)); + return build(session, globalSettingsSource, null, userSettingsSource); } /** @@ -60,6 +60,37 @@ public interface SettingsBuilder extends Service { @Nonnull default SettingsBuilderResult build( @Nonnull Session session, @Nonnull Path globalSettingsPath, @Nonnull Path userSettingsPath) { - return build(SettingsBuilderRequest.build(session, globalSettingsPath, userSettingsPath)); + return build(session, globalSettingsPath, null, userSettingsPath); + } + + /** + * Builds the effective settings of the specified settings sources. + * + * @return the result of the settings building, never {@code null} + * @throws SettingsBuilderException if the effective settings could not be built + */ + @Nonnull + default SettingsBuilderResult build( + @Nonnull Session session, + @Nonnull Source globalSettingsSource, + @Nonnull Source projectSettingsSource, + @Nonnull Source userSettingsSource) { + return build( + SettingsBuilderRequest.build(session, globalSettingsSource, projectSettingsSource, userSettingsSource)); + } + + /** + * Builds the effective settings of the specified settings paths. + * + * @return the result of the settings building, never {@code null} + * @throws SettingsBuilderException if the effective settings could not be built + */ + @Nonnull + default SettingsBuilderResult build( + @Nonnull Session session, + @Nonnull Path globalSettingsPath, + @Nonnull Path projectSettingsPath, + @Nonnull Path userSettingsPath) { + return build(SettingsBuilderRequest.build(session, globalSettingsPath, projectSettingsPath, userSettingsPath)); } } diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/SettingsBuilderRequest.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/SettingsBuilderRequest.java index 95923fab92..ee7692d9a9 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/SettingsBuilderRequest.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/SettingsBuilderRequest.java @@ -56,6 +56,22 @@ public interface SettingsBuilderRequest { @Nonnull Optional getGlobalSettingsSource(); + /** + * Gets the project settings source. + * + * @return the project settings source or {@code null} if none + */ + @Nonnull + Optional getProjectSettingsSource(); + + /** + * Gets the project settings path. + * + * @return the project settings path or {@code null} if none + */ + @Nonnull + Optional getProjectSettingsPath(); + /** * Gets the user settings path. * @@ -75,19 +91,39 @@ public interface SettingsBuilderRequest { @Nonnull static SettingsBuilderRequest build( @Nonnull Session session, @Nonnull Source globalSettingsSource, @Nonnull Source userSettingsSource) { + return build(session, globalSettingsSource, null, userSettingsSource); + } + + @Nonnull + static SettingsBuilderRequest build( + @Nonnull Session session, @Nonnull Path globalSettingsPath, @Nonnull Path userSettingsPath) { + return build(session, globalSettingsPath, null, userSettingsPath); + } + + @Nonnull + static SettingsBuilderRequest build( + @Nonnull Session session, + @Nonnull Source globalSettingsSource, + @Nonnull Source projectSettingsSource, + @Nonnull 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")) .build(); } @Nonnull static SettingsBuilderRequest build( - @Nonnull Session session, @Nonnull Path globalSettingsPath, @Nonnull Path userSettingsPath) { + @Nonnull Session session, + @Nonnull Path globalSettingsPath, + @Nonnull Path projectSettingsPath, + @Nonnull 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")) .build(); } @@ -102,6 +138,8 @@ public interface SettingsBuilderRequest { Session session; Path globalSettingsPath; Source globalSettingsSource; + Path projectSettingsPath; + Source projectSettingsSource; Path userSettingsPath; Source userSettingsSource; @@ -120,6 +158,16 @@ public interface SettingsBuilderRequest { 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; @@ -132,12 +180,20 @@ public interface SettingsBuilderRequest { public SettingsBuilderRequest build() { return new DefaultSettingsBuilderRequest( - session, globalSettingsPath, globalSettingsSource, userSettingsPath, userSettingsSource); + session, + globalSettingsPath, + globalSettingsSource, + projectSettingsPath, + projectSettingsSource, + userSettingsPath, + 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; @@ -146,11 +202,15 @@ public interface SettingsBuilderRequest { @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; } @@ -167,6 +227,18 @@ public interface SettingsBuilderRequest { return Optional.ofNullable(globalSettingsSource); } + @Nonnull + @Override + public Optional getProjectSettingsPath() { + return Optional.ofNullable(projectSettingsPath); + } + + @Nonnull + @Override + public Optional getProjectSettingsSource() { + return Optional.ofNullable(projectSettingsSource); + } + @Nonnull @Override public Optional getUserSettingsPath() { diff --git a/api/maven-api-settings/src/main/mdo/settings.mdo b/api/maven-api-settings/src/main/mdo/settings.mdo index 9e9221aaa4..f1ec2e5bdc 100644 --- a/api/maven-api-settings/src/main/mdo/settings.mdo +++ b/api/maven-api-settings/src/main/mdo/settings.mdo @@ -53,6 +53,7 @@ com.github.siom79.japicmp japicmp-maven-plugin - + + + + org.apache.maven.settings.validation.SettingsValidator#validate(org.apache.maven.settings.Settings,boolean,org.apache.maven.settings.building.SettingsProblemCollector):METHOD_NEW_DEFAULT + + + diff --git a/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuilder.java b/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuilder.java index 67acbafac9..9eddc5c8b4 100644 --- a/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuilder.java +++ b/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuilder.java @@ -32,6 +32,7 @@ import java.util.Map; import org.apache.maven.building.FileSource; import org.apache.maven.building.Source; +import org.apache.maven.settings.Server; import org.apache.maven.settings.Settings; import org.apache.maven.settings.TrackableBase; import org.apache.maven.settings.io.SettingsParseException; @@ -90,12 +91,17 @@ public class DefaultSettingsBuilder implements SettingsBuilder { Source globalSettingsSource = getSettingsSource(request.getGlobalSettingsFile(), request.getGlobalSettingsSource()); - Settings globalSettings = readSettings(globalSettingsSource, request, problems); + Settings globalSettings = readSettings(globalSettingsSource, false, request, problems); + + Source projectSettingsSource = + getSettingsSource(request.getProjectSettingsFile(), request.getProjectSettingsSource()); + Settings projectSettings = readSettings(projectSettingsSource, true, request, problems); Source userSettingsSource = getSettingsSource(request.getUserSettingsFile(), request.getUserSettingsSource()); - Settings userSettings = readSettings(userSettingsSource, request, problems); + Settings userSettings = readSettings(userSettingsSource, false, request, problems); - settingsMerger.merge(userSettings, globalSettings, TrackableBase.GLOBAL_LEVEL); + settingsMerger.merge(projectSettings, globalSettings, TrackableBase.GLOBAL_LEVEL); + settingsMerger.merge(userSettings, projectSettings, TrackableBase.PROJECT_LEVEL); problems.setSource(""); @@ -139,7 +145,10 @@ public class DefaultSettingsBuilder implements SettingsBuilder { } private Settings readSettings( - Source settingsSource, SettingsBuildingRequest request, DefaultSettingsProblemCollector problems) { + Source settingsSource, + boolean isProjectSettings, + SettingsBuildingRequest request, + DefaultSettingsProblemCollector problems) { if (settingsSource == null) { return new Settings(); } @@ -179,7 +188,23 @@ public class DefaultSettingsBuilder implements SettingsBuilder { return new Settings(); } - settingsValidator.validate(settings, problems); + settingsValidator.validate(settings, isProjectSettings, problems); + + if (isProjectSettings) { + settings.setLocalRepository(null); + settings.setInteractiveMode(true); + settings.setOffline(false); + settings.setProxies(Collections.emptyList()); + settings.setUsePluginRegistry(false); + for (Server server : settings.getServers()) { + server.setUsername(null); + server.setPassword(null); + server.setPrivateKey(null); + server.setPassword(null); + server.setFilePermissions(null); + server.setDirectoryPermissions(null); + } + } return settings; } diff --git a/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuildingRequest.java b/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuildingRequest.java index 8372d2f031..71eb44663c 100644 --- a/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuildingRequest.java +++ b/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuildingRequest.java @@ -30,10 +30,14 @@ public class DefaultSettingsBuildingRequest implements SettingsBuildingRequest { private File globalSettingsFile; + private File projectSettingsFile; + private File userSettingsFile; private SettingsSource globalSettingsSource; + private SettingsSource projectSettingsSource; + private SettingsSource userSettingsSource; private Properties systemProperties; @@ -64,6 +68,30 @@ public class DefaultSettingsBuildingRequest implements SettingsBuildingRequest { return this; } + @Override + public File getProjectSettingsFile() { + return projectSettingsFile; + } + + @Override + public DefaultSettingsBuildingRequest setProjectSettingsFile(File projectSettingsFile) { + this.projectSettingsFile = projectSettingsFile; + + return this; + } + + @Override + public SettingsSource getProjectSettingsSource() { + return projectSettingsSource; + } + + @Override + public DefaultSettingsBuildingRequest setProjectSettingsSource(SettingsSource projectSettingsSource) { + this.projectSettingsSource = projectSettingsSource; + + return this; + } + @Override public File getUserSettingsFile() { return userSettingsFile; diff --git a/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsBuilder.java b/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsBuilder.java index 5c8533a192..b3618791ba 100644 --- a/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsBuilder.java +++ b/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsBuilder.java @@ -19,7 +19,8 @@ package org.apache.maven.settings.building; /** - * Builds the effective settings from a user settings file and/or a global settings file. + * Builds the effective settings from a user settings file, a project settings file + * and/or a global settings file. * * @author Benjamin Bentmann */ diff --git a/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsBuildingRequest.java b/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsBuildingRequest.java index b13f30c6cc..575f1d5391 100644 --- a/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsBuildingRequest.java +++ b/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsBuildingRequest.java @@ -60,6 +60,40 @@ public interface SettingsBuildingRequest { */ SettingsBuildingRequest setGlobalSettingsSource(SettingsSource globalSettingsSource); + /** + * Gets the project settings file. + * + * @return The project settings file or {@code null} if none. + * @since 4.0.0 + */ + File getProjectSettingsFile(); + + /** + * Sets the project settings file. A non-existent settings file is equivalent to empty settings. + * + * @param projectSettingsFile The project settings file, may be {@code null} to disable project settings. + * @return This request, never {@code null}. + * @since 4.0.0 + */ + DefaultSettingsBuildingRequest setProjectSettingsFile(File projectSettingsFile); + + /** + * Gets the project settings source. + * + * @return The project settings source or {@code null} if none. + * @since 4.0.0 + */ + SettingsSource getProjectSettingsSource(); + + /** + * Sets the project settings source. + * + * @param projectSettingsSource The project settings source, may be {@code null} to disable global settings. + * @return This request, never {@code null}. + * @since 4.0.0 + */ + SettingsBuildingRequest setProjectSettingsSource(SettingsSource projectSettingsSource); + /** * Gets the user settings file. * diff --git a/maven-settings-builder/src/main/java/org/apache/maven/settings/validation/DefaultSettingsValidator.java b/maven-settings-builder/src/main/java/org/apache/maven/settings/validation/DefaultSettingsValidator.java index 5ed3d37f70..f3a5cce480 100644 --- a/maven-settings-builder/src/main/java/org/apache/maven/settings/validation/DefaultSettingsValidator.java +++ b/maven-settings-builder/src/main/java/org/apache/maven/settings/validation/DefaultSettingsValidator.java @@ -49,6 +49,44 @@ public class DefaultSettingsValidator implements SettingsValidator { @Override public void validate(Settings settings, SettingsProblemCollector problems) { + validate(settings, false, problems); + } + + @Override + public void validate(Settings settings, boolean isProjectSettings, SettingsProblemCollector problems) { + if (isProjectSettings) { + String msgS = "is not supported on project settings."; + String msgP = "are not supported on project settings."; + if (settings.getLocalRepository() != null + && !settings.getLocalRepository().isEmpty()) { + addViolation(problems, Severity.WARNING, "localRepository", null, msgS); + } + if (settings.getInteractiveMode() != null && !settings.getInteractiveMode()) { + addViolation(problems, Severity.WARNING, "interactiveMode", null, msgS); + } + if (settings.isOffline()) { + addViolation(problems, Severity.WARNING, "offline", null, msgS); + } + if (!settings.getProxies().isEmpty()) { + addViolation(problems, Severity.WARNING, "proxies", null, msgP); + } + if (settings.isUsePluginRegistry()) { + addViolation(problems, Severity.WARNING, "usePluginRegistry", null, msgS); + } + List servers = settings.getServers(); + for (int i = 0; i < servers.size(); i++) { + Server server = servers.get(i); + String serverField = "servers.server[" + i + "]"; + validateStringEmpty(problems, serverField + ".username", server.getUsername(), msgS); + validateStringEmpty(problems, serverField + ".password", server.getPassword(), msgS); + validateStringEmpty(problems, serverField + ".privateKey", server.getPrivateKey(), msgS); + validateStringEmpty(problems, serverField + ".passphrase", server.getPassphrase(), msgS); + validateStringEmpty(problems, serverField + ".filePermissions", server.getFilePermissions(), msgS); + validateStringEmpty( + problems, serverField + ".directoryPermissions", server.getDirectoryPermissions(), msgS); + } + } + if (settings.isUsePluginRegistry()) { addViolation(problems, Severity.WARNING, "usePluginRegistry", null, "is deprecated and has no effect."); } @@ -207,6 +245,25 @@ public class DefaultSettingsValidator implements SettingsValidator { // Field validation // ---------------------------------------------------------------------- + /** + * Asserts: + *

+ *

    + *
  • string.length == null + *
  • string.length == 0 + *
+ */ + private static boolean validateStringEmpty( + SettingsProblemCollector problems, String fieldName, String string, String message) { + if (string == null || string.length() == 0) { + return true; + } + + addViolation(problems, Severity.WARNING, fieldName, null, message); + + return false; + } + /** * Asserts: *

diff --git a/maven-settings-builder/src/main/java/org/apache/maven/settings/validation/SettingsValidator.java b/maven-settings-builder/src/main/java/org/apache/maven/settings/validation/SettingsValidator.java index 66bd2f7388..9eb3c1ffed 100644 --- a/maven-settings-builder/src/main/java/org/apache/maven/settings/validation/SettingsValidator.java +++ b/maven-settings-builder/src/main/java/org/apache/maven/settings/validation/SettingsValidator.java @@ -35,4 +35,15 @@ public interface SettingsValidator { * @param problems The container used to collect problems that were encountered, must not be {@code null}. */ void validate(Settings settings, SettingsProblemCollector problems); + + /** + * 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. + * @param problems The container used to collect problems that were encountered, must not be {@code null}. + */ + default void validate(Settings settings, boolean isProjectSettings, SettingsProblemCollector problems) { + validate(settings, problems); + } }