[maven-release] branch master updated: OddEvenVersionPolicy should not depend on maven-release-manager

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[maven-release] branch master updated: OddEvenVersionPolicy should not depend on maven-release-manager

rfscholte
This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-release.git


The following commit(s) were added to refs/heads/master by this push:
     new e505fac  OddEvenVersionPolicy should not depend on maven-release-manager
e505fac is described below

commit e505fac1b055f0fdeb2d84c24ad82cdd6a601d7f
Author: rfscholte <[hidden email]>
AuthorDate: Fri Jan 12 16:20:38 2018 +0100

    OddEvenVersionPolicy should not depend on maven-release-manager
---
 .../maven/shared/release/versions/Version.java     | 66 +++++++++++++++++--
 .../maven-release-oddeven-policy/pom.xml           | 12 ++--
 .../policy/oddeven/OddEvenVersionPolicy.java       | 77 +++++++++++++---------
 .../maven-release-semver-policy/pom.xml            |  1 -
 4 files changed, 114 insertions(+), 42 deletions(-)

diff --git a/maven-release-api/src/main/java/org/apache/maven/shared/release/versions/Version.java b/maven-release-api/src/main/java/org/apache/maven/shared/release/versions/Version.java
index b41593f..c408756 100644
--- a/maven-release-api/src/main/java/org/apache/maven/shared/release/versions/Version.java
+++ b/maven-release-api/src/main/java/org/apache/maven/shared/release/versions/Version.java
@@ -21,6 +21,7 @@ package org.apache.maven.shared.release.versions;
 
 import java.util.Arrays;
 import java.util.List;
+import java.util.Objects;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -31,7 +32,7 @@ import org.codehaus.plexus.util.StringUtils;
  *
  */
 public class Version
-    implements Comparable<Version>
+    implements Comparable<Version>, Cloneable
 {
     private final AetherVersion aetherVersion;
 
@@ -51,7 +52,7 @@ public class Version
 
     private String annotationRevSeparator;
 
-    private final String buildSeparator;
+    private String buildSeparator;
 
     private static final int DIGITS_INDEX = 1;
 
@@ -70,6 +71,10 @@ public class Version
     private static final String SNAPSHOT_IDENTIFIER = "SNAPSHOT";
 
     private static final String DIGIT_SEPARATOR_STRING = ".";
+    
+    private static final String DEFAULT_ANNOTATION_REV_SEPARATOR = "-";
+
+    private static final String DEFAULT_BUILD_SEPARATOR = "-";
 
     public static final Pattern STANDARD_PATTERN = Pattern.compile( "^((?:\\d+\\.)*\\d+)" // digit(s) and '.' repeated -
                                                                                           // followed by digit (version
@@ -86,6 +91,23 @@ public class Version
      */
     // for SNAPSHOT releases only (possible versions include: trunk-SNAPSHOT or SNAPSHOT)
     public static final Pattern ALTERNATE_PATTERN = Pattern.compile( "^(SNAPSHOT|[a-zA-Z]+[_-]SNAPSHOT)" );
+    
+    private Version( List<String> digits, String annotation, String annotationRevision, String buildSpecifier,
+                               String annotationSeparator, String annotationRevSeparator, String buildSeparator )
+    {
+        this.digits = digits;
+        this.annotation = annotation;
+        this.annotationRevision = annotationRevision;
+        this.buildSpecifier = buildSpecifier;
+        this.annotationSeparator = annotationSeparator;
+        this.annotationRevSeparator = annotationRevSeparator;
+        this.buildSeparator = buildSeparator;
+        this.strVersion = getVersionString( this, buildSpecifier, buildSeparator );
+
+        // for now no need to reparse, original version was valid
+        this.aetherVersion = null;
+        this.mavenArtifactVersion = null;
+    }
 
     public Version( String version )
         throws VersionParseException
@@ -220,7 +242,7 @@ public class Version
     {
         return digits;
     }
-
+    
     public String getAnnotation()
     {
         return annotation;
@@ -245,7 +267,43 @@ public class Version
     {
         return buildSpecifier;
     }
-
+    
+    /**
+     *
+     * @param newDigits the new list of digits
+     * @return a new instance of Version
+     */
+    public Version setDigits( List<String> newDigits )
+    {
+        return new Version( newDigits, this.annotation, this.annotationRevision, this.buildSpecifier,
+                            this.annotationSeparator, this.annotationRevSeparator, this.buildSeparator );
+    }
+    
+    /**
+     *
+     * @param newAnnotationRevision the new annotation revision
+     * @return a new instance of Version
+     */
+    public Version setAnnotationRevision( String newAnnotationRevision )
+    {
+        return new Version( this.digits, this.annotation, newAnnotationRevision, this.buildSpecifier,
+                            this.annotationSeparator,
+                            Objects.toString( this.annotationRevSeparator, DEFAULT_ANNOTATION_REV_SEPARATOR ),
+                            this.buildSeparator );
+    }
+    
+    /**
+     *
+     * @param newBuildSpecifier the new build specifier
+     * @return a new instance of Version
+     */
+    public Version setBuildSpecifier( String newBuildSpecifier )
+    {
+        return new Version( this.digits, this.annotation, this.annotationRevision, newBuildSpecifier,
+                            this.annotationSeparator, this.annotationRevSeparator,
+                            Objects.toString( this.buildSeparator, DEFAULT_BUILD_SEPARATOR ) );
+    }
+    
     /**
      * @throws VersionComparisonConflictException if {@link org.eclipse.aether.version.Version} and
      *             {@link org.apache.maven.artifact.versioning.ArtifactVersion ArtifactVersion} give different results
diff --git a/maven-release-policies/maven-release-oddeven-policy/pom.xml b/maven-release-policies/maven-release-oddeven-policy/pom.xml
index de5ed3c..37b23c6 100644
--- a/maven-release-policies/maven-release-oddeven-policy/pom.xml
+++ b/maven-release-policies/maven-release-oddeven-policy/pom.xml
@@ -47,12 +47,6 @@
       <version>${project.version}</version>
     </dependency>
     <dependency>
-      <groupId>org.apache.maven.release</groupId>
-      <artifactId>maven-release-manager</artifactId>
-      <version>${project.version}</version>
-    </dependency>
-
-    <dependency>
       <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-component-annotations</artifactId>
       <optional>true</optional>
@@ -63,6 +57,12 @@
       <artifactId>junit</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.eclipse.aether</groupId>
+      <artifactId>aether-util</artifactId>
+      <version>1.0.0.v20140518</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
diff --git a/maven-release-policies/maven-release-oddeven-policy/src/main/java/org/apache/maven/shared/release/policy/oddeven/OddEvenVersionPolicy.java b/maven-release-policies/maven-release-oddeven-policy/src/main/java/org/apache/maven/shared/release/policy/oddeven/OddEvenVersionPolicy.java
index 58a83d7..2aedf9c 100644
--- a/maven-release-policies/maven-release-oddeven-policy/src/main/java/org/apache/maven/shared/release/policy/oddeven/OddEvenVersionPolicy.java
+++ b/maven-release-policies/maven-release-oddeven-policy/src/main/java/org/apache/maven/shared/release/policy/oddeven/OddEvenVersionPolicy.java
@@ -21,12 +21,12 @@ package org.apache.maven.shared.release.policy.oddeven;
 
 import java.util.List;
 
+import org.apache.maven.artifact.Artifact;
 import org.apache.maven.shared.release.policy.PolicyException;
 import org.apache.maven.shared.release.policy.version.VersionPolicy;
 import org.apache.maven.shared.release.policy.version.VersionPolicyRequest;
 import org.apache.maven.shared.release.policy.version.VersionPolicyResult;
-import org.apache.maven.shared.release.versions.DefaultVersionInfo;
-import org.apache.maven.shared.release.versions.VersionInfo;
+import org.apache.maven.shared.release.versions.Version;
 import org.apache.maven.shared.release.versions.VersionParseException;
 import org.codehaus.plexus.component.annotations.Component;
 import org.codehaus.plexus.util.StringUtils;
@@ -48,12 +48,14 @@ public final class OddEvenVersionPolicy
     implements VersionPolicy
 {
 
+    @Override
     public VersionPolicyResult getReleaseVersion( VersionPolicyRequest request )
         throws PolicyException
     {
         return calculateNextVersion( request, false );
     }
 
+    @Override
     public VersionPolicyResult getDevelopmentVersion( VersionPolicyRequest request )
         throws PolicyException
     {
@@ -62,52 +64,34 @@ public final class OddEvenVersionPolicy
 
     private VersionPolicyResult calculateNextVersion( VersionPolicyRequest request, boolean development )
     {
-        DefaultVersionInfo defaultVersionInfo = null;
+        Version defaultVersionInfo = null;
 
         try
         {
-            defaultVersionInfo = new DefaultVersionInfo( request.getVersion() );
+            defaultVersionInfo = new Version( request.getVersion() );
         }
         catch ( VersionParseException e )
         {
             throw new IllegalArgumentException( "Can't tell if version with no digits is even: " + e.getMessage(), e );
         }
 
-        // by default, never reuse revisions
-        int versionNumbersToSkip = 1;
+        Version newVersion = newVersion( defaultVersionInfo, development );
 
-        // do we need a snapshot? make sure the version info is odd
-        if ( development && !isEven( defaultVersionInfo ) )
-        {
-            versionNumbersToSkip = 2;
-        }
-
-        // do we need a release? make sure the version info is even
-        if ( !development && isEven( defaultVersionInfo ) )
-        {
-            versionNumbersToSkip = 0;
-        }
-
-        VersionInfo suggestedVersionInfo = defaultVersionInfo;
-        while ( versionNumbersToSkip != 0 )
-        {
-            suggestedVersionInfo = suggestedVersionInfo.getNextVersion();
-            versionNumbersToSkip--;
-        }
-
-        String nextVersion = development ? suggestedVersionInfo.getSnapshotVersionString()
-                                         : suggestedVersionInfo.getReleaseVersionString();
-
-        return new VersionPolicyResult().setVersion( nextVersion );
+        return new VersionPolicyResult().setVersion( newVersion.toString()  );
     }
 
-    private boolean isEven( DefaultVersionInfo defaultVersionInfo )
+    private Version newVersion( Version defaultVersionInfo, boolean development )
     {
+        Version newVersion;
         int mostSignificantSegment;
 
         if ( StringUtils.isNumeric( defaultVersionInfo.getAnnotationRevision() ) )
         {
             mostSignificantSegment = Integer.parseInt( defaultVersionInfo.getAnnotationRevision() );
+
+            int skip = getVersionIncrements( development, mostSignificantSegment % 2 == 0 );
+
+            newVersion = defaultVersionInfo.setAnnotationRevision( String.valueOf( mostSignificantSegment + skip ) );
         }
         else
         {
@@ -119,9 +103,40 @@ public final class OddEvenVersionPolicy
             }
 
             mostSignificantSegment = Integer.parseInt( digits.get( digits.size() - 1 ) );
+
+            int skip = getVersionIncrements( development, mostSignificantSegment % 2 == 0 );
+
+            digits.set( digits.size() - 1, String.valueOf( String.valueOf( mostSignificantSegment + skip ) ) );
+
+            newVersion = defaultVersionInfo.setDigits( digits );
         }
 
-        return mostSignificantSegment % 2 == 0;
+        if ( development )
+        {
+            return newVersion.setBuildSpecifier( Artifact.SNAPSHOT_VERSION );
+        }
+        else
+        {
+            return newVersion.setBuildSpecifier( null );
+        }
     }
 
+    private int getVersionIncrements( boolean development, boolean isEven )
+    {
+        if ( development && !isEven )
+        {
+            // do we need a snapshot? make sure the version info is odd
+            return 2;
+        }
+        else if ( !development && isEven )
+        {
+            // do we need a release? make sure the version info is even
+            return 0;
+        }
+        else
+        {
+            // by default, never reuse revisions
+            return 1;
+        }
+    }
 }
diff --git a/maven-release-policies/maven-release-semver-policy/pom.xml b/maven-release-policies/maven-release-semver-policy/pom.xml
index ef11d01..e5aa951 100644
--- a/maven-release-policies/maven-release-semver-policy/pom.xml
+++ b/maven-release-policies/maven-release-semver-policy/pom.xml
@@ -58,7 +58,6 @@
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
-      <version>4.11</version>
       <scope>test</scope>
     </dependency>
   </dependencies>

--
To stop receiving notification emails like this one, please contact
['"[hidden email]" <[hidden email]>'].