You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MavenVersionManager.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package org.pf4j;
  2. import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
  3. import org.apache.maven.artifact.versioning.VersionRange;
  4. import org.pf4j.util.StringUtils;
  5. /**
  6. * Implementation for {@link VersionManager}.
  7. * This implementation uses Maven . Before using it you have to include the optional dependency maven-artifact
  8. *
  9. * @author Wolfram Haussig
  10. */
  11. public class MavenVersionManager implements VersionManager {
  12. /**
  13. * parses a version with the current parser type
  14. * @param version
  15. */
  16. protected DefaultArtifactVersion parseVersion(String version) {
  17. if (version == null)
  18. return new DefaultArtifactVersion("");
  19. return new DefaultArtifactVersion(version);
  20. }
  21. /**
  22. * Checks if a version satisfies the specified SemVer {@link Expression} string.
  23. * If the constraint is empty or null then the method returns true.
  24. * Constraint examples: {@code >2.0.0} (simple), {@code "1.1.1 || 1.2.3 - 2.0.0"} (range).
  25. * See https://github.com/vdurmont/semver4j#requirements for more info.
  26. *
  27. * @param version
  28. * @param constraint
  29. * @return
  30. */
  31. @Override
  32. public boolean checkVersionConstraint(String version, String constraint) {
  33. if (StringUtils.isNullOrEmpty(constraint) || "*".equals(constraint)) {
  34. return true;
  35. }
  36. try {
  37. return VersionRange.createFromVersionSpec(constraint).containsVersion(parseVersion(version));
  38. } catch (org.apache.maven.artifact.versioning.InvalidVersionSpecificationException e) {
  39. //throw custom InvalidVersionSpecificationException as the interface does not declare an exception to be thrown
  40. //so we need a RuntimeException here
  41. throw new InvalidVersionSpecificationException("failed to parse constraint as maven version range: " + constraint, e);
  42. }
  43. }
  44. @Override
  45. public int compareVersions(String v1, String v2) {
  46. return parseVersion(v1).compareTo(parseVersion(v2));
  47. }
  48. @Override
  49. public boolean isStable(String version) {
  50. DefaultArtifactVersion av = parseVersion(version);
  51. return av.getQualifier() == null || !"SNAPSHOT".equals(av.getQualifier());
  52. }
  53. public static class InvalidVersionSpecificationException extends RuntimeException
  54. {
  55. private static final long serialVersionUID = 8636081416771885576L;
  56. public InvalidVersionSpecificationException( String message, Throwable cause )
  57. {
  58. super( message, cause);
  59. }
  60. }
  61. }