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 5bddaa6 commit 9a05b66
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 45 deletions.
4 changes: 2 additions & 2 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.202408091532</version>
<version>20240116.0.202408091618</version>
<packaging>maven-plugin</packaging>

<name>semver-maven-plugin</name>
Expand Down Expand Up @@ -229,7 +229,7 @@
<plugin>
<groupId>io.github.q3769</groupId>
<artifactId>semver-maven-plugin</artifactId>
<version>20240116.0.202408091532</version>
<version>20240116.0.202408091618</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/q3769/maven/plugins/semver/SemverMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ protected String originalPomVersion() {
return project.getOriginalModel().getVersion();
}

protected void logError(String message, Object... args) {
getLog().error(String.format(message, args));
}

protected void logError(Throwable t, String message, Object... args) {
getLog().error(String.format(message, args), t);
}

protected void logWarn(String message, Object... args) {
getLog().warn(String.format(message, args));
}
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/q3769/maven/plugins/semver/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
*/
public abstract class Updater extends SemverMojo {
private static final String SNAPSHOT = "SNAPSHOT";

/**
* Flag to append SNAPSHOT as the prerelease label in the target version. Expected to be passed in
* as a -D parameter from CLI.
*/
@Parameter(property = "snapshot", defaultValue = "false")
protected boolean addingSnapshotLabel;

/** */
@Component
protected BuildPluginManager pluginManager;
Expand All @@ -67,14 +69,17 @@ protected void doExecute() throws MojoExecutionException, MojoFailureException {
* @throws MojoFailureException if original version in POM is malformed
*/
private Version getUpdatedVersion() throws MojoFailureException {
Version updatedVersion = update(requireValidSemVer(project.getVersion()));
Version original = requireValidSemVer(project.getVersion());
Version updatedVersion = update(original);
if (!addingSnapshotLabel) {
return updatedVersion;
}
if (hasPreReleaseVersionOrBuildMetadata(updatedVersion)) {
throw new MojoFailureException(String.format(
"snapshot labeling requested for updated semver %s but not honored, because snapshot flag only supports normal version number increments with no labels",
updatedVersion));
logError(
"SNAPSHOT labeling requested for POM version %s but not honored, because SNAPSHOT may collide with other labels in the updated version %s",
original, updatedVersion);
throw new MojoFailureException(
String.format("SNAPSHOT may collide with other labels in %s", updatedVersion));
}
logInfo("labeling version %s as a SNAPSHOT...", updatedVersion);
return addSnapshotLabel(updatedVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ protected Version update(Version original) throws MojoFailureException {
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);
logError(
e,
"Failed to calendar-increment the %s version of semver %s",
SemverNormalVersion.MAJOR,
original);
throw new MojoFailureException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ protected Version update(Version original) throws MojoFailureException {
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);
logError(
e,
"Failed to calendar-increment the %s version of semver %s",
SemverNormalVersion.MINOR,
original);
throw new MojoFailureException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ protected Version update(Version original) throws MojoFailureException {
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);
logError(
e,
"Failed to calendar-increment the %s version of semver %s",
SemverNormalVersion.PATCH,
original);
throw new MojoFailureException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ 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);
logError(e, "Failed to increment the major version of semver %s", original);
throw new MojoFailureException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ 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);
logError(e, "Failed to increment the minor version of semver %s", original);
throw new MojoFailureException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ 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);
logError(e, "Failed to increment the patch version of semver %s", original);
throw new MojoFailureException(e);
}
}
}
7 changes: 3 additions & 4 deletions src/main/java/q3769/maven/plugins/semver/mojos/Merge.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ protected Version update(final Version original) throws MojoFailureException {
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);
logError(
e, "Failed to merge the provided version %s with the POM version %s", other, original);
throw new MojoFailureException(e);
}
logDebug(
"Incrementing provided version %s on POM semver incremented normal version %s, provisional merge version: %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ protected Version update(@NonNull final Version original) throws MojoFailureExce
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);
logError(
e,
"Failed to calendar-merge provided version %s with the POM version %s",
other,
original);
throw new MojoFailureException(e);
}
logDebug(
"Incrementing provided version %s on POM semver incremented normal version %s, provisional merge version: %s",
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/q3769/maven/plugins/semver/mojos/SetCurrent.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ 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);
logError(
e,
"Failed to set the version to %s - the provided version is required to be a valid semver",
semver);
throw new MojoFailureException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ protected Version incrementLabel(@NonNull Version version) throws MojoFailureExc
try {
return version.incrementBuildMetadata();
} catch (Exception e) {
throw new MojoFailureException("Failed to increment build metadata label for " + version, e);
logError("Failed to increment build metadata label for %s", version);
throw new MojoFailureException(e);
}
}

Expand All @@ -60,7 +61,8 @@ protected Version setLabel(@NonNull Version version, String label) throws MojoFa
try {
return version.withBuildMetadata(label);
} catch (Exception e) {
throw new MojoFailureException("Failed to set build metadata label for " + version, e);
logError(e, "Failed to set build metadata label for %s", version);
throw new MojoFailureException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ protected Version setLabel(Version version, String label) throws MojoFailureExce
try {
return version.nextPreReleaseVersion(label);
} catch (Exception e) {
throw new MojoFailureException("Failed to set pre-release label for " + version, e);
logError(e, "Failed to set pre-release label for %s", version);
throw new MojoFailureException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ protected void doExecute() throws MojoFailureException {
try {
requireValidSemVer(version);
} catch (Exception e) {
throw new MojoFailureException(String.format("Invalid SemVer '%s'", version), e);
logError(e, "POM version '%s' is not a valid SemVer", version);
throw new MojoFailureException(e);
}
logInfo("POM version '%s' is a valid SemVer", version);
if (forceStdOut) {
Expand Down

0 comments on commit 9a05b66

Please sign in to comment.