From 5bddaa673fca3467fad5295a48e3b1b53f0e90f8 Mon Sep 17 00:00:00 2001 From: q3769 Date: Fri, 9 Aug 2024 10:33:16 -0500 Subject: [PATCH] + better error messages --- pom.xml | 6 +++--- .../maven/plugins/semver/LabelUpdater.java | 7 ++++--- .../q3769/maven/plugins/semver/SemverMojo.java | 7 ++++++- .../plugins/semver/mojos/CalendarMajor.java | 10 +++++++++- .../plugins/semver/mojos/CalendarMinor.java | 10 +++++++++- .../plugins/semver/mojos/CalendarPatch.java | 10 +++++++++- .../semver/mojos/CalendarVersionFormatter.java | 12 ++++-------- .../plugins/semver/mojos/FinalizeCurrent.java | 11 +++++------ .../plugins/semver/mojos/IncrementMajor.java | 11 +++++++++-- .../plugins/semver/mojos/IncrementMinor.java | 11 +++++++++-- .../plugins/semver/mojos/IncrementPatch.java | 11 +++++++++-- .../q3769/maven/plugins/semver/mojos/Merge.java | 15 ++++++++++++--- .../plugins/semver/mojos/MergeCalendar.java | 13 +++++++++++-- .../maven/plugins/semver/mojos/SetCurrent.java | 13 +++++++++++-- .../semver/mojos/UpdateBuildMetadata.java | 17 +++++++++++++---- .../plugins/semver/mojos/UpdatePreRelease.java | 17 +++++++++++++---- .../maven/plugins/semver/mojos/MergeTest.java | 5 +++-- 17 files changed, 139 insertions(+), 47 deletions(-) diff --git a/pom.xml b/pom.xml index a27d250..bdcc2b0 100644 --- a/pom.xml +++ b/pom.xml @@ -30,7 +30,7 @@ io.github.q3769 semver-maven-plugin - 20240116.0.202408090614 + 20240116.0.202408091532 maven-plugin semver-maven-plugin @@ -214,7 +214,7 @@ - 2.48.0 + 2.49.0 true @@ -229,7 +229,7 @@ io.github.q3769 semver-maven-plugin - 20240116.0.202408090614 + 20240116.0.202408091532 org.apache.maven.plugins diff --git a/src/main/java/q3769/maven/plugins/semver/LabelUpdater.java b/src/main/java/q3769/maven/plugins/semver/LabelUpdater.java index 09d7019..1f095e5 100644 --- a/src/main/java/q3769/maven/plugins/semver/LabelUpdater.java +++ b/src/main/java/q3769/maven/plugins/semver/LabelUpdater.java @@ -26,6 +26,7 @@ import com.github.zafarkhaja.semver.Version; import org.apache.commons.lang3.StringUtils; +import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Parameter; /** @@ -51,7 +52,7 @@ public abstract class LabelUpdater extends Updater { * @param version the semantic version to increment * @return the incremented semantic version */ - protected abstract Version incrementLabel(Version version); + protected abstract Version incrementLabel(Version version) throws MojoFailureException; /** * Sets the label of the given semantic version to the specified value. @@ -60,7 +61,7 @@ public abstract class LabelUpdater extends Updater { * @param label the new label to set * @return the semantic version with the newly set label */ - protected abstract Version setLabel(Version version, String label); + protected abstract Version setLabel(Version version, String label) throws MojoFailureException; /** * Updates the semantic version by either incrementing or setting its label. @@ -72,7 +73,7 @@ public abstract class LabelUpdater extends Updater { * @return the updated semantic version */ @Override - protected Version update(Version original) { + protected Version update(Version original) throws MojoFailureException { if (StringUtils.isBlank(set)) { logInfo("Incrementing label of version: %s", original); return incrementLabel(original); diff --git a/src/main/java/q3769/maven/plugins/semver/SemverMojo.java b/src/main/java/q3769/maven/plugins/semver/SemverMojo.java index 1f2f26b..2de893f 100644 --- a/src/main/java/q3769/maven/plugins/semver/SemverMojo.java +++ b/src/main/java/q3769/maven/plugins/semver/SemverMojo.java @@ -24,6 +24,7 @@ package q3769.maven.plugins.semver; import com.github.zafarkhaja.semver.Version; +import lombok.NonNull; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecution; @@ -39,15 +40,19 @@ */ public abstract class SemverMojo extends AbstractMojo { private static final String FALSE = "false"; + /** */ @Parameter(defaultValue = "${mojoExecution}", readonly = true) protected MojoExecution mojo; + /** */ @Parameter(property = "processModule", defaultValue = FALSE) protected String processModule; + /** Current Maven POM */ @Parameter(property = "project", defaultValue = "${project}", readonly = true, required = true) protected MavenProject project; + /** Default session */ @Parameter(property = "session", defaultValue = "${session}", readonly = true, required = true) protected MavenSession session; @@ -56,7 +61,7 @@ public abstract class SemverMojo extends AbstractMojo { * @param version text that is supposed to be valid per SemVer spec * @return A valid SemVer */ - public static Version requireValidSemVer(String version) { + public static @NonNull Version requireValidSemVer(String version) { try { return Version.parse(version); } catch (Exception ex) { diff --git a/src/main/java/q3769/maven/plugins/semver/mojos/CalendarMajor.java b/src/main/java/q3769/maven/plugins/semver/mojos/CalendarMajor.java index 8a550bc..5345892 100644 --- a/src/main/java/q3769/maven/plugins/semver/mojos/CalendarMajor.java +++ b/src/main/java/q3769/maven/plugins/semver/mojos/CalendarMajor.java @@ -48,6 +48,14 @@ public class CalendarMajor extends Updater { */ @Override protected Version update(Version original) throws MojoFailureException { - return CalendarVersionFormatter.calendarIncrement(original, SemverNormalVersion.MAJOR); + try { + return CalendarVersionFormatter.calendarIncrement(original, SemverNormalVersion.MAJOR); + } catch (Exception e) { + throw new MojoFailureException( + String.format( + "Failed to increment the %s version of semver %s", + SemverNormalVersion.MAJOR, original), + e); + } } } diff --git a/src/main/java/q3769/maven/plugins/semver/mojos/CalendarMinor.java b/src/main/java/q3769/maven/plugins/semver/mojos/CalendarMinor.java index 3c8a372..851ea0d 100644 --- a/src/main/java/q3769/maven/plugins/semver/mojos/CalendarMinor.java +++ b/src/main/java/q3769/maven/plugins/semver/mojos/CalendarMinor.java @@ -41,6 +41,14 @@ public class CalendarMinor extends Updater { */ @Override protected Version update(Version original) throws MojoFailureException { - return CalendarVersionFormatter.calendarIncrement(original, SemverNormalVersion.MINOR); + try { + return CalendarVersionFormatter.calendarIncrement(original, SemverNormalVersion.MINOR); + } catch (Exception e) { + throw new MojoFailureException( + String.format( + "Failed to increment the %s version of semver %s", + SemverNormalVersion.MINOR, original), + e); + } } } diff --git a/src/main/java/q3769/maven/plugins/semver/mojos/CalendarPatch.java b/src/main/java/q3769/maven/plugins/semver/mojos/CalendarPatch.java index 25da544..e5c09cd 100644 --- a/src/main/java/q3769/maven/plugins/semver/mojos/CalendarPatch.java +++ b/src/main/java/q3769/maven/plugins/semver/mojos/CalendarPatch.java @@ -41,6 +41,14 @@ public class CalendarPatch extends Updater { */ @Override protected Version update(Version original) throws MojoFailureException { - return CalendarVersionFormatter.calendarIncrement(original, SemverNormalVersion.PATCH); + try { + return CalendarVersionFormatter.calendarIncrement(original, SemverNormalVersion.PATCH); + } catch (Exception e) { + throw new MojoFailureException( + String.format( + "Failed to increment the %s version of semver %s", + SemverNormalVersion.PATCH, original), + e); + } } } diff --git a/src/main/java/q3769/maven/plugins/semver/mojos/CalendarVersionFormatter.java b/src/main/java/q3769/maven/plugins/semver/mojos/CalendarVersionFormatter.java index b4dfbe2..e907d54 100644 --- a/src/main/java/q3769/maven/plugins/semver/mojos/CalendarVersionFormatter.java +++ b/src/main/java/q3769/maven/plugins/semver/mojos/CalendarVersionFormatter.java @@ -30,7 +30,6 @@ import java.time.format.DateTimeFormatter; import javax.annotation.Nonnull; import lombok.NonNull; -import org.apache.maven.plugin.MojoFailureException; import q3769.maven.plugins.semver.SemverNormalVersion; enum CalendarVersionFormatter { @@ -52,13 +51,10 @@ enum CalendarVersionFormatter { /** * @param original pom version * @param selectedNormalVersion to increment - * @return new instance incremented - * @throws MojoFailureException if the original version's target category version is newer than - * now + * @return new instance incremented to current date in UTC zone */ public static Version calendarIncrement( - Version original, @Nonnull SemverNormalVersion selectedNormalVersion) - throws MojoFailureException { + Version original, @Nonnull SemverNormalVersion selectedNormalVersion) { long selectedNormalVersionNumber = selectedNormalVersion.getNumber(original); Instant now = Instant.now(); for (CalendarVersionFormatter formatter : values()) { @@ -67,9 +63,9 @@ public static Version calendarIncrement( return selectedNormalVersion.incrementTo(updatedNormalVersionNumber, original); } } - throw new MojoFailureException(new UnsupportedOperationException(String.format( + throw new UnsupportedOperationException(String.format( "%s version %s in POM semver %s is not supported for calendar style increment - it has to be older than current date in UTC zone", - selectedNormalVersion, selectedNormalVersionNumber, original))); + selectedNormalVersion, selectedNormalVersionNumber, original)); } private DateTimeFormatter getDateTimeFormatter() { diff --git a/src/main/java/q3769/maven/plugins/semver/mojos/FinalizeCurrent.java b/src/main/java/q3769/maven/plugins/semver/mojos/FinalizeCurrent.java index b4a34c2..b7901e4 100644 --- a/src/main/java/q3769/maven/plugins/semver/mojos/FinalizeCurrent.java +++ b/src/main/java/q3769/maven/plugins/semver/mojos/FinalizeCurrent.java @@ -24,6 +24,7 @@ package q3769.maven.plugins.semver.mojos; import com.github.zafarkhaja.semver.Version; +import lombok.NonNull; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import q3769.maven.plugins.semver.Updater; @@ -39,14 +40,12 @@ public class FinalizeCurrent extends Updater { /** * @param original to finalize - * @return final SemVer version of the original, all meta info stripped + * @return final SemVer version of the original, all labels stripped */ @Override - protected Version update(Version original) { - if (!original.preReleaseVersion().isPresent() && !original.buildMetadata().isPresent()) { - getLog() - .info("Current version: " + original - + " contains only normal version numbers, so no change."); + protected Version update(@NonNull Version original) { + if (original.preReleaseVersion().isEmpty() && original.buildMetadata().isEmpty()) { + logInfo("Current version: %s contains only normal version numbers, so no change.", original); return original; } return Version.of(original.majorVersion(), original.minorVersion(), original.patchVersion()); diff --git a/src/main/java/q3769/maven/plugins/semver/mojos/IncrementMajor.java b/src/main/java/q3769/maven/plugins/semver/mojos/IncrementMajor.java index 303088e..d6fd990 100644 --- a/src/main/java/q3769/maven/plugins/semver/mojos/IncrementMajor.java +++ b/src/main/java/q3769/maven/plugins/semver/mojos/IncrementMajor.java @@ -24,6 +24,8 @@ package q3769.maven.plugins.semver.mojos; import com.github.zafarkhaja.semver.Version; +import lombok.NonNull; +import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import q3769.maven.plugins.semver.Updater; @@ -37,7 +39,12 @@ public class IncrementMajor extends Updater { @Override - protected Version update(Version original) { - return original.nextMajorVersion(); + protected Version update(@NonNull Version original) throws MojoFailureException { + try { + return original.nextMajorVersion(); + } catch (Exception e) { + throw new MojoFailureException( + "Failed to increment the major version of semver " + original, e); + } } } diff --git a/src/main/java/q3769/maven/plugins/semver/mojos/IncrementMinor.java b/src/main/java/q3769/maven/plugins/semver/mojos/IncrementMinor.java index ca649ab..96183ae 100644 --- a/src/main/java/q3769/maven/plugins/semver/mojos/IncrementMinor.java +++ b/src/main/java/q3769/maven/plugins/semver/mojos/IncrementMinor.java @@ -24,6 +24,8 @@ package q3769.maven.plugins.semver.mojos; import com.github.zafarkhaja.semver.Version; +import lombok.NonNull; +import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import q3769.maven.plugins.semver.Updater; @@ -37,7 +39,12 @@ public class IncrementMinor extends Updater { @Override - protected Version update(Version original) { - return original.nextMinorVersion(); + protected Version update(@NonNull Version original) throws MojoFailureException { + try { + return original.nextMinorVersion(); + } catch (Exception e) { + throw new MojoFailureException( + "Failed to increment the minor version of semver " + original, e); + } } } diff --git a/src/main/java/q3769/maven/plugins/semver/mojos/IncrementPatch.java b/src/main/java/q3769/maven/plugins/semver/mojos/IncrementPatch.java index 72f4a21..5d29752 100644 --- a/src/main/java/q3769/maven/plugins/semver/mojos/IncrementPatch.java +++ b/src/main/java/q3769/maven/plugins/semver/mojos/IncrementPatch.java @@ -24,6 +24,8 @@ package q3769.maven.plugins.semver.mojos; import com.github.zafarkhaja.semver.Version; +import lombok.NonNull; +import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import q3769.maven.plugins.semver.Updater; @@ -37,7 +39,12 @@ public class IncrementPatch extends Updater { @Override - protected Version update(Version original) { - return original.nextPatchVersion(); + protected Version update(@NonNull Version original) throws MojoFailureException { + try { + return original.nextPatchVersion(); + } catch (Exception e) { + throw new MojoFailureException( + "Failed to increment the patch version of semver " + original, e); + } } } diff --git a/src/main/java/q3769/maven/plugins/semver/mojos/Merge.java b/src/main/java/q3769/maven/plugins/semver/mojos/Merge.java index 22bb863..9f8e1c9 100644 --- a/src/main/java/q3769/maven/plugins/semver/mojos/Merge.java +++ b/src/main/java/q3769/maven/plugins/semver/mojos/Merge.java @@ -25,6 +25,7 @@ import com.github.zafarkhaja.semver.Version; import lombok.NonNull; +import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; @@ -43,12 +44,12 @@ */ @Mojo(name = "merge", defaultPhase = LifecyclePhase.NONE) public class Merge extends Updater { - /** The other SemVer to be merged with current local POM's version */ + /** The other SemVer to be merged with current local POM version */ @Parameter(property = "semver", defaultValue = "NOT_SET") protected String otherSemVer; @Override - protected Version update(final Version original) { + protected Version update(final Version original) throws MojoFailureException { Version other = requireValidSemVer(otherSemVer); logDebug("Merging current POM version %s with provided version %s", original, other); if (original.isHigherThan(other)) { @@ -62,7 +63,15 @@ protected Version update(final Version original) { SemverNormalVersion.getLastIncrementedNormalVersion(original); logDebug( "Last incremented normal version of current pom semver is %s", pomIncrementedNormalVersion); - Version incrementedVersion = increment(other, pomIncrementedNormalVersion); + Version incrementedVersion; + try { + incrementedVersion = increment(other, pomIncrementedNormalVersion); + } catch (Exception e) { + throw new MojoFailureException( + String.format( + "Failed to merge the provided version %s with the POM version %s", other, original), + e); + } logDebug( "Incrementing provided version %s on POM semver incremented normal version %s, provisional merge version: %s", other, pomIncrementedNormalVersion, incrementedVersion); diff --git a/src/main/java/q3769/maven/plugins/semver/mojos/MergeCalendar.java b/src/main/java/q3769/maven/plugins/semver/mojos/MergeCalendar.java index 8eed140..aa834a5 100644 --- a/src/main/java/q3769/maven/plugins/semver/mojos/MergeCalendar.java +++ b/src/main/java/q3769/maven/plugins/semver/mojos/MergeCalendar.java @@ -63,8 +63,17 @@ protected Version update(@NonNull final Version original) throws MojoFailureExce SemverNormalVersion.getLastIncrementedNormalVersion(original); logDebug( "Last incremented normal version of current pom semver is %s", pomIncrementedNormalVersion); - Version provisionalMergedVersion = - CalendarVersionFormatter.calendarIncrement(other, pomIncrementedNormalVersion); + Version provisionalMergedVersion; + try { + provisionalMergedVersion = + CalendarVersionFormatter.calendarIncrement(other, pomIncrementedNormalVersion); + } catch (Exception e) { + throw new MojoFailureException( + String.format( + "Failed to calendar-merge provided version %s with the POM version %s", + other, original), + e); + } logDebug( "Incrementing provided version %s on POM semver incremented normal version %s, provisional merge version: %s", other, pomIncrementedNormalVersion, provisionalMergedVersion); diff --git a/src/main/java/q3769/maven/plugins/semver/mojos/SetCurrent.java b/src/main/java/q3769/maven/plugins/semver/mojos/SetCurrent.java index e33140d..a3412ab 100644 --- a/src/main/java/q3769/maven/plugins/semver/mojos/SetCurrent.java +++ b/src/main/java/q3769/maven/plugins/semver/mojos/SetCurrent.java @@ -24,6 +24,7 @@ package q3769.maven.plugins.semver.mojos; import com.github.zafarkhaja.semver.Version; +import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; @@ -42,7 +43,15 @@ public class SetCurrent extends Updater { protected String semver; @Override - protected Version update(Version original) { - return requireValidSemVer(semver); + protected Version update(Version original) throws MojoFailureException { + try { + return requireValidSemVer(semver); + } catch (Exception e) { + throw new MojoFailureException( + String.format( + "Failed to set the version to %s - the provided version is required to be a valid semver", + semver), + e); + } } } diff --git a/src/main/java/q3769/maven/plugins/semver/mojos/UpdateBuildMetadata.java b/src/main/java/q3769/maven/plugins/semver/mojos/UpdateBuildMetadata.java index 3c08d7e..501ccaf 100644 --- a/src/main/java/q3769/maven/plugins/semver/mojos/UpdateBuildMetadata.java +++ b/src/main/java/q3769/maven/plugins/semver/mojos/UpdateBuildMetadata.java @@ -25,6 +25,7 @@ import com.github.zafarkhaja.semver.Version; import lombok.NonNull; +import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import q3769.maven.plugins.semver.LabelUpdater; @@ -46,12 +47,20 @@ public class UpdateBuildMetadata extends LabelUpdater { */ @Deprecated(since = "20240115.0.0") @Override - protected Version incrementLabel(@NonNull Version version) { - return version.incrementBuildMetadata(); + protected Version incrementLabel(@NonNull Version version) throws MojoFailureException { + try { + return version.incrementBuildMetadata(); + } catch (Exception e) { + throw new MojoFailureException("Failed to increment build metadata label for " + version, e); + } } @Override - protected Version setLabel(@NonNull Version version, String label) { - return version.withBuildMetadata(label); + protected Version setLabel(@NonNull Version version, String label) throws MojoFailureException { + try { + return version.withBuildMetadata(label); + } catch (Exception e) { + throw new MojoFailureException("Failed to set build metadata label for " + version, e); + } } } diff --git a/src/main/java/q3769/maven/plugins/semver/mojos/UpdatePreRelease.java b/src/main/java/q3769/maven/plugins/semver/mojos/UpdatePreRelease.java index ce89e59..b0613f8 100644 --- a/src/main/java/q3769/maven/plugins/semver/mojos/UpdatePreRelease.java +++ b/src/main/java/q3769/maven/plugins/semver/mojos/UpdatePreRelease.java @@ -24,6 +24,7 @@ package q3769.maven.plugins.semver.mojos; import com.github.zafarkhaja.semver.Version; +import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import q3769.maven.plugins.semver.LabelUpdater; @@ -38,12 +39,20 @@ public class UpdatePreRelease extends LabelUpdater { @Override - protected Version incrementLabel(Version version) { - return version.nextPreReleaseVersion(); + protected Version incrementLabel(Version version) throws MojoFailureException { + try { + return version.nextPreReleaseVersion(); + } catch (Exception e) { + throw new MojoFailureException("Failed to increment pre-release label for " + version, e); + } } @Override - protected Version setLabel(Version version, String label) { - return version.nextPreReleaseVersion(label); + protected Version setLabel(Version version, String label) throws MojoFailureException { + try { + return version.nextPreReleaseVersion(label); + } catch (Exception e) { + throw new MojoFailureException("Failed to set pre-release label for " + version, e); + } } } diff --git a/src/test/java/q3769/maven/plugins/semver/mojos/MergeTest.java b/src/test/java/q3769/maven/plugins/semver/mojos/MergeTest.java index 8811dd2..c40010c 100644 --- a/src/test/java/q3769/maven/plugins/semver/mojos/MergeTest.java +++ b/src/test/java/q3769/maven/plugins/semver/mojos/MergeTest.java @@ -30,6 +30,7 @@ import com.github.zafarkhaja.semver.Version; import elf4j.Logger; +import org.apache.maven.plugin.MojoFailureException; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import q3769.maven.plugins.semver.SemverNormalVersion; @@ -42,7 +43,7 @@ class MergeTest { @Nested class update { @Test - void whenOriginalVersionIsNewer() { + void whenOriginalVersionIsNewer() throws MojoFailureException { Version original = Version.parse("1.4.0-SNAPSHOT"); Version toMerge = Version.parse("1.3.4-hotfix"); info.log("Merging " + toMerge + " to " + original); @@ -56,7 +57,7 @@ void whenOriginalVersionIsNewer() { } @Test - void whenOriginalVersionIsOlder() { + void whenOriginalVersionIsOlder() throws MojoFailureException { Version original = Version.parse("1.2.0-pre-release.1+build.metadata"); Version toMerge = Version.parse("1.3.4-hotfix"); info.log("Merging " + toMerge + " to " + original);