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

simonetripodi pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-slingfeature-maven-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 1fff9cd  SLING-8347 Enhance the ApisJarMojo to infer the classifier of 
wrapper artifacts
     new 50ab89f  Merge pull request #29 from bosschaert/SLING-8347
1fff9cd is described below

commit 1fff9cdd64da649470a5bb12bbb1323ed634ad70
Author: David Bosschaert <[email protected]>
AuthorDate: Tue Apr 9 14:45:04 2019 +0100

    SLING-8347 Enhance the ApisJarMojo to infer the classifier of wrapper 
artifacts
---
 .../sling/feature/maven/mojos/ApisJarMojo.java     | 36 +++++++++++++++++--
 .../sling/feature/maven/mojos/ApisJarMojoTest.java | 40 ++++++++++++++++++++++
 2 files changed, 73 insertions(+), 3 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/feature/maven/mojos/ApisJarMojo.java 
b/src/main/java/org/apache/sling/feature/maven/mojos/ApisJarMojo.java
index 01c357b..a83997b 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/ApisJarMojo.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/ApisJarMojo.java
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.io.StringReader;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Collection;
 import java.util.Enumeration;
@@ -92,8 +93,6 @@ import org.codehaus.plexus.archiver.manager.ArchiverManager;
 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
 import org.osgi.framework.Constants;
 
-import edu.emory.mathcs.backport.java.util.Arrays;
-
 /**
  * Generates the APIs JARs for each selected Feature file.
  */
@@ -355,12 +354,43 @@ public class ApisJarMojo extends 
AbstractIncludingFeatureMojo implements Artifac
             String groupId = properties.getProperty("groupId");
             String artifactId = properties.getProperty("artifactId");
             String version = properties.getProperty("version");
+            String classifier = properties.getProperty("classifier");
+            if (classifier == null) {
+                classifier = inferClassifier(bundleName, artifactId, version);
+            }
 
-            Artifact syntheticArtifact = new Artifact(new ArtifactId(groupId, 
artifactId, version, null, null));
+            Artifact syntheticArtifact = new Artifact(new ArtifactId(groupId, 
artifactId, version, classifier, null));
             onArtifact(syntheticArtifact, apiRegions, javadocClasspath, 
deflatedBinDir, deflatedSourcesDir, checkedOutSourcesDir);
         }
     }
 
+    // Guess the classifier based on the file name
+    String inferClassifier(String bundleName, String artifactId, String 
version) {
+        if (bundleName == null || artifactId == null || version == null)
+            return null;
+
+        int idx = bundleName.lastIndexOf('/');
+        if (idx >= 0)
+            bundleName = bundleName.substring(idx + 1);
+
+        int edx = bundleName.lastIndexOf('.');
+        if (edx > 0)
+            bundleName = bundleName.substring(0, edx);
+
+        // bundleName is now the bare name without extension
+        String synthesized = artifactId + "-" + version;
+        if (synthesized.length() < bundleName.length() &&
+                bundleName.startsWith(synthesized)) {
+            String suffix = bundleName.substring(synthesized.length());
+            if (suffix.length() > 1 && suffix.startsWith("-")) {
+                String classifier = suffix.substring(1);
+                getLog().info("Inferred classifier of '" + artifactId + ":" + 
version + "' to be '" + classifier + "'");
+                return classifier;
+            }
+        }
+        return null;
+    }
+
     private void buildJavadocClasspath(Set<String> javadocClasspath, 
ArtifactId artifactId)
             throws MojoExecutionException {
         getLog().debug("Retrieving " + artifactId + " and related 
dependencies...");
diff --git 
a/src/test/java/org/apache/sling/feature/maven/mojos/ApisJarMojoTest.java 
b/src/test/java/org/apache/sling/feature/maven/mojos/ApisJarMojoTest.java
new file mode 100644
index 0000000..b05bfaa
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/maven/mojos/ApisJarMojoTest.java
@@ -0,0 +1,40 @@
+/*
+ * 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.sling.feature.maven.mojos;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+public class ApisJarMojoTest {
+    @Test
+    public void testGuessClassifier() {
+        ApisJarMojo ajm = new ApisJarMojo();
+
+        assertNull(ajm.inferClassifier(null, null, null));
+        assertNull(ajm.inferClassifier("foo-123.jar", "foo", "123"));
+        assertNull(ajm.inferClassifier("foo-1234.jar", "foo", "123"));
+        assertNull(ajm.inferClassifier("foo-12345.jar", "foo", "123"));
+        assertEquals("abc", ajm.inferClassifier("foo-123-abc", "foo", "123"));
+        assertNull(ajm.inferClassifier("blah/blah/foo-123.jar", "foo", "123"));
+        assertEquals("bar", ajm.inferClassifier("foo-123-bar.jar", "foo", 
"123"));
+        assertEquals("bar", ajm.inferClassifier("blah/blah/foo-123-bar.jar", 
"foo", "123"));
+        assertNull(ajm.inferClassifier("oof-123-bar.jar", "foo", "123"));
+        assertNull(ajm.inferClassifier("foo-345-bar.jar", "foo", "123"));
+    }
+}

Reply via email to