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 86568510a746..bc216c9d7819 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 @@ default SettingsBuilderResult build( @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 95923fab9211..ee7692d9a973 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 @@ class SettingsBuilderRequestBuilder { Session session; Path globalSettingsPath; Source globalSettingsSource; + Path projectSettingsPath; + Source projectSettingsSource; Path userSettingsPath; Source userSettingsSource; @@ -120,6 +158,16 @@ public SettingsBuilderRequestBuilder globalSettingsSource(Source globalSettingsS 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 SettingsBuilderRequestBuilder userSettingsSource(Source userSettingsSourc 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 @@ private static class DefaultSettingsBuilderRequest extends BaseRequest implement @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 Optional getGlobalSettingsSource() { 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 9e9221aaa49f..f1ec2e5bdc5b 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 67acbafac9ba..a693a3c99187 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 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 SettingsBuildingResult build(SettingsBuildingRequest request) throws Sett 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 @@ private Source getSettingsSource(File settingsFile, Source settingsSource) { } 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,22 @@ private Settings readSettings( 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()); + 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 8372d2f031a7..71eb44663ce6 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 DefaultSettingsBuildingRequest setGlobalSettingsSource(SettingsSource glo 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/SettingsBuildingRequest.java b/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsBuildingRequest.java index b13f30c6cc4a..d1e28d18d7d5 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,37 @@ 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 + */ + 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}. + */ + DefaultSettingsBuildingRequest setProjectSettingsFile(File projectSettingsFile); + + /** + * Gets the project settings source. + * + * @return The project settings source or {@code null} if none. + */ + 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}. + */ + 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 3706775f73b7..082aa4c024f9 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 @@ -50,6 +50,36 @@ 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) { + if (settings.getLocalRepository() != null && !settings.getLocalRepository().isEmpty()) { + addViolation(problems, Severity.WARNING, "localRepository", null, "is not supported on project settings."); + } + if (settings.getInteractiveMode() != null && !settings.getInteractiveMode()) { + addViolation(problems, Severity.WARNING, "interactiveMode", null, "is not supported on project settings."); + } + if (settings.isOffline()) { + addViolation(problems, Severity.WARNING, "offline", null, "is not supported on project settings."); + } + if (!settings.getProxies().isEmpty()) { + addViolation(problems, Severity.WARNING, "proxies", null, "are not supported on project settings."); + } + List servers = settings.getServers(); + for (int i = 0; i < servers.size(); i++) { + Server server = servers.get(i); + validateStringEmpty(problems, "servers.server[" + i + "].username", server.getUsername(), null, "is not supported on project settings."); + validateStringEmpty(problems, "servers.server[" + i + "].password", server.getPassword(), null, "is not supported on project settings."); + validateStringEmpty(problems, "servers.server[" + i + "].privateKey", server.getPrivateKey(), null, "is not supported on project settings."); + validateStringEmpty(problems, "servers.server[" + i + "].passphrase", server.getPassphrase(), null, "is not supported on project settings."); + validateStringEmpty(problems, "servers.server[" + i + "].filePermissions", server.getFilePermissions(), null, "is not supported on project settings."); + validateStringEmpty(problems, "servers.server[" + i + "].directoryPermissions", server.getDirectoryPermissions(), null, "is not supported on project settings."); + } + } + if (settings.isUsePluginRegistry()) { addViolation(problems, Severity.WARNING, "usePluginRegistry", null, "is deprecated and has no effect."); } @@ -209,6 +239,25 @@ private void validateRepositories(SettingsProblemCollector problems, List + *
    + *
  • string.length == null + *
  • string.length == 0 + *
+ */ + private static boolean validateStringEmpty( + SettingsProblemCollector problems, String fieldName, String string, String sourceHint, String message) { + if (string == null || string.length() == 0) { + return true; + } + + addViolation(problems, Severity.WARNING, fieldName, sourceHint, 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 66bd2f738851..9eb3c1ffed72 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); + } }