Generator: merge existing dependency information 03/52003/4
authorStephen Kitt <skitt@redhat.com>
Fri, 17 Feb 2017 13:29:19 +0000 (14:29 +0100)
committerStephen Kitt <skitt@redhat.com>
Mon, 20 Feb 2017 16:47:32 +0000 (17:47 +0100)
This patch allows the generator to merge dependency information from
feature.xml stubs and POMs ("dependency" and "prerequisite" flags).
This allows version information to be pulled from the POMs and still
allow the flags to be set.

Bug: 7752
Bug: 7753
Change-Id: I2b5147dc1636b4c9a3ec3b77654c9994702c7877
Signed-off-by: Stephen Kitt <skitt@redhat.com>
karaf/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java

index 55355a4522d5953be709fbd98bb587566f297d8e..2328d25a3facd921a2edbfc4c411f1de679e7034 100644 (file)
@@ -537,7 +537,14 @@ public class GenerateDescriptorMojo extends MojoSupport {
             }
             for (Feature includedFeature : includedFeatures.getFeature()) {
                 Dependency dependency = new Dependency(includedFeature.getName(), includedFeature.getVersion());
-                // We musn't de-duplicate here, we may have seen a feature in !add mode
+                // Determine what dependency we're actually going to use
+                Dependency matchingDependency = findMatchingDependency(feature.getFeature(), dependency);
+                if (matchingDependency != null) {
+                    // The feature already has a matching dependency, merge them
+                    mergeDependencies(matchingDependency, dependency);
+                    dependency = matchingDependency;
+                }
+                // We mustn't de-duplicate here, we may have seen a feature in !add mode
                 otherFeatures.put(dependency, includedFeature);
                 if (add) {
                     if (!feature.getFeature().contains(dependency)) {
@@ -555,6 +562,28 @@ public class GenerateDescriptorMojo extends MojoSupport {
         }
     }
 
+    private Dependency findMatchingDependency(List<Dependency> dependencies, Dependency reference) {
+        String referenceName = reference.getName();
+        for (Dependency dependency : dependencies) {
+            if (referenceName.equals(dependency.getName())) {
+                return dependency;
+            }
+        }
+        return null;
+    }
+
+    private void mergeDependencies(Dependency target, Dependency source) {
+        if (target.getVersion() == null || Feature.DEFAULT_VERSION.equals(target.getVersion())) {
+            target.setVersion(source.getVersion());
+        }
+        if (source.isDependency()) {
+            target.setDependency(true);
+        }
+        if (source.isPrerequisite()) {
+            target.setPrerequisite(true);
+        }
+    }
+
     private boolean isBundleIncludedTransitively(Feature feature, Map<Dependency, Feature> otherFeatures,
                                                  Bundle bundle) {
         for (Dependency dependency : feature.getFeature()) {