Skip to content

Feat: Add id attribute (gav) support to Dependency, Exclusion, Mixin#11904

Open
rbygrave wants to merge 13 commits into
apache:masterfrom
rbygrave:feature/dependency-id-attribute
Open

Feat: Add id attribute (gav) support to Dependency, Exclusion, Mixin#11904
rbygrave wants to merge 13 commits into
apache:masterfrom
rbygrave:feature/dependency-id-attribute

Conversation

@rbygrave

@rbygrave rbygrave commented Apr 8, 2026

Copy link
Copy Markdown

Based on design discussion on Issue #11500, the proposal is to support a compact id attribute format for Dependency, Exclusion and Mixin elements.

Dependency id Format

The id attribute supports the following compact formats, using the standard Maven coordinate order (groupId:artifactId:type:classifier:version):

Format Description
groupId:artifactId version managed via <dependencyManagement>
groupId:artifactId:version explicit version
groupId:artifactId:type:version with explicit type
groupId:artifactId:type:classifier:version with type and classifier

Full syntax: groupId:artifactId[[:type[:classifier]]:version][@scope][?]

A trailing : (empty version) indicates the version is managed (e.g. groupId:artifactId:type:).

Any segment may be left empty to skip setting that field, leaving it for later inference (e.g. from dependencyManagement). For example, :artifactId leaves groupId unset, and :artifactId:version leaves groupId unset with an explicit version.

An optional @scope suffix sets the dependency scope (test, provided, runtime, compile, system, import). A trailing ? marks the dependency as optional.

Examples

<!-- Basic GAV -->
<dependency id="org.slf4j:slf4j-api:2.0.17"/>

<!-- Managed version (from dependencyManagement) -->
<dependency id="org.slf4j:slf4j-api"/>

<!-- Inferred groupId (from dependencyManagement) -->
<dependency id=":slf4j-api"/>

<!-- Inferred groupId with explicit version -->
<dependency id=":slf4j-api:2.0.17"/>

<!-- With scope -->
<dependency id="org.junit.jupiter:junit-jupiter-api:5.14.1@test"/>

<!-- Inferred groupId with scope -->
<dependency id=":junit-jupiter-api@test"/>

<!-- With optional marker -->
<dependency id="commons-io:commons-io:2.11.0?"/>

<!-- Scope + optional combined -->
<dependency id="org.apache.maven:maven-core:3.9.0@provided?"/>

<!-- Import scope (for BOMs) -->
<dependency id="org.junit:junit-bom:5.12.0@import"/>

<!-- With type -->
<dependency id="org.example:lib-b:pom:1.0"/>

<!-- With type and classifier -->
<dependency id="org.example:lib-c:jar:sources:1.0"/>

<!-- Type with managed version -->
<dependency id="org.example:lib-b:pom:"/>

<!-- Exclusions with id attribute -->
<dependency id="org.postgresql:postgresql:42.7.3">
  <exclusions>
    <exclusion id="*:*"/>
  </exclusions>
</dependency>

<!-- Mixin with compact format -->
<mixin id="com.example.mixins:java-mixin:1.0.0"/>

Validation

  • When id is used, child elements groupId, artifactId, and version must not be specified (even if the corresponding segment in the id is empty).
  • type conflict is only checked when the id has 4+ parts (3+ colons, i.e. type is encoded).
  • classifier conflict is only checked when the id has 5 parts (4 colons, i.e. classifier is encoded).
  • When @scope is present in the id, a <scope> child element must not be specified.
  • When ? is present, an <optional> child element must not be specified.
  • Scope and optional can still be specified as child elements when not encoded in the id attribute.

Inference

Empty segments are skipped during normalization, leaving the corresponding field unset for later inference. This allows patterns like:

  • :artifactId — groupId and version left unset for inference from dependencyManagement
  • :artifactId:version — groupId left unset
  • groupId:artifactId:type: — version left unset (managed), type explicit

Notes

  • Mixin has the XML attribute id but uses gav underneath as the Java field, to work around a name clash with getId() from Parent.
  • Normalization splits the id into component fields and clears the id attribute, so downstream code works with standard dependency fields.
  • Existing child element values are not overridden — the id attribute only fills in fields that are currently null/empty.

Following this checklist to help us incorporate your
contribution quickly and easily:

  • Your pull request should address just one issue, without pulling in other changes.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body.
    Note that commits might be squashed by a maintainer on merge.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied.
    This may not always be possible but is a best-practice.
  • Run mvn verify to make sure basic checks pass.
    A more thorough check will be performed on your pull request automatically.
  • You have run the Core IT successfully.

If your pull request is about ~20 lines of code you don't need to sign an
Individual Contributor License Agreement if you are unsure
please ask on the developers list.

To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.

rbygrave added 2 commits April 8, 2026 18:08
Based on design discussion on Issue apache#11500, the proposal is to support
`groupId:artifactId:version` format for Dependency, Exclusion and Mixin.

This means that we can define dependencies in the form:
```xml
<dependency id="groupId:artifactId:version" />
```

Examples:
```xml
<dependency id="org.slf4j:slf4j-api:2.0.17"/>
```
```xml
<dependency id="org.postgresql:postgresql:42.7.3">
  <exclusions>
    <exclusion id="*:*"/>
  </exclusions>
</dependency>
```
```xml
<mixin id="com.example.mixins:java-mixin:1.0.0" />
```
Add more tests and make them more specific to the validation that is
being tested by each test.
<superClass>Parent</superClass>
<fields>
<field xml.attribute="true" xml.tagName="id">
<name>gav</name>

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Using field name gav with attribute name id ... in order to avoid the conflict with the existing Parent.getId()

@gnodet gnodet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Claude Code on behalf of Guillaume Nodet

Overall a clean implementation. The strict validation, test coverage, and the Mixin gav field workaround are well thought out. A few issues to address:

  1. Dependency management dependencies are not expanded -- mergeDuplicates only expands id on model.getDependencies() but not on model.getDependencyManagement().getDependencies(), nor on profile-scoped dependencies/dependency-management. The validator correctly validates all of these (lines 536-596 of DefaultModelValidator), so managed deps with id will pass validation but never get expanded.

  2. Normalization runs after raw validation -- validateRawModel runs in doReadRawModel() (line 1818) before mergeDuplicates (line 1362 in readInputModel). The validator's duplicate-detection key logic (using dependency.getId() directly when set) handles this correctly, but it means the validator sees the unexpanded state. This is fine for the conflict checks but worth keeping in mind.

  3. Dependency.getId() naming clash -- The Dependency class now has a new id field with getId()/setId() via the model generator. But Parent (and therefore Mixin) has a computed getId() method in a codeSegment that returns groupId:artifactId:pom:version. Since Mixin extends Parent, the new field was wisely renamed to gav. However, does Dependency have a similar computed getId() method anywhere? If so, the new id field's generated getter could shadow or conflict with it. Worth verifying the generated code compiles cleanly.

  4. isBlank does not trim -- The isBlank helper in the normalizer only checks for null/empty, not for whitespace-only strings. The name isBlank is misleading since String.isBlank() checks whitespace. Consider renaming to isNullOrEmpty or using String.isBlank() which handles whitespace in coordinates (e.g., " ").

}
return builder.build();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: this method name is misleading -- String.isBlank() in Java checks for whitespace-only strings, but this only checks null/empty. Consider renaming to isNullOrEmpty to avoid confusion, or use s == null || s.isBlank() to also handle whitespace-only values in coordinates.

Suggested change
private static boolean isNullOrEmpty(String s) {
return s == null || s.isEmpty();
}

if (dependency.getId() != null && !dependency.getId().isEmpty()) {
key = dependency.getId();
} else {
key = dependency.getManagementKey();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good approach using dependency.getId() as the dedup key when set, since normalization hasn't run yet at this point. However, two dependencies with the same id attribute but different scopes/classifiers would collide here (since the key is just the raw id string, not the management key). After normalization, they'd have distinct management keys (which includes type and classifier). Is this the intended behavior?

@gnodet gnodet added this to the 4.1.0 milestone Jun 16, 2026
@gnodet gnodet added mvn4 enhancement New feature or request labels Jun 22, 2026
gnodet and others added 11 commits June 22, 2026 20:17
… profiles

- Expand id attributes on dependencies in dependencyManagement, not just
  top-level dependencies
- Expand id attributes on dependencies within profile sections (both
  regular dependencies and profile dependencyManagement)
- Extract expandAndDeduplicateDependencies() and
  expandProfileDependencyIds() helper methods
- Rename isBlank to isNullOrEmpty for clarity
- Add tests for dependency management and profile id expansion

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add compact `id` XML attribute to Dependency, Exclusion, and Mixin
elements in the Maven model (4.2.0+). Supported formats:
- Dependency: g:a:v, g:a:type:v, g:a:type:classifier:v
- Exclusion: g:a
- Mixin (as `gav`/`id`): g:a:v

The normalizer expands `id` into individual fields and clears it so
consumer POMs never contain the attribute. Uses forceCopy=true in
builders to ensure `id(null)` actually clears the field.

Includes comprehensive unit tests (normalizer, validator), consumer
POM transformation tests, and integration test verifying no id
attributes leak into deployed POMs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…attributes

Add DependencyIdStrategy that migrates verbose dependency/exclusion
child elements into compact `id` attributes when upgrading to model
version 4.2.0 via mvnup.

The strategy runs at @priority(25), after InferenceStrategy (30), so
only dependencies that still have full explicit coordinates after
inference are collapsed. Triggered by --model-version 4.2.0 or --all.

Supported transformations:
- <dependency> g:a:v / g:a:type:v / g:a:type:classifier:v
- <exclusion> g:a
- Processes all sections: dependencies, dependencyManagement, profiles,
  plugin dependencies

Inferred ids (partial coordinates like `:a` or `g:a` without version)
are intentionally not supported — the `id` attribute is a shorthand
for explicit coordinates, not a replacement for Maven's inference
mechanism. Dependencies with missing coordinates after inference are
left in their expanded form.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ribute

Add support for 2-part `g:a` format and trailing-colon formats
(`g:a:`, `g:a:type:`, `g:a:type:classifier:`) for dependencies
whose version is provided by dependencyManagement. The trailing
colon convention signals that version is managed, not missing.

Leave aside inferred ids (`:a:` with inferred groupId) for now.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Extend the compact id attribute format from groupId:artifactId:version
to groupId:artifactId:version[@scope][?], where @scope sets the
dependency scope and trailing ? marks the dependency as optional.

Examples:
- org.junit:junit-jupiter-api:5.0@test
- commons-io:commons-io:2.11.0?
- org.apache.maven:maven-core:3.9.0@provided?

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ature/dependency-id-attribute

Combines type/classifier/managed-version support with @scope/? optional
markers in the dependency id attribute compact syntax.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Empty segments in the compact id attribute are now skipped rather than
setting fields to empty strings, allowing groupId/version/type to be
left unset for later inference from dependencyManagement.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The formal syntax incorrectly showed version before type. Maven
consistently uses groupId:artifactId:type:classifier:version order
(as in Artifact.key() and Dependency.getManagementKey()).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…dator

- Remove redundant model version guard in DependencyIdStrategy (the second
  check alone suffices)
- Simplify redundant type removal branches in collapseDependency()
- Remove redundant validateExclusionIdAttribute call from effective model
  validation (id is already cleared by normalizer before effective validation)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request mvn4

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants