-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Feat: Add id attribute (gav) support to Dependency, Exclusion, Mixin #11904
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -29,6 +29,8 @@ | |||||||||
| import org.apache.maven.api.di.Singleton; | ||||||||||
| import org.apache.maven.api.model.Build; | ||||||||||
| import org.apache.maven.api.model.Dependency; | ||||||||||
| import org.apache.maven.api.model.Exclusion; | ||||||||||
| import org.apache.maven.api.model.Mixin; | ||||||||||
| import org.apache.maven.api.model.Model; | ||||||||||
| import org.apache.maven.api.model.Plugin; | ||||||||||
| import org.apache.maven.api.services.ModelBuilderRequest; | ||||||||||
|
|
@@ -49,6 +51,9 @@ public class DefaultModelNormalizer implements ModelNormalizer { | |||||||||
| public Model mergeDuplicates(Model model, ModelBuilderRequest request, ModelProblemCollector problems) { | ||||||||||
| Model.Builder builder = Model.newBuilder(model); | ||||||||||
|
|
||||||||||
| // Expand id attributes on mixins | ||||||||||
| builder.mixins(injectList(model.getMixins(), this::expandMixinGav)); | ||||||||||
|
|
||||||||||
| Build build = model.getBuild(); | ||||||||||
| if (build != null) { | ||||||||||
| List<Plugin> plugins = build.getPlugins(); | ||||||||||
|
|
@@ -76,15 +81,20 @@ public Model mergeDuplicates(Model model, ModelBuilderRequest request, ModelProb | |||||||||
| * the way 2.x works. When we're in strict mode, the removal of duplicates just saves other merging steps from | ||||||||||
| * aftereffects and bogus error messages. | ||||||||||
| */ | ||||||||||
| // Expand id attributes on dependencies (and their exclusions), then deduplicate | ||||||||||
| List<Dependency> dependencies = model.getDependencies(); | ||||||||||
| Map<String, Dependency> normalized = new LinkedHashMap<>(dependencies.size() * 2); | ||||||||||
| List<Dependency> expanded = injectList(dependencies, this::expandDependencyId); | ||||||||||
| if (expanded != null) { | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This only expands Should the expansion also apply to:
Without this, a user writing |
||||||||||
| dependencies = expanded; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| Map<String, Dependency> normalizedDeps = new LinkedHashMap<>(dependencies.size() * 2); | ||||||||||
| for (Dependency dependency : dependencies) { | ||||||||||
| normalized.put(dependency.getManagementKey(), dependency); | ||||||||||
| normalizedDeps.put(dependency.getManagementKey(), dependency); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (dependencies.size() != normalized.size()) { | ||||||||||
| builder.dependencies(normalized.values()); | ||||||||||
| if (expanded != null || dependencies.size() != normalizedDeps.size()) { | ||||||||||
| builder.dependencies(normalizedDeps.values()); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return builder.build(); | ||||||||||
|
|
@@ -144,4 +154,92 @@ private <T> List<T> injectList(List<T> list, UnaryOperator<T> modifer) { | |||||||||
| } | ||||||||||
| return newList; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Expands the {@code id} attribute on a dependency into its component fields. | ||||||||||
| * The id format is {@code groupId:artifactId:version}. | ||||||||||
| */ | ||||||||||
| Dependency expandDependencyId(Dependency d) { | ||||||||||
| String id = d.getId(); | ||||||||||
| if (id == null || id.isEmpty()) { | ||||||||||
| // No id attribute, but still expand exclusion ids | ||||||||||
| List<Exclusion> expanded = injectList(d.getExclusions(), this::expandExclusionId); | ||||||||||
| return expanded != null ? d.withExclusions(expanded) : d; | ||||||||||
| } | ||||||||||
| String[] parts = id.split(":"); | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The normalizer silently ignores the However, consider that |
||||||||||
| if (parts.length != 3) { | ||||||||||
| // Invalid format — will be caught by the validator | ||||||||||
| return d; | ||||||||||
| } | ||||||||||
| Dependency.Builder builder = Dependency.newBuilder(d); | ||||||||||
| if (isBlank(d.getGroupId())) { | ||||||||||
| builder.groupId(parts[0]); | ||||||||||
| } | ||||||||||
| if (isBlank(d.getArtifactId())) { | ||||||||||
| builder.artifactId(parts[1]); | ||||||||||
| } | ||||||||||
| if (isBlank(d.getVersion())) { | ||||||||||
| builder.version(parts[2]); | ||||||||||
| } | ||||||||||
| List<Exclusion> expanded = injectList(d.getExclusions(), this::expandExclusionId); | ||||||||||
| if (expanded != null) { | ||||||||||
| builder.exclusions(expanded); | ||||||||||
| } | ||||||||||
| return builder.build(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Expands the {@code id} attribute on an exclusion into its component fields. | ||||||||||
| * The id format is {@code groupId:artifactId}. | ||||||||||
| */ | ||||||||||
| Exclusion expandExclusionId(Exclusion e) { | ||||||||||
| String id = e.getId(); | ||||||||||
| if (id == null || id.isEmpty()) { | ||||||||||
| return e; | ||||||||||
| } | ||||||||||
| String[] parts = id.split(":"); | ||||||||||
| if (parts.length != 2) { | ||||||||||
| // Invalid format — will be caught by the validator | ||||||||||
| return e; | ||||||||||
| } | ||||||||||
| Exclusion.Builder builder = Exclusion.newBuilder(e); | ||||||||||
| if (isBlank(e.getGroupId())) { | ||||||||||
| builder.groupId(parts[0]); | ||||||||||
| } | ||||||||||
| if (isBlank(e.getArtifactId())) { | ||||||||||
| builder.artifactId(parts[1]); | ||||||||||
| } | ||||||||||
| return builder.build(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Expands the {@code id} (XML attribute) / {@code gav} (Java field) on a mixin | ||||||||||
| * into its component fields. The format is {@code groupId:artifactId:version}. | ||||||||||
| */ | ||||||||||
| Mixin expandMixinGav(Mixin m) { | ||||||||||
| String gav = m.getGav(); | ||||||||||
| if (gav == null || gav.isEmpty()) { | ||||||||||
| return m; | ||||||||||
| } | ||||||||||
| String[] parts = gav.split(":"); | ||||||||||
| if (parts.length != 3) { | ||||||||||
| // Invalid format — will be caught by the validator | ||||||||||
| return m; | ||||||||||
| } | ||||||||||
| Mixin.Builder builder = Mixin.newBuilder(m); | ||||||||||
| if (isBlank(m.getGroupId())) { | ||||||||||
| builder.groupId(parts[0]); | ||||||||||
| } | ||||||||||
| if (isBlank(m.getArtifactId())) { | ||||||||||
| builder.artifactId(parts[1]); | ||||||||||
| } | ||||||||||
| if (isBlank(m.getVersion())) { | ||||||||||
| builder.version(parts[2]); | ||||||||||
| } | ||||||||||
| return builder.build(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this method name is misleading --
Suggested change
|
||||||||||
| private static boolean isBlank(String s) { | ||||||||||
| return s == null || s.isEmpty(); | ||||||||||
| } | ||||||||||
| } | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using field name
gavwith attribute nameid... in order to avoid the conflict with the existingParent.getId()