Merge pull request #1 from apache/feature/attach-only-optionally

SLING-7299 do not always attach built artifact
diff --git a/src/main/java/org/apache/sling/maven/slingstart/AbstractSlingStartMojo.java b/src/main/java/org/apache/sling/maven/slingstart/AbstractSlingStartMojo.java
index b877f47..a5bc9ab 100644
--- a/src/main/java/org/apache/sling/maven/slingstart/AbstractSlingStartMojo.java
+++ b/src/main/java/org/apache/sling/maven/slingstart/AbstractSlingStartMojo.java
@@ -94,7 +94,7 @@
     protected MavenSession mavenSession;
 
     /**
-     * If set to true, properties from the Maven POM can be used as variables in the provisioning files.
+     * If set to {@code true}, properties from the Maven POM can be used as variables in the provisioning files.
      * The resolved variables are added to the generated provisioning file, so other tools using this model
      * do not have to resolve them themselves.
      */
@@ -102,8 +102,14 @@
     protected boolean usePomVariables;
 
     /**
-     * If set to true, Artifact dependencies from provisioning file without explicit version are tried
-     * to be resolved against the dependency versions from the Maven POM.
+     * If set to {@code true}, Artifact dependencies from provisioning file without explicit version are tried
+     * to be resolved against the versions given in the Maven POM.
+     * The following sections in the effective pom are considered during resolving the version:
+     * <ol>
+     *   <li>The project's artifact itself</li>
+     *   <li>The project's dependencies</li>
+     *   <li>The project's dependencyManagement</li>
+     * </ol>
      */
     @Parameter(defaultValue="false")
     protected boolean usePomDependencies;
diff --git a/src/main/java/org/apache/sling/maven/slingstart/PomArtifactVersionResolver.java b/src/main/java/org/apache/sling/maven/slingstart/PomArtifactVersionResolver.java
index 8fa941f..f2dd228 100644
--- a/src/main/java/org/apache/sling/maven/slingstart/PomArtifactVersionResolver.java
+++ b/src/main/java/org/apache/sling/maven/slingstart/PomArtifactVersionResolver.java
@@ -29,6 +29,12 @@
 /**
  * Provisioning artifact resolver that tries to resolve artifacts in provisioning file without version (LATEST)
  * against the dependencies defined in the maven project.
+ * The following sections in the Maven project are considered during resolving the version:
+ * <ol>
+ *   <li>The project's artifact itself</li>
+ *   <li>The project's dependencies</li>
+ *   <li>The project's dependencyManagement</li>
+ * </ol>
  */
 public class PomArtifactVersionResolver implements ArtifactVersionResolver {
 
@@ -46,10 +52,15 @@
     
     @Override
     public String resolve(Artifact artifact) {
+        if (artifactEquals(project.getArtifact(), artifact)) {
+            return project.getVersion();
+        }
+        
         String version = findVersion(project.getDependencies(), artifact);
         if (version != null) {
             return version;
         }
+        
         if (project.getDependencyManagement() != null) {
             version = findVersion(project.getDependencyManagement().getDependencies(), artifact);
             if (version != null) {
@@ -75,11 +86,27 @@
         return null;
     }
     
-    private boolean artifactEquals(Dependency dependency, Artifact artifact) {
+    static boolean artifactEquals(Dependency dependency, Artifact artifact) {
         return StringUtils.equals(dependency.getGroupId(), artifact.getGroupId())
                 && StringUtils.equals(dependency.getArtifactId(), artifact.getArtifactId())
                 && StringUtils.equals(dependency.getClassifier(), artifact.getClassifier())
-                && StringUtils.equals(dependency.getType(), artifact.getType());
+                && StringUtils.equals(normalizeType(dependency.getType()), normalizeType(artifact.getType()));
     }
-
+    
+    static boolean artifactEquals(org.apache.maven.artifact.Artifact artifact, Artifact artifact2) {
+        return StringUtils.equals(artifact.getGroupId(), artifact2.getGroupId())
+                && StringUtils.equals(artifact.getArtifactId(), artifact2.getArtifactId())
+                && StringUtils.equals(artifact.getClassifier(), artifact2.getClassifier())
+                && StringUtils.equals(normalizeType(artifact.getType()), normalizeType(artifact2.getType()));
+    }
+    
+    static String normalizeType(String type) {
+        // bundles are often referred to with type "jar" in the provisioning model.
+        // especially when leaving out the version you cannot even specify a type different from the default "jar"
+        if (type.equals("bundle")) {
+            return "jar";
+        } else {
+            return type;
+        }
+    }
 }
diff --git a/src/test/java/org/apache/sling/maven/slingstart/PomArtifactVersionResolverTest.java b/src/test/java/org/apache/sling/maven/slingstart/PomArtifactVersionResolverTest.java
new file mode 100644
index 0000000..47f43a7
--- /dev/null
+++ b/src/test/java/org/apache/sling/maven/slingstart/PomArtifactVersionResolverTest.java
@@ -0,0 +1,74 @@
+/*
+ * 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.maven.slingstart;
+
+import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.artifact.handler.DefaultArtifactHandler;
+import org.apache.maven.model.Dependency;
+import org.apache.sling.provisioning.model.Artifact;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PomArtifactVersionResolverTest {
+
+    @Test
+    public void testNormalizeType() {
+        Assert.assertEquals("jar", PomArtifactVersionResolver.normalizeType("jar"));
+        Assert.assertEquals("jar", PomArtifactVersionResolver.normalizeType("bundle"));
+        Assert.assertEquals("bla", PomArtifactVersionResolver.normalizeType("bla"));
+    }
+
+    @Test
+    public void testArtifactEqualsForMavenArtifact() {
+        Assert.assertTrue(PomArtifactVersionResolver.artifactEquals(
+                new DefaultArtifact("somegroup", "someartifact", "someversion", "somescope", "sometype", "someclassifier", new DefaultArtifactHandler()),
+                new Artifact("somegroup", "someartifact", "someversion", "someclassifier", "sometype")));
+        // test maven artifact with type "bundle", other with type "jar"
+        Assert.assertTrue(PomArtifactVersionResolver.artifactEquals(
+                new DefaultArtifact("somegroup", "someartifact", "someversion", "somescope", "bundle", "someclassifier", new DefaultArtifactHandler()), 
+                new Artifact("somegroup", "someartifact", "someversion", "someclassifier", "jar")));
+        
+        // test without classifier
+        Assert.assertTrue(PomArtifactVersionResolver.artifactEquals(
+                new DefaultArtifact("somegroup", "someartifact", "someversion", "somescope", "bundle", null, new DefaultArtifactHandler()), 
+                new Artifact("somegroup", "someartifact", "LATEST", null, "jar")));
+    }
+    
+    @Test
+    public void testArtifactEqualsForMavenDependencies() {
+        Dependency dependency = new Dependency();
+        dependency.setGroupId("somegroup");
+        dependency.setArtifactId("someartifact");
+        dependency.setVersion("someversion");
+        dependency.setClassifier("someclassifier");
+        dependency.setType("sometype");
+        Assert.assertTrue(PomArtifactVersionResolver.artifactEquals(
+                dependency,
+                new Artifact("somegroup", "someartifact", "someversion", "someclassifier", "sometype")));
+        // test dependency with type "bundle", other with type "jar"
+        dependency.setType("bundle");
+        Assert.assertTrue(PomArtifactVersionResolver.artifactEquals(
+                dependency, 
+                new Artifact("somegroup", "someartifact", "someversion", "someclassifier", "jar")));
+        
+        // test without classifier
+        dependency.setClassifier(null);
+        Assert.assertTrue(PomArtifactVersionResolver.artifactEquals(
+                dependency, 
+                new Artifact("somegroup", "someartifact", "LATEST", null, "jar")));
+    }
+}