Author: gertv
Date: Fri Jul  4 07:04:32 2008
New Revision: 674060

URL: http://svn.apache.org/viewvc?rev=674060&view=rev
Log:
SMX4-49: Add a Camel features.xml

Modified:
    
servicemix/smx4/kernel/trunk/testing/features-maven-plugin/src/main/java/org/apache/servicemix/tooling/features/GenerateFeaturesFileMojo.java

Modified: 
servicemix/smx4/kernel/trunk/testing/features-maven-plugin/src/main/java/org/apache/servicemix/tooling/features/GenerateFeaturesFileMojo.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/testing/features-maven-plugin/src/main/java/org/apache/servicemix/tooling/features/GenerateFeaturesFileMojo.java?rev=674060&r1=674059&r2=674060&view=diff
==============================================================================
--- 
servicemix/smx4/kernel/trunk/testing/features-maven-plugin/src/main/java/org/apache/servicemix/tooling/features/GenerateFeaturesFileMojo.java
 (original)
+++ 
servicemix/smx4/kernel/trunk/testing/features-maven-plugin/src/main/java/org/apache/servicemix/tooling/features/GenerateFeaturesFileMojo.java
 Fri Jul  4 07:04:32 2008
@@ -22,8 +22,15 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
 
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
+import org.apache.maven.artifact.metadata.ResolutionGroup;
 import org.apache.maven.model.Dependency;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
@@ -41,18 +48,56 @@
  */
 public class GenerateFeaturesFileMojo extends MojoSupport {
     protected static final String SEPARATOR = "/";
+    
     /**
      * The file to generate
      *
      * @parameter 
default-value="${project.build.directory}/classes/feature.xml"
      */
     private File outputFile;
+    
     /**
      * The name of the feature, which defaults to the artifact ID if its not 
specified
      *
      * @parameter default-value="${project.artifactId}"
      */
     private String featureName;
+    
+    /**
+     * The artifact type for attaching the generated file to the project
+     * 
+     * @parameter default-value="features.xml"
+     */
+    private String attachmentArtifactType = "features.xml";
+    
+    /**
+     * The artifact classifier for attaching the generated file to the project
+     * 
+     * @parameter
+     */
+    private String attachmentArtifactClassifier;
+    
+    /**
+     * Should we generate a <feature> for the current project?
+     * 
+     * @parameter default-value="true"
+     */
+    private boolean includeProject = true;
+    
+    /**
+     * Should we generate a <feature> for the current project's <dependency>s?
+     * 
+     * @parameter default-value="false"
+     */
+    private boolean includeDependencies = false;
+    
+    private Set<Artifact> features = new HashSet<Artifact>();
+        
+    private static final List<String> bundles = new ArrayList<String>();
+    static {
+        bundles.add("org.apache.camel");
+    }
+    
 
     public void execute() throws MojoExecutionException, MojoFailureException {
         OutputStream out = null;
@@ -64,7 +109,7 @@
             getLog().info("Created: " + outputFile);
 
             // now lets attach it
-            projectHelper.attachArtifact(project, "features.xml", null, 
outputFile);
+            projectHelper.attachArtifact(project, attachmentArtifactType, 
attachmentArtifactClassifier, outputFile);
         }
         catch (Exception e) {
             throw new MojoExecutionException(
@@ -85,6 +130,72 @@
     protected void populateProperties(PrintStream out) {
         out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
         out.println("<features>");
+        if (includeProject) {
+            writeCurrentProjectFeature(out);
+        }
+        if (includeDependencies) {
+            writeProjectDependencyFeatures(out);
+        }
+        out.println("</features>");
+    }
+
+    @SuppressWarnings("unchecked")
+    private void writeProjectDependencyFeatures(PrintStream out) {
+        for (Artifact artifact : (Set<Artifact>) 
project.getDependencyArtifacts()) {
+            System.out.println("Adding feature " + artifact.getArtifactId() + 
" from " + artifact);
+            out.println("  <feature name='" + artifact.getArtifactId() + "'>");
+            writeBundle(out, artifact);
+            features.add(artifact);
+            out.println("  </feature>");
+        }
+        
+    }
+
+    private void writeBundle(PrintStream out, Artifact artifact) {
+        if (features.contains(artifact)) {
+            //if we already created a feature for this one, just add that 
instead of the bundle
+            out.println(String.format("    <feature>%s</feature>", 
artifact.getArtifactId()));
+            return;
+        }
+        //first write the dependencies
+        for (Artifact dependency : getDependencies(artifact)) {
+            writeBundle(out, dependency);
+        }
+        //and then write the bundle itself
+        if (isBundle(artifact)) {
+            writeBundle(out, artifact.getGroupId(), artifact.getArtifactId(), 
artifact.getBaseVersion());
+        } else {
+            writeServicemixWrapperBundle(out, artifact.getGroupId(), 
artifact.getArtifactId(), artifact.getBaseVersion());
+        }
+    }
+
+    private boolean isBundle(Artifact artifact) {
+        return artifact.getArtifactHandler().getPackaging().equals("bundle") 
|| bundles.contains(artifact.getGroupId());
+    }
+
+    @SuppressWarnings("unchecked")
+    private List<Artifact> getDependencies(Artifact artifact) {
+        List<Artifact> list = new ArrayList<Artifact>();
+        try {
+            ResolutionGroup pom = artifactMetadataSource.retrieve(artifact, 
localRepo, remoteRepos);
+            list.addAll(pom.getArtifacts());
+        } catch (ArtifactMetadataRetrievalException e) {
+            getLog().warn("Unable to retrieve metadata for " + artifact + ", 
not including dependencies for it");
+        }
+        return list;
+    }
+
+    private void writeServicemixWrapperBundle(PrintStream out, String groupId, 
String artifactId,
+                                              String version) {
+        writeBundle(out, 
+                    "org.apache.servicemix.bundles", 
+                    "org.apache.servicemix.bundles." + artifactId, 
+                    version + 
project.getProperties().getProperty("servicemix.bundle.suffix"));
+        
+    }
+
+
+    private void writeCurrentProjectFeature(PrintStream out) {
         out.println("  <feature name='" + featureName + "'>");
 
 
@@ -102,7 +213,6 @@
         }
 
         out.println("  </feature>");
-        out.println("</features>");
     }
 
     protected boolean isValidDependency(Dependency dependency) {
@@ -111,7 +221,7 @@
     }
 
     protected void writeBundle(PrintStream out, String groupId, String 
artifactId, String version) {
-        out.print("  <bundle>mvn:");
+        out.print("    <bundle>mvn:");
         out.print(groupId);
         out.print("/");
         out.print(artifactId);


Reply via email to