Skip to content

Maven add missing version to a dependency #6770

@wesboe

Description

@wesboe

What problem are you trying to solve?

Spring boot and other frameworks manage dependencies. It can happen the dependency is removed from the dependencies management part. After migration to the new parent version, your application will complain the version of the dependency is missing. This happens when you migrate to Spring Boot 4 for dependencies spring-retry and rest-assured.

What precondition(s) should be checked before applying this recipe?

If the dependency has a version or not.

Description recipe

The recipe should follow the input of other maven recipes like upgradeparentversion.

  • groupId
  • artifactId
  • newVersion
  • versionPattern
    Optional is acceptTransitive like with recipe AddDependency. To not add the version if dependency is found in management dependency.

Draft recipe

`
@value
@EqualsAndHashCode(callSuper = false)
public class ExplicitAddDependencyVersion extends Recipe {

@Getter
final String displayName = "Add version to dependency";

@Getter
final String description =
        "Add version to a dependency that is missing it. This can be required when dependency is not managed anymore by a parent pom.";

@Option(
        displayName = "Group",
        description = "The first part of a dependency coordinate `com.google.guava:guava:VERSION`.",
        example = "com.google.guava")
String groupId;

@Option(
        displayName = "Artifact",
        description = "The second part of a dependency coordinate `com.google.guava:guava:VERSION`.",
        example = "guava")
String artifactId;

@Option(
        displayName = "Version",
        description = "An exact version number or node-style semver selector used to select the version number.",
        example = "29.X")
String newVersion;

@Override
public Validated<Object> validate() {
    Validated<Object> validated = super.validate();
    //noinspection ConstantConditions
    if (newVersion != null) {
        validated = validated.and(Semver.validate(newVersion, null));
    }

    return validated;
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
    return new MavenIsoVisitor<>() {
        @Override
        public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
            Xml.Tag t = super.visitTag(tag, ctx);
            if (isDependencyTag(groupId, artifactId)) {
                if (t.getChildValue("version").isEmpty()) {
                    Xml.Tag versionTag = Xml.Tag.build("<version>" + newVersion + "</version>");

                    t = (Xml.Tag)
                            new AddToTagVisitor<>(t, versionTag, new MavenTagInsertionComparator(t.getChildren()))
                                    .visitNonNull(t, 0, getCursor().getParent());
                    maybeUpdateModel();
                }
            }
            return t;
        }
    };
}

}
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions