-
Notifications
You must be signed in to change notification settings - Fork 517
Maven add missing version to a dependency #6770
Copy link
Copy link
Open
Description
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;
}
};
}
}
`
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Status
No status