Skip to content

Commit

Permalink
+ better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
q3769 committed Aug 9, 2024
1 parent 6205954 commit 5bddaa6
Show file tree
Hide file tree
Showing 17 changed files with 139 additions and 47 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<groupId>io.github.q3769</groupId>
<artifactId>semver-maven-plugin</artifactId>
<version>20240116.0.202408090614</version>
<version>20240116.0.202408091532</version>
<packaging>maven-plugin</packaging>

<name>semver-maven-plugin</name>
Expand Down Expand Up @@ -214,7 +214,7 @@
<java>
<removeUnusedImports/>
<palantirJavaFormat>
<version>2.48.0</version>
<version>2.49.0</version>
<style>GOOGLE</style>
<formatJavadoc>true</formatJavadoc>
</palantirJavaFormat>
Expand All @@ -229,7 +229,7 @@
<plugin>
<groupId>io.github.q3769</groupId>
<artifactId>semver-maven-plugin</artifactId>
<version>20240116.0.202408090614</version>
<version>20240116.0.202408091532</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/q3769/maven/plugins/semver/LabelUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/q3769/maven/plugins/semver/SemverMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()) {
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
}
15 changes: 12 additions & 3 deletions src/main/java/q3769/maven/plugins/semver/mojos/Merge.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)) {
Expand All @@ -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);
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/q3769/maven/plugins/semver/mojos/MergeCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/q3769/maven/plugins/semver/mojos/SetCurrent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
}
Loading

0 comments on commit 5bddaa6

Please sign in to comment.