Author: cziegeler
Date: Fri Sep 29 15:02:10 2017
New Revision: 1810118

URL: http://svn.apache.org/viewvc?rev=1810118&view=rev
Log:
Support maven coordinates

Modified:
    sling/whiteboard/cziegeler/feature/readme.md
    
sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/ArtifactId.java
    
sling/whiteboard/cziegeler/feature/src/test/java/org/apache/sling/feature/ArtifactIdTest.java

Modified: sling/whiteboard/cziegeler/feature/readme.md
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature/readme.md?rev=1810118&r1=1810117&r2=1810118&view=diff
==============================================================================
--- sling/whiteboard/cziegeler/feature/readme.md (original)
+++ sling/whiteboard/cziegeler/feature/readme.md Fri Sep 29 15:02:10 2017
@@ -72,7 +72,7 @@ TBD We need to define a common type for
 
 # Maven coordinates
 
-Maven coordinates are used to describe artifacts, e.g. bundles, content 
packages or includes. In these cases either the short notation (mvn url without 
the protocol) can be used or the long version as a JSON object with an id 
property.
+Maven coordinates are used to describe artifacts, e.g. bundles, content 
packages or includes. In these cases either the short notation (as described 
here: https://maven.apache.org/pom.html#Maven_Coordinates) can be used or the 
long version as a JSON object with an id property.
 
 # Requirements and Capabilities vs Dependencies
 
@@ -135,11 +135,11 @@ In fact, configurations - whether they a
 This is a feature example:
 
     {
-      "id" : "org.apache.sling/my.app/1.0/optional",
+      "id" : "org.apache.sling:my.app:feature:optional:1.0",
 
       "includes" : [
          {
-             "id" : "org.apache.sling/sling/9",
+             "id" : "org.apache.sling:sling:9",
              "removals" : {
                  "configurations" : [
                  ],
@@ -187,14 +187,14 @@ This is a feature example:
       "bundles" : {
         "1" : [
             {
-              "id" : "org.apache.sling/security-server/2.2.0",
+              "id" : "org.apache.sling:security-server:2.2.0",
               "hash" : "4632463464363646436"
             },
-            "org.apache.sling/application-bundle/2.0.0",
-            "org.apache.sling/another-bundle/2.1.0"
+            "org.apache.sling:application-bundle:2.0.0",
+            "org.apache.sling:another-bundle:2.1.0"
           ],
         "2" : [
-            "org.apache.sling/foo-xyz/1.2.3"
+            "org.apache.sling:foo-xyz:1.2.3"
           ]
       },
       "configurations" {
@@ -216,12 +216,12 @@ An optional application configuration fu
 
     {
          "features" : [
-             "org.apache.sling/org.apache.sling.launchpad/10"
+             "org.apache.sling:org.apache.sling.launchpad:10"
          ],
          "options" : [
-             "org.apache.sling/org.apache.sling.scripting.jsp/1.0.0",
+             "org.apache.sling:org.apache.sling.scripting.jsp:1.0.0",
              {
-                 "id" : 
"org.apache.sling/org.apache.sling.scripting.htl/1.0.0",
+                 "id" : 
"org.apache.sling:org.apache.sling.scripting.htl:1.0.0",
                  "tag": "htl"
              }
          ],
@@ -230,7 +230,7 @@ An optional application configuration fu
              "tags" : ["htl"]
          },
          "framework" : {
-             "id" : "org.apache.felix/org.apache.felix.framework/5.6.4"
+             "id" : "org.apache.felix:org.apache.felix.framework:5.6.4"
          }
     }
 

Modified: 
sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/ArtifactId.java
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/ArtifactId.java?rev=1810118&r1=1810117&r2=1810118&view=diff
==============================================================================
--- 
sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/ArtifactId.java
 (original)
+++ 
sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/ArtifactId.java
 Fri Sep 29 15:02:10 2017
@@ -16,12 +16,12 @@
  */
 package org.apache.sling.feature;
 
-import org.osgi.framework.Version;
-
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import org.osgi.framework.Version;
+
 /**
  * An artifact identifier.
  *
@@ -81,31 +81,37 @@ public class ArtifactId implements Compa
     }
 
     /**
-     * Create a new artifact from a maven url,
+     * Create a new artifact id from a string, the string must either be a
+     * mvn url or a mvn id (= coordinates)
+     * @param s The string to parse
+     * @return The artifact id
+     * @throws IllegalArgumentException if the string can't be parsed to a 
valid artifact id.
+     */
+    public static ArtifactId parse(final String s) {
+        if ( s.contains(":") ) {
+            return ArtifactId.fromMvnId(s);
+        } else if ( s.contains("/") ) {
+            return ArtifactId.fromMvnUrl(s);
+        }
+        throw new IllegalArgumentException("Unable to parse mvn 
coordinates/url: " + s);
+    }
+
+    /**
+     * Create a new artifact id from a maven url,
      * 'mvn:' group-id '/' artifact-id [ '/' [version] [ '/' [type] [ '/' 
classifier ] ] ] ]
      * @param url The url
-     * @return A new artifact
+     * @return A new artifact id
      * @throws IllegalArgumentException If the url is not valid
      */
     public static ArtifactId fromMvnUrl(final String url) {
-        if ( url == null || !url.startsWith("mvn:") ) {
+        if ( url == null || (url.indexOf(':') != -1 && 
!url.startsWith("mvn:")) ) {
             throw new IllegalArgumentException("Invalid mvn url: " + url);
         }
-        return fromMvnId(url.substring(4));
-    }
-
-    /**
-     * Create a new artifact from a maven id,
-     * group-id '/' artifact-id [ '/' [version] [ '/' [type] [ '/' classifier 
] ] ] ]
-     * @param coordinates The id
-     * @return A new artifact
-     * @throws IllegalArgumentException If the id is not valid
-     */
-    public static ArtifactId fromMvnId(final String coordinates) {
         // throw if repository url is included
-        if ( coordinates.indexOf('!') != -1 ) {
+        if ( url.indexOf('!') != -1 ) {
             throw new IllegalArgumentException("Repository url is not 
supported for Maven artifacts at the moment.");
         }
+        final String coordinates = url.startsWith("mvn:") ? url.substring(4) : 
url;
         String gId = null;
         String aId = null;
         String version = null;
@@ -146,12 +152,47 @@ public class ArtifactId implements Compa
     }
 
     /**
+     * Create a new artifact id from maven coordinates/id
+     * groupId:artifactId[:packaging[:classifier]]:version
+     * @param coordinates The coordinates as outlined above
+     * @return A new artifact id
+     * @throws IllegalArgumentException If the id is not valid
+     */
+    public static ArtifactId fromMvnId(final String coordinates) {
+        final String[] parts = coordinates.split(":");
+        if ( parts.length < 3 || parts.length > 5) {
+            throw new IllegalArgumentException("Invalid mvn coordinates: " + 
coordinates);
+        }
+        final String gId = parts[0];
+        final String aId = parts[1];
+        final String version = parts[parts.length - 1];
+        final String type = parts.length > 3 ? parts[2] : null;
+        final String classifier = parts.length > 4 ? parts[3] : null;
+
+        return new ArtifactId(gId, aId, version, classifier, type);
+    }
+
+    /**
      * Return a mvn url
      * @return A mvn url
      * @see #fromMvnUrl(String)
      */
     public String toMvnUrl() {
-        return toId(new StringBuilder("mvn:"));
+        final StringBuilder sb = new StringBuilder("mvn:");
+        sb.append(this.groupId);
+        sb.append('/');
+        sb.append(this.artifactId);
+        sb.append('/');
+        sb.append(version);
+        if ( this.classifier != null || !"jar".equals(this.type)) {
+            sb.append('/');
+            sb.append(this.type);
+            if ( this.classifier != null ) {
+                sb.append('/');
+                sb.append(this.classifier);
+            }
+        }
+        return sb.toString();
     }
 
     /**
@@ -160,25 +201,17 @@ public class ArtifactId implements Compa
      * #see {@link #fromMvnId(String)}
      */
     public String toMvnId() {
-        return toId(new StringBuilder());
-    }
-
-    /**
-     * Internal method to create the mvn url/id
-     * @param sb A string builder
-     * @return The resulting id
-     */
-    private String toId(final StringBuilder sb) {
+        final StringBuilder sb = new StringBuilder();
         sb.append(this.groupId);
-        sb.append('/');
+        sb.append(':');
         sb.append(this.artifactId);
-        sb.append('/');
+        sb.append(':');
         sb.append(version);
         if ( this.classifier != null || !"jar".equals(this.type)) {
-            sb.append('/');
+            sb.append(':');
             sb.append(this.type);
             if ( this.classifier != null ) {
-                sb.append('/');
+                sb.append(':');
                 sb.append(this.classifier);
             }
         }

Modified: 
sling/whiteboard/cziegeler/feature/src/test/java/org/apache/sling/feature/ArtifactIdTest.java
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature/src/test/java/org/apache/sling/feature/ArtifactIdTest.java?rev=1810118&r1=1810117&r2=1810118&view=diff
==============================================================================
--- 
sling/whiteboard/cziegeler/feature/src/test/java/org/apache/sling/feature/ArtifactIdTest.java
 (original)
+++ 
sling/whiteboard/cziegeler/feature/src/test/java/org/apache/sling/feature/ArtifactIdTest.java
 Fri Sep 29 15:02:10 2017
@@ -17,6 +17,7 @@
 package org.apache.sling.feature;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
@@ -113,4 +114,31 @@ public class ArtifactIdTest {
         assertEquals(3, v.getMicro());
         assertEquals("20170712_062549-4", v.getQualifier());
     }
+
+    @Test public void testCoordinatesGAV() {
+        final ArtifactId id = ArtifactId.fromMvnId("group.a:artifact.b:1.0");
+        assertEquals("group.a", id.getGroupId());
+        assertEquals("artifact.b", id.getArtifactId());
+        assertEquals("1.0", id.getVersion());
+        assertEquals("jar", id.getType());
+        assertNull(id.getClassifier());
+    }
+
+    @Test public void testCoordinatesGAVP() {
+        final ArtifactId id = 
ArtifactId.fromMvnId("group.a:artifact.b:zip:1.0");
+        assertEquals("group.a", id.getGroupId());
+        assertEquals("artifact.b", id.getArtifactId());
+        assertEquals("1.0", id.getVersion());
+        assertEquals("zip", id.getType());
+        assertNull(id.getClassifier());
+    }
+
+    @Test public void testCoordinatesGAVPC() {
+        final ArtifactId id = 
ArtifactId.fromMvnId("group.a:artifact.b:zip:foo:1.0");
+        assertEquals("group.a", id.getGroupId());
+        assertEquals("artifact.b", id.getArtifactId());
+        assertEquals("1.0", id.getVersion());
+        assertEquals("zip", id.getType());
+        assertEquals("foo", id.getClassifier());
+    }
 }


Reply via email to