-
Notifications
You must be signed in to change notification settings - Fork 145
Fixed #1426 : Replace archived org.semver:api with custom SemVer implementation
#1430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
69d89c3
Replace archived org.semver:api with custom SemVer implementation
HarshMehta112 f9c3e46
Merge branch 'master' of https://github.com/HarshMehta112/maven-relea…
harsh-mehta-sb 94b02df
Refactor SemVer implementation: replace 'Version' with 'SemVer' in ve…
HarshMehta112 0630440
Removed unrelated changes.
HarshMehta112 b0e3e62
Removed unrelated changes.
HarshMehta112 74f9ce9
Merge branch 'master' of https://github.com/HarshMehta112/maven-relea…
harsh-mehta-sb 2ec8719
Refactor SemVer class: change access modifiers and simplify version p…
HarshMehta112 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
270 changes: 270 additions & 0 deletions
270
...ase-semver-policy/src/main/java/org/apache/maven/shared/release/policy/semver/SemVer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,270 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.maven.shared.release.policy.semver; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * A simple Semantic Versioning 2.0.0 implementation. | ||
HarshMehta112 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * <p> | ||
| * This class provides basic parsing and manipulation of semantic version strings | ||
| * following the format: MAJOR.MINOR.PATCH[-PRERELEASE][+METADATA] | ||
| * </p> | ||
| * | ||
| * @see <a href="https://semver.org/">Semantic Versioning 2.0.0</a> | ||
| */ | ||
| public class SemVer { | ||
HarshMehta112 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
HarshMehta112 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Regex pattern for parsing semantic versions from semver.org. | ||
| * Groups: 1=major, 2=minor, 3=patch, 4=prerelease, 5=metadata | ||
| * | ||
| * @see <a href="https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string">SemVer Regex</a> | ||
| */ | ||
| private static final Pattern SEMVER_PATTERN = Pattern.compile("^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)" | ||
| + "(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)" | ||
| + "(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$"); | ||
|
|
||
| private final int major; | ||
| private final int minor; | ||
| private final int patch; | ||
| private final String preRelease; | ||
| private final String metadata; | ||
|
|
||
| /** | ||
| * Version element types that can be incremented. | ||
| */ | ||
| public enum Element { | ||
| MAJOR, | ||
| MINOR, | ||
| PATCH | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a new SemVer instance. | ||
| * | ||
| * @param major the major version number | ||
| * @param minor the minor version number | ||
| * @param patch the patch version number | ||
| * @param preRelease the pre-release identifier (can be null) | ||
| * @param metadata the build metadata (can be null) | ||
| */ | ||
| public SemVer(int major, int minor, int patch, String preRelease, String metadata) { | ||
| if (major < 0 || minor < 0 || patch < 0) { | ||
| throw new IllegalArgumentException("Version numbers must be non-negative"); | ||
| } | ||
| this.major = major; | ||
| this.minor = minor; | ||
| this.patch = patch; | ||
| this.preRelease = preRelease; | ||
| this.metadata = metadata; | ||
| } | ||
|
|
||
| /** | ||
| * Parses a version string into a SemVer object. | ||
| * | ||
| * @param version the version string to parse | ||
| * @return the parsed SemVer object | ||
| * @throws IllegalArgumentException if the version string is invalid | ||
| */ | ||
| public static SemVer parse(String version) { | ||
| if (version == null || version.trim().isEmpty()) { | ||
| throw new IllegalArgumentException("Version string cannot be null or empty"); | ||
| } | ||
|
|
||
| Matcher matcher = SEMVER_PATTERN.matcher(version.trim()); | ||
| if (!matcher.matches()) { | ||
| throw new IllegalArgumentException("Invalid semantic version format: " + version); | ||
| } | ||
|
|
||
| int major = Integer.parseInt(matcher.group(1)); | ||
| int minor = Integer.parseInt(matcher.group(2)); | ||
| int patch = Integer.parseInt(matcher.group(3)); | ||
| String preRelease = matcher.group(4); | ||
| String metadata = matcher.group(5); | ||
|
|
||
| return new SemVer(major, minor, patch, preRelease, metadata); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a new SemVer with the release version (removes -SNAPSHOT and any pre-release/metadata). | ||
| * <p> | ||
| * Examples: | ||
| * <ul> | ||
| * <li>1.2.3-SNAPSHOT → 1.2.3</li> | ||
| * <li>1.2.3-beta+build → 1.2.3</li> | ||
| * <li>1.2.3 → 1.2.3</li> | ||
| * </ul> | ||
| * | ||
| * @return a new SemVer representing the release version | ||
| */ | ||
| public SemVer toReleaseVersion() { | ||
| return new SemVer(major, minor, patch, null, null); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a new SemVer with -SNAPSHOT appended as pre-release identifier. | ||
| * If the version already has a pre-release identifier, it will be replaced with SNAPSHOT. | ||
| * Metadata is removed. | ||
| * <p> | ||
| * Examples: | ||
| * <ul> | ||
| * <li>1.2.3 → 1.2.3-SNAPSHOT</li> | ||
| * <li>1.2.3-beta → 1.2.3-SNAPSHOT</li> | ||
| * <li>1.2.3+build → 1.2.3-SNAPSHOT</li> | ||
| * <li>1.2.3-SNAPSHOT → 1.2.3-SNAPSHOT (no change)</li> | ||
| * </ul> | ||
| * | ||
| * @return a new SemVer with SNAPSHOT pre-release identifier | ||
| */ | ||
| public SemVer toSnapshotVersion() { | ||
| return new SemVer(major, minor, patch, "SNAPSHOT", null); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a new SemVer with the specified element incremented. | ||
| * If the version has pre-release or metadata, returns the release version without incrementing. | ||
| * Otherwise, increments the specified element and all lower elements are reset to 0. | ||
| * Pre-release and metadata are always cleared in the result. | ||
| * | ||
| * @param element the element to increment (MAJOR, MINOR, or PATCH) | ||
| * @return a new SemVer with the specified element incremented (or release version if pre-release/metadata present) | ||
| */ | ||
| public SemVer next(Element element) { | ||
| Objects.requireNonNull(element, "Element cannot be null"); | ||
|
|
||
| // If version has pre-release or metadata, just return release version without incrementing | ||
| if (hasPreRelease() || hasMetadata()) { | ||
| return toReleaseVersion(); | ||
| } | ||
|
|
||
| switch (element) { | ||
| case MAJOR: | ||
| return new SemVer(major + 1, 0, 0, null, null); | ||
| case MINOR: | ||
| return new SemVer(major, minor + 1, 0, null, null); | ||
| case PATCH: | ||
| return new SemVer(major, minor, patch + 1, null, null); | ||
| default: | ||
| throw new IllegalArgumentException("Unknown element: " + element); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Checks if this version has a pre-release identifier. | ||
| * | ||
| * @return true if pre-release identifier is present, false otherwise | ||
| */ | ||
| public boolean hasPreRelease() { | ||
HarshMehta112 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return preRelease != null && !preRelease.isEmpty(); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if this version has build metadata. | ||
| * | ||
| * @return true if build metadata is present, false otherwise | ||
| */ | ||
| public boolean hasMetadata() { | ||
| return metadata != null && !metadata.isEmpty(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the major version number. | ||
| * | ||
| * @return the major version | ||
| */ | ||
| public int getMajor() { | ||
| return major; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the minor version number. | ||
| * | ||
| * @return the minor version | ||
| */ | ||
| public int getMinor() { | ||
| return minor; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the patch version number. | ||
| * | ||
| * @return the patch version | ||
| */ | ||
| public int getPatch() { | ||
| return patch; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the pre-release identifier. | ||
| * | ||
| * @return the pre-release identifier, or null if not present | ||
| */ | ||
| public String getPreRelease() { | ||
| return preRelease; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the build metadata. | ||
| * | ||
| * @return the build metadata, or null if not present | ||
| */ | ||
| public String getMetadata() { | ||
| return metadata; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| StringBuilder sb = new StringBuilder(); | ||
| sb.append(major).append('.').append(minor).append('.').append(patch); | ||
|
|
||
| if (preRelease != null && !preRelease.isEmpty()) { | ||
| sb.append('-').append(preRelease); | ||
| } | ||
|
|
||
| if (metadata != null && !metadata.isEmpty()) { | ||
| sb.append('+').append(metadata); | ||
| } | ||
|
|
||
| return sb.toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
HarshMehta112 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| SemVer semVer = (SemVer) o; | ||
| return major == semVer.major | ||
| && minor == semVer.minor | ||
| && patch == semVer.patch | ||
| && Objects.equals(preRelease, semVer.preRelease) | ||
| && Objects.equals(metadata, semVer.metadata); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(major, minor, patch, preRelease, metadata); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.