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-8050] emit warn in case of repo id clashes between settings and POM #1412

Merged
merged 3 commits into from
May 18, 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
1 change: 1 addition & 0 deletions maven-model-builder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ under the License.
<exclude>org.apache.maven.model.superpom.SuperPomProvider#getSuperModel(java.lang.String):METHOD_RETURN_TYPE_CHANGED</exclude>
<exclude>org.apache.maven.model.validation.DefaultModelValidator#validateDependencyVersion(org.apache.maven.model.building.ModelProblemCollector,org.apache.maven.model.Dependency,java.lang.String):METHOD_REMOVED</exclude>
<exclude>org.apache.maven.model.validation.ModelValidator#validateFileModel(org.apache.maven.model.Model,org.apache.maven.model.building.ModelBuildingRequest,org.apache.maven.model.building.ModelProblemCollector):METHOD_NEW_DEFAULT</exclude>
<exclude>org.apache.maven.model.validation.ModelValidator#validateExternalProfiles(java.util.List,org.apache.maven.model.Model,org.apache.maven.model.building.ModelBuildingRequest,org.apache.maven.model.building.ModelProblemCollector):METHOD_NEW_DEFAULT</exclude>
</excludes>
</parameter>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ private void activateFileModel(
profileInjector.injectProfile(inputModel, activeProfile, request, problems);
}

modelValidator.validateExternalProfiles(activeExternalProfiles, inputModel, request, problems);
for (Profile activeProfile : activeExternalProfiles) {
profileInjector.injectProfile(inputModel, activeProfile, request, problems);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
Expand Down Expand Up @@ -788,6 +789,54 @@ public void validateEffectiveModel(Model ma, ModelBuildingRequest request, Model
}
}

@Override
public void validateExternalProfiles(
List<org.apache.maven.model.Profile> activeExternalProfiles,
Model ma,
ModelBuildingRequest request,
ModelProblemCollector problems) {
org.apache.maven.api.model.Model m = ma.getDelegate();
// check for id clashes in repositories
for (Profile profile : activeExternalProfiles.stream()
.map(org.apache.maven.model.Profile::getDelegate)
.collect(Collectors.toList())) {
String externalRepositoriesSource = "external profile with id '" + profile.getId() + "' in settings.xml";
validateUniqueRepositoryIds(
false, m.getRepositories(), profile.getRepositories(), externalRepositoriesSource, problems);
validateUniqueRepositoryIds(
true,
m.getPluginRepositories(),
profile.getPluginRepositories(),
externalRepositoriesSource,
problems);
}
}

private void validateUniqueRepositoryIds(
boolean isPluginRepository,
Collection<Repository> pomRepositories,
Collection<Repository> externalRepositories,
String externalRepositoriesSource,
ModelProblemCollector problems) {
for (Repository externalRepository : externalRepositories) {
Optional<Repository> clashingPomRepository = pomRepositories.stream()
.filter(r -> Objects.equals(r.getId(), externalRepository.getId()))
.filter(r -> !Objects.equals(r.getUrl(), externalRepository.getUrl()))
.findFirst();
if (clashingPomRepository.isPresent()) {
addViolation(
problems,
Severity.WARNING,
Version.BASE,
isPluginRepository ? "pluginRepositories.repository" : "repositories.repository",
clashingPomRepository.get().getId(),
"is overwritten by the repository with same id but having a different url from "
+ externalRepositoriesSource,
clashingPomRepository.get());
}
}
}

private void validate20RawDependencies(
ModelProblemCollector problems,
List<Dependency> dependencies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
*/
package org.apache.maven.model.validation;

import java.util.List;

import org.apache.maven.model.Model;
import org.apache.maven.model.Profile;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.model.building.ModelProblemCollector;

Expand Down Expand Up @@ -49,6 +52,24 @@ default void validateFileModel(Model model, ModelBuildingRequest request, ModelP
*/
void validateRawModel(Model model, ModelBuildingRequest request, ModelProblemCollector problems);

/**
* Checks the specified (raw) model for clashes with the passed active external profiles. The raw model is the
* file model + buildpom filter transformation and has not been subjected to inheritance, interpolation or profile/default injection.
*
* @param activeExternalProfiles the active profiles coming from external sources (settings.xml), must not be {@code null}
* @param model The model to validate, must not be {@code null}.
* @param request The model building request that holds further settings, must not be {@code null}.
* @param problems The container used to collect problems that were encountered, must not be {@code null}.
* @since 4.0.0
*/
default void validateExternalProfiles(
List<Profile> activeExternalProfiles,
Model model,
ModelBuildingRequest request,
ModelProblemCollector problems) {
// do nothing
}

/**
* Checks the specified (effective) model for missing or invalid values. The effective model is fully assembled and
* has undergone inheritance, interpolation and other model operations.
Expand Down