Skip to content

Commit

Permalink
added the option newPropertyVersionName to ChangeDependencyVersionVal…
Browse files Browse the repository at this point in the history
…ue to rename the old property name.
  • Loading branch information
marcel-gepardec committed Jul 30, 2024
1 parent 1b93b20 commit ec3e81f
Show file tree
Hide file tree
Showing 6 changed files with 494 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ public class ChangeDependencyGroupIdAndArtifactId extends Recipe {
@Nullable
Boolean changeManagedDependency;

@Option(displayName = "Update dependency management",
description = "Also update the dependency management section. The default for this flag is `true`.",
@Option(displayName = "Change property version name",
description = "Allows property version name to be changed to best practice naming convention. The default for this flag is `false`.",
required = false)
@Nullable
Boolean changePropertyVersionNames;
Boolean changePropertyVersionName;

public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, @Nullable String newGroupId, @Nullable String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern) {
this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, false, true, false);
Expand All @@ -109,7 +109,7 @@ public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifac
}

@JsonCreator
public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, @Nullable String newGroupId, @Nullable String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean overrideManagedVersion, @Nullable Boolean changeManagedDependency, @Nullable Boolean changePropertyVersionNames) {
public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, @Nullable String newGroupId, @Nullable String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean overrideManagedVersion, @Nullable Boolean changeManagedDependency, @Nullable Boolean changePropertyVersionName) {
this.oldGroupId = oldGroupId;
this.oldArtifactId = oldArtifactId;
this.newGroupId = newGroupId;
Expand All @@ -118,7 +118,7 @@ public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifac
this.versionPattern = versionPattern;
this.overrideManagedVersion = overrideManagedVersion;
this.changeManagedDependency = changeManagedDependency;
this.changePropertyVersionNames = changePropertyVersionNames;
this.changePropertyVersionName = changePropertyVersionName != null && changePropertyVersionName; // False by default
}

@Override
Expand Down Expand Up @@ -173,7 +173,7 @@ public Xml visitDocument(Xml.Document document, ExecutionContext ctx) {
oldGroupId, oldArtifactId,
Optional.ofNullable(newGroupId).orElse(oldGroupId),
Optional.ofNullable(newArtifactId).orElse(oldArtifactId),
newVersion, versionPattern, changePropertyVersionNames).getVisitor());
newVersion, versionPattern, changePropertyVersionName).getVisitor());
}
return super.visitDocument(document, ctx);
}
Expand Down Expand Up @@ -213,7 +213,7 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
if (!configuredToOverrideManageVersion && newDependencyManaged || (oldDependencyManaged && configuredToChangeManagedDependency)) {
t = (Xml.Tag) new RemoveContentVisitor<>(versionTag.get(), false).visit(t, ctx);
} else {
doAfterVisit(new ChangeVersionValue(groupId, artifactId, newVersion, versionPattern, ChangeVersionValue.Changes.DEPENDENCY.name()).getVisitor());
doAfterVisit(new ChangeDependencyVersionValue(groupId, artifactId, newVersion, versionPattern, changePropertyVersionName, ChangeDependencyVersionValue.VersionLocation.DEPENDENCY.name()).getVisitor());

}
} else if (!(newDependencyManaged && configuredToChangeManagedDependency)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@

@Value
@EqualsAndHashCode(callSuper = false)
public class ChangeVersionValue extends Recipe {
private static final Logger log = LoggerFactory.getLogger(ChangeVersionValue.class);
public class ChangeDependencyVersionValue extends Recipe {
private static final Logger log = LoggerFactory.getLogger(ChangeDependencyVersionValue.class);
@EqualsAndHashCode.Exclude
MavenMetadataFailures metadataFailures = new MavenMetadataFailures(this);

Expand All @@ -46,14 +46,14 @@ public class ChangeVersionValue extends Recipe {
"${version}", "${project.version}", "${pom.version}", "${project.parent.version}"
));

public enum Changes {
public enum VersionLocation {
DEPENDENCY ("DEPENDENCY"),
MANAGED_DEPENDENCY ("MANAGED_DEPENDENCY"),
PLUGIN_DEPENDENCY ("PLUGIN_DEPENDENCY");

private final String name;

Changes(String name) {
VersionLocation(String name) {
this.name = name;
}

Expand Down Expand Up @@ -88,36 +88,45 @@ public String toString() {
@Nullable
String versionPattern;

@Option(displayName = "Change property version names",
description = "Allows property version names to be changed to best practice naming convention",
example = "-jre",
@Option(displayName = "Change property version name",
description = "Allows property version name to be changed to best practice naming convention or what ever is in the newPropertyVersionName option. The default for this flag is `false`.",
required = false)
@Nullable
Boolean changePropertyVersionNames;
Boolean changePropertyVersionName;

@Option(displayName = "Change property version names",
description = "Allows property version names to be changed to best practice naming convention",
example = "-jre",
@Option(displayName = "New property version name",
description = "The new property version name used as property variable name. If it is set it will be changed. No need to set the changePropertyVersionName to true.",
example = "example.property.version",
required = false)
@Nullable
String newPropertyVersionName;

@Option(displayName = "Declare the location where the dependency should be changed.",
description = "Changes dependency version right where you want it. The default is set to change it everywhere." +
"But you can also specifically target `DEPENDENCY`, `MANAGED_DEPENDENCY` or `PLUGIN_DEPENDENCY`." +
"It also doesn't matter if they are in a profile or not.",
example = "DEPENDENCY",
required = false)
@Nullable
Changes versionChangePlace;
ChangeDependencyVersionValue.VersionLocation versionLocation;

public ChangeVersionValue(String groupId, String artifactId, @Nullable String newVersion, @Nullable String versionChangePlace) {
this(groupId, artifactId, newVersion, null, false, versionChangePlace);
public ChangeDependencyVersionValue(String groupId, String artifactId, @Nullable String newVersion, @Nullable String versionLocation) {
this(groupId, artifactId, newVersion, null, false, null, versionLocation);
}

public ChangeVersionValue(String groupId, String artifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable String versionChangePlace) {
this(groupId, artifactId, newVersion, versionPattern, false, versionChangePlace);
public ChangeDependencyVersionValue(String groupId, String artifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean changePropertyVersionName, @Nullable String versionLocation) {
this(groupId, artifactId, newVersion, versionPattern, changePropertyVersionName, null, versionLocation);
}

@JsonCreator
public ChangeVersionValue(String groupId, String artifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean changePropertyVersionNames, @Nullable String versionChangePlace) {
public ChangeDependencyVersionValue(String groupId, String artifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean changePropertyVersionName, @Nullable String newPropertyVersionName, @Nullable String versionLocation) {
this.groupId = groupId;
this.artifactId = artifactId;
this.newVersion = newVersion;
this.versionPattern = versionPattern;
this.changePropertyVersionNames = changePropertyVersionNames;
this.versionChangePlace = versionChangePlace != null ? Changes.valueOf(versionChangePlace) : null;
this.changePropertyVersionName = changePropertyVersionName != null && changePropertyVersionName; // False by default
this.newPropertyVersionName = newPropertyVersionName;
this.versionLocation = versionLocation != null ? VersionLocation.valueOf(versionLocation) : null;
}

@Override
Expand All @@ -142,12 +151,13 @@ public Validated<Object> validate() {

@Override
public String getDisplayName() {
return "Change the version or the referenced property version of dependencies or plugins";
return "Change Maven dependency version";
}

@Override
public String getDescription() {
return "Change the version or the referenced property version of dependencies or plugins.";
return "Change a Maven dependency version. Declare `groupId` and `artifactId` of the dependency in which the version should be changed. " +
"By adding `versionLocation`, a set of dependencies can be targeted such as managed Dependencies with `MANAGED_DEPENDENCY`.";
}

@Override
Expand All @@ -163,30 +173,37 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {

Xml.Tag t = super.visitTag(tag, ctx);

String artifact = artifactId == null ? t.getChildValue("artifactId").orElse(null) : artifactId;
if (artifactId != null){
if (matchesGlob(tag.getChildValue("groupId").orElse(null), groupId) && (artifactId == null || matchesGlob(tag.getChildValue("artifactId").orElse(null), artifactId))) {
String artifact = artifactId == null ? t.getChildValue("artifactId").orElse(null) : artifactId;

if (matchesGlob(tag.getChildValue("groupId").orElse(null), groupId) && (artifactId == null || matchesGlob(tag.getChildValue("artifactId").orElse(null), artifactId))) {
if (versionChangePlace == null){
return changeVersion(t, ctx, groupId, artifact, newVersion, changePropertyVersionNames);
}
switch (Objects.requireNonNull(versionChangePlace)) {
case DEPENDENCY:
if (isDependencyTag() || PROFILE_DEPENDENCY_MATCHER.matches(getCursor())){
return changeVersion(t, ctx, groupId, artifact, newVersion, changePropertyVersionNames);
}
break;
case MANAGED_DEPENDENCY:
if (isManagedDependencyTag() || PROFILE_MANAGED_DEPENDENCY_MATCHER.matches(getCursor())){
return changeVersion(t, ctx, groupId, artifact, newVersion, changePropertyVersionNames);
if (newVersion != null){
if (versionLocation == null){
return changeVersion(t, ctx);
}
break;
case PLUGIN_DEPENDENCY:
if (isPluginDependencyTag(groupId, artifact) || PROFILE_PLUGIN_DEPENDENCY_MATCHER.matches(getCursor())){
return changeVersion(t, ctx, groupId, artifact, newVersion, changePropertyVersionNames);
switch (Objects.requireNonNull(versionLocation)) {
case DEPENDENCY:
if (isDependencyTag() || PROFILE_DEPENDENCY_MATCHER.matches(getCursor())){
return changeVersion(t, ctx);
}
break;
case MANAGED_DEPENDENCY:
if (isManagedDependencyTag() || PROFILE_MANAGED_DEPENDENCY_MATCHER.matches(getCursor())){
return changeVersion(t, ctx);
}
break;
case PLUGIN_DEPENDENCY:
if (isPluginDependencyTag(groupId, artifact) || PROFILE_PLUGIN_DEPENDENCY_MATCHER.matches(getCursor())){
return changeVersion(t, ctx);
}
break;
}
break;
}

}
}


return t;
}

Expand Down Expand Up @@ -222,7 +239,7 @@ private String resolveSemverVersion(ExecutionContext ctx, String groupId, String
return availableVersions.isEmpty() ? newVersion : Collections.max(availableVersions, versionComparator);
}

public Xml.Tag changeVersion(Xml.Tag t, ExecutionContext ctx, String groupId, String artifactId, String newVersion, Boolean changePropertyVersionNames) {
public Xml.Tag changeVersion(Xml.Tag t, ExecutionContext ctx) {

boolean changed = false;
if (newVersion != null) {
Expand All @@ -235,14 +252,18 @@ public Xml.Tag changeVersion(Xml.Tag t, ExecutionContext ctx, String groupId, St
String propertyVariable = version.substring(2, version.length() - 1);
String newPropertyVariable = propertyVariable;
if (!matchesGlob(getResolutionResult().getPom().getProperties().get(propertyVariable), resolvedNewVersion)) {
if (Boolean.TRUE.equals(changePropertyVersionNames)) {
newPropertyVariable = artifactId + ".version";
doAfterVisit(new RenamePropertyKey(propertyVariable, newPropertyVariable).getVisitor());
t = changeChildTagValue(t, "version", "${" + newPropertyVariable + "}", ctx);
}
doAfterVisit(new ChangePropertyValue(newPropertyVariable, resolvedNewVersion, false, false).getVisitor());
changed = true;
}
newPropertyVariable = artifactId + ".version";
if (newPropertyVersionName != null) {
newPropertyVariable = newPropertyVersionName;
}
if (!getResolutionResult().getPom().getProperties().containsKey(newPropertyVariable) && (Boolean.TRUE.equals(changePropertyVersionName) || newPropertyVersionName != null)) {
doAfterVisit(new RenamePropertyKey(propertyVariable, newPropertyVariable).getVisitor());
t = changeChildTagValue(t, "version", "${" + newPropertyVariable + "}", ctx);
changed = true;
}
} else {
if (!matchesGlob(t.getChildValue("version").orElse(null), resolvedNewVersion)){
t = changeChildTagValue(t, "version", resolvedNewVersion, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,11 @@ public class ChangeManagedDependencyGroupIdAndArtifactId extends Recipe {
@Nullable
String versionPattern;

@Option(displayName = "Change property version names",
description = "Allows property version names to be changed to best practice naming convention",
example = "-jre",
@Option(displayName = "Change property version name",
description = "Allows property version name to be changed to best practice naming convention. The default for this flag is `false`.",
required = false)
@Nullable
Boolean changePropertyVersionNames;
Boolean changePropertyVersionName;

public ChangeManagedDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, String newGroupId, String newArtifactId, @Nullable String newVersion) {
this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, null, false);
Expand All @@ -92,14 +91,14 @@ public ChangeManagedDependencyGroupIdAndArtifactId(String oldGroupId, String old
}

@JsonCreator
public ChangeManagedDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, String newGroupId, String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean changePropertyVersionNames) {
public ChangeManagedDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, String newGroupId, String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean changePropertyVersionName) {
this.oldGroupId = oldGroupId;
this.oldArtifactId = oldArtifactId;
this.newGroupId = newGroupId;
this.newArtifactId = newArtifactId;
this.newVersion = newVersion;
this.versionPattern = versionPattern;
this.changePropertyVersionNames = changePropertyVersionNames;
this.changePropertyVersionName = changePropertyVersionName != null && changePropertyVersionName; // False by default
}

@Override
Expand Down Expand Up @@ -154,7 +153,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
}
Optional<Xml.Tag> versionTag = t.getChild("version");
if (versionTag.isPresent() && newVersion != null && !newVersion.equals(versionTag.get().getValue().orElse(null))) {
doAfterVisit(new ChangeVersionValue(newGroupId, newArtifactId, newVersion, versionPattern, ChangeVersionValue.Changes.MANAGED_DEPENDENCY.name()).getVisitor());
doAfterVisit(new ChangeDependencyVersionValue(newGroupId, newArtifactId, newVersion, versionPattern, changePropertyVersionName, ChangeDependencyVersionValue.VersionLocation.MANAGED_DEPENDENCY.name()).getVisitor());
changed = true;
}
if (changed) {
Expand Down
Loading

0 comments on commit ec3e81f

Please sign in to comment.