You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by pd...@apache.org on 2013/01/22 18:58:36 UTC

svn commit: r1437070 - in /felix/trunk/dependencymanager/annotation: doc/changelog.txt src/main/java/org/apache/felix/dm/annotation/plugin/bnd/AnnotationPlugin.java src/main/java/org/apache/felix/dm/annotation/plugin/mvn/AnnotationMojo.java

Author: pderop
Date: Tue Jan 22 17:58:35 2013
New Revision: 1437070

URL: http://svn.apache.org/viewvc?rev=1437070&view=rev
Log:
[FELIX-3863] - Generate DependencyManager Annotation MetaData In Project Folder.

Modified:
    felix/trunk/dependencymanager/annotation/doc/changelog.txt
    felix/trunk/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/AnnotationPlugin.java
    felix/trunk/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/mvn/AnnotationMojo.java

Modified: felix/trunk/dependencymanager/annotation/doc/changelog.txt
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/annotation/doc/changelog.txt?rev=1437070&r1=1437069&r2=1437070&view=diff
==============================================================================
--- felix/trunk/dependencymanager/annotation/doc/changelog.txt (original)
+++ felix/trunk/dependencymanager/annotation/doc/changelog.txt Tue Jan 22 17:58:35 2013
@@ -10,6 +10,7 @@ Changes from 3.0.0 to 3.0.1
     * [FELIX-2956] - json should be embedded in the annotation scanner plugin
     * [FELIX-2966] - Annotations should automatically generate Import-Service/Export-Service headers
     * [FELIX-2965] - Annotations should allow to enable or disable auto-configuration mode.
+    * [FELIX-3863] - Generate DependencyManager Annotation MetaData In Project Folder
 
 Initial Release 3.0.0
 ---------------------

Modified: felix/trunk/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/AnnotationPlugin.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/AnnotationPlugin.java?rev=1437070&r1=1437069&r2=1437070&view=diff
==============================================================================
--- felix/trunk/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/AnnotationPlugin.java (original)
+++ felix/trunk/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/AnnotationPlugin.java Tue Jan 22 17:58:35 2013
@@ -48,11 +48,11 @@ public class AnnotationPlugin implements
     /**
      * This plugin is called after analysis of the JAR but before manifest
      * generation. When some DM annotations are found, the plugin will add the corresponding 
-     * DM component descriptors under OSGI-INF/ directory. It will also set the  
+     * DM component descriptors under META-INF/ directory. It will also set the  
      * "DependencyManager-Component" manifest header (which references the descriptor paths).
      * 
      * @param analyzer the object that is used to retrieve classes containing DM annotations.
-     * @return true if the classpace has been modified so that the bundle classpath must be reanalyzed
+     * @return true if the classpath has been modified so that the bundle classpath must be reanalyzed
      * @throws Exception on any errors.
      */
     public boolean analyzeJar(Analyzer analyzer) throws Exception
@@ -90,7 +90,7 @@ public class AnnotationPlugin implements
                     analyzer.getJar().putResource(entry.getKey(), entry.getValue());
                 }
 
-                // Inser the metatype resource, if any.
+                // Insert the metatype resource, if any.
                 Resource metaType = generator.getMetaTypeResource();
                 if (metaType != null)
                 {

Modified: felix/trunk/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/mvn/AnnotationMojo.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/mvn/AnnotationMojo.java?rev=1437070&r1=1437069&r2=1437070&view=diff
==============================================================================
--- felix/trunk/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/mvn/AnnotationMojo.java (original)
+++ felix/trunk/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/mvn/AnnotationMojo.java Tue Jan 22 17:58:35 2013
@@ -18,8 +18,12 @@
  */
 package org.apache.felix.dm.annotation.plugin.mvn;
 
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Map;
 import java.util.Set;
 
@@ -36,7 +40,7 @@ import aQute.lib.osgi.Resource;
 /**
  * The <code>AnnotationMojo</code>
  * generates a Dependency Manager component descriptor file based on annotations found from java classes.
- *
+ * 
  * @goal scan
  * @phase package
  * @description Build DependencyManager component descriptors from class annotations.
@@ -47,7 +51,7 @@ public class AnnotationMojo extends Abst
 {
     /**
      * The Maven project.
-     *
+     * 
      * @parameter expression="${project}"
      * @required
      * @readonly
@@ -56,7 +60,7 @@ public class AnnotationMojo extends Abst
 
     /**
      * The target extension
-     *
+     * 
      * @parameter default-value="jar"
      * @required
      */
@@ -64,19 +68,35 @@ public class AnnotationMojo extends Abst
 
     /**
      * If set, configures the log level.
-     *
+     * 
      * @parameter alias="log"
      */
     private String m_log;
 
     /**
      * If set, configures if we must auto generate Import-Service/Export-Service headers.
-     *
+     * 
      * @parameter alias="build-import-export-service" default-value="true"
      */
     private boolean m_buildImportExportService;
 
     /**
+     * The maven project bas directory, used when generating metadata in maven project directory.
+     * 
+     * @parameter expression="${project.basedir}"
+     * @required
+     * @readonly
+     */
+    private File m_projectBaseDir;
+    
+    /**
+     * If set, configures the output directory where generated descriptor files are generated.
+     * 
+     * @parameter alias="generated-output-dir"
+     */
+    private String m_generatedOutputDir;
+
+    /**
      * "Import-Service" osgi header
      */
     private static final String IMPORT_SERVICE = "Import-Service";
@@ -87,7 +107,8 @@ public class AnnotationMojo extends Abst
     private static final String EXPORT_SERVICE = "Export-Service";
 
     /**
-     * Executes this mojo. We'll use the bnd library in order to scan classes from our target bundle.
+     * Executes this mojo. We'll use the bnd library in order to scan classes
+     * from our target bundle.
      */
     public void execute() throws MojoExecutionException
     {
@@ -111,29 +132,34 @@ public class AnnotationMojo extends Abst
             // Start scanning
             if (generator.execute())
             {
-                // Some annotations have been parsed. 
-                // Add the list of generated component descriptors in our special header.
+                // Some annotations have been parsed.
+                // Add the list of generated component descriptors in our
+                // special header.
                 jar = analyzer.getJar();
-                jar.getManifest().getMainAttributes().putValue("DependencyManager-Component",
-                                                               generator.getDescriptorPaths());
+                jar.getManifest().getMainAttributes()
+                    .putValue( "DependencyManager-Component", generator.getDescriptorPaths() );
 
-                // Add generated descriptors into the target bundle (we'll use a temp file).
+                // Add generated descriptors into the target bundle (we'll use a
+                // temp file).
                 Map<String, Resource> resources = generator.getDescriptors();
                 for (Map.Entry<String, Resource> entry : resources.entrySet())
                 {
+                    addResource(entry.getKey(), entry.getValue().openInputStream());
                     jar.putResource(entry.getKey(), entry.getValue());
                 }
 
                 Resource metaType = generator.getMetaTypeResource();
                 if (metaType != null)
                 {
+                    addResource("OSGI-INF/metatype/metatype.xml", metaType.openInputStream());
                     jar.putResource("OSGI-INF/metatype/metatype.xml", metaType);
                 }
 
                 // Possibly set the Import-Service/Export-Service header
                 if (m_buildImportExportService)
                 {
-                    // Don't override Import-Service header, if it is found from the bnd directives.
+                    // Don't override Import-Service header, if it is found from
+                    // the bnd directives.
                     if (jar.getManifest().getMainAttributes().getValue(IMPORT_SERVICE) == null)
                     {
                         buildImportExportService(jar, IMPORT_SERVICE, generator.getImportService());
@@ -170,6 +196,29 @@ public class AnnotationMojo extends Abst
         }
     }
 
+    /**
+     * Adds a resource file into the project base directory
+     * @param key
+     * @param in
+     * @throws IOException
+     */
+    private void addResource(String key, InputStream in) throws IOException
+    {
+        if (m_generatedOutputDir != null) {
+            File descriptorFile = new File( m_projectBaseDir + File.separator + m_generatedOutputDir, key );
+            descriptorFile.getParentFile().mkdirs();
+            BufferedInputStream bin = new BufferedInputStream( in );
+            BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream( descriptorFile ) );
+            int b;
+            while ( ( b = bin.read() ) != -1 )
+            {
+                out.write( b );
+            }
+            out.close();
+            bin.close();
+        }
+    }
+
     private void buildImportExportService(Jar jar, String header, Set<String> services) throws IOException
     {
         getLog().info("building " + header + " header with the following services: " + services);
@@ -189,6 +238,7 @@ public class AnnotationMojo extends Abst
 
     /**
      * Returns the target name of this maven project.
+     * 
      * @return the target name of this maven project.
      */
     private File getBundleName()
@@ -200,6 +250,7 @@ public class AnnotationMojo extends Abst
 
     /**
      * Copy the generated jar into our target bundle.
+     * 
      * @param jar the jar with the generated component descriptors
      * @param target our target bundle
      * @throws MojoExecutionException on any errors