Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -486,37 +486,38 @@ protected List<CoreExtensions> readCoreExtensionsDescriptor(LocalContext context

// project
file = context.cwd.resolve(eff.get(Constants.MAVEN_PROJECT_EXTENSIONS));
loaded = readCoreExtensionsDescriptorFromFile(file);
loaded = readCoreExtensionsDescriptorFromFile(file, false);
if (!loaded.isEmpty()) {
result.add(new CoreExtensions(file, loaded));
}

// user
file = context.userHomeDirectory.resolve(eff.get(Constants.MAVEN_USER_EXTENSIONS));
loaded = readCoreExtensionsDescriptorFromFile(file);
loaded = readCoreExtensionsDescriptorFromFile(file, true);
if (!loaded.isEmpty()) {
result.add(new CoreExtensions(file, loaded));
}

// installation
file = context.installationDirectory.resolve(eff.get(Constants.MAVEN_INSTALLATION_EXTENSIONS));
loaded = readCoreExtensionsDescriptorFromFile(file);
loaded = readCoreExtensionsDescriptorFromFile(file, true);
if (!loaded.isEmpty()) {
result.add(new CoreExtensions(file, loaded));
}

return result.isEmpty() ? null : List.copyOf(result);
}

protected List<CoreExtension> readCoreExtensionsDescriptorFromFile(Path extensionsFile) {
protected List<CoreExtension> readCoreExtensionsDescriptorFromFile(Path extensionsFile, boolean allowMetaVersions) {
try {
if (extensionsFile != null && Files.exists(extensionsFile)) {
try (InputStream is = Files.newInputStream(extensionsFile)) {
return validateCoreExtensionsDescriptorFromFile(
extensionsFile,
List.copyOf(new CoreExtensionsStaxReader()
.read(is, true, new InputSource(extensionsFile.toString()))
.getExtensions()));
.getExtensions()),
allowMetaVersions);
}
}
return List.of();
Expand All @@ -526,23 +527,37 @@ protected List<CoreExtension> readCoreExtensionsDescriptorFromFile(Path extensio
}

protected List<CoreExtension> validateCoreExtensionsDescriptorFromFile(
Path extensionFile, List<CoreExtension> coreExtensions) {
Path extensionFile, List<CoreExtension> coreExtensions, boolean allowMetaVersions) {
Map<String, List<InputLocation>> gasLocations = new HashMap<>();
Map<String, List<InputLocation>> metaVersionLocations = new HashMap<>();
for (CoreExtension coreExtension : coreExtensions) {
String ga = coreExtension.getGroupId() + ":" + coreExtension.getArtifactId();
InputLocation location = coreExtension.getLocation("");
gasLocations.computeIfAbsent(ga, k -> new ArrayList<>()).add(location);
// TODO: metaversions could be extensible enum with these two values out of the box
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gnodet maybe not extensible enum right now, but where would be their proper place? Maybe not even in API (to draw attention) for start, just some constants somewhere?

if ("LATEST".equals(coreExtension.getVersion()) || "RELEASE".equals(coreExtension.getVersion())) {
metaVersionLocations.computeIfAbsent(ga, k -> new ArrayList<>()).add(location);
}
}
if (gasLocations.values().stream().noneMatch(l -> l.size() > 1)) {
return coreExtensions;
}
throw new IllegalStateException("Extension conflicts in file " + extensionFile + ": "
+ gasLocations.entrySet().stream()
.map(e -> e.getKey() + " defined on lines "
+ e.getValue().stream()
.map(l -> String.valueOf(l.getLineNumber()))
.collect(Collectors.joining(", ")))
.collect(Collectors.joining("; ")));
if (gasLocations.values().stream().anyMatch(l -> l.size() > 1)) {
throw new IllegalStateException("Extension conflicts in file " + extensionFile + ": "
+ gasLocations.entrySet().stream()
.map(e -> e.getKey() + " defined on lines "
+ e.getValue().stream()
.map(l -> String.valueOf(l.getLineNumber()))
.collect(Collectors.joining(", ")))
.collect(Collectors.joining("; ")));
}
if (!allowMetaVersions && !metaVersionLocations.isEmpty()) {
throw new IllegalStateException("Extension illegal version in file " + extensionFile + ": "
+ metaVersionLocations.entrySet().stream()
.map(e -> e.getKey() + " defined on lines "
+ e.getValue().stream()
.map(l -> String.valueOf(l.getLineNumber()))
.collect(Collectors.joining(", ")))
.collect(Collectors.joining("; ")));
}
return coreExtensions;
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.apache.maven.api.cli.CoreExtensions;
Expand Down Expand Up @@ -60,7 +61,7 @@ protected List<CoreExtension> selectCoreExtensions(C context, List<CoreExtension
for (CoreExtension coreExtension : coreExtensions.coreExtensions()) {
String key = coreExtension.getGroupId() + ":" + coreExtension.getArtifactId();
CoreExtension conflict = selectedExtensions.putIfAbsent(key, coreExtension);
if (conflict != null) {
if (conflict != null && !Objects.equals(coreExtension.getVersion(), conflict.getVersion())) {
conflicts.add(String.format(
"Conflicting extension %s: %s vs %s",
key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,38 @@ public DependencyResult resolveCoreExtension(
List<RemoteRepository> repositories,
RepositorySystemSession session)
throws PluginResolutionException {
return resolveInternal(plugin, null /* pluginArtifact */, dependencyFilter, repositories, session);
RequestTrace trace = RequestTrace.newChild(null, plugin);

Artifact pluginArtifact = toArtifact(plugin, session);

try {
DefaultRepositorySystemSession pluginSession = new DefaultRepositorySystemSession(session);
pluginSession.setArtifactDescriptorPolicy(new SimpleArtifactDescriptorPolicy(true, false));

ArtifactDescriptorRequest request =
new ArtifactDescriptorRequest(pluginArtifact, repositories, REPOSITORY_CONTEXT);
request.setTrace(trace);
ArtifactDescriptorResult result = repoSystem.readArtifactDescriptor(pluginSession, request);

for (MavenPluginDependenciesValidator dependenciesValidator : dependenciesValidators) {
dependenciesValidator.validate(session, pluginArtifact, result);
}

pluginArtifact = result.getArtifact();

if (logger.isWarnEnabled() && !result.getRelocations().isEmpty()) {
String message =
pluginArtifact instanceof RelocatedArtifact relocated ? ": " + relocated.getMessage() : "";
logger.warn(
"The extension {} has been relocated to {}{}",
result.getRelocations().get(0),
pluginArtifact,
message);
}
return resolveInternal(plugin, pluginArtifact, dependencyFilter, repositories, session);
} catch (ArtifactDescriptorException e) {
throw new PluginResolutionException(plugin, e.getResult().getExceptions(), e);
}
}

@Override
Expand Down
Loading