You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2012/10/23 07:19:52 UTC

svn commit: r1401164 - /felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java

Author: cziegeler
Date: Tue Oct 23 05:19:52 2012
New Revision: 1401164

URL: http://svn.apache.org/viewvc?rev=1401164&view=rev
Log:
FELIX-3643 :  Use BuildContext for scanning changed files. Apply another patch from Robert Munteanu

Modified:
    felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java

Modified: felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java
URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java?rev=1401164&r1=1401163&r2=1401164&view=diff
==============================================================================
--- felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java (original)
+++ felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java Tue Oct 23 05:19:52 2012
@@ -232,13 +232,20 @@ public class SCRDescriptorMojo extends A
             generator.setOptions(options);
             generator.setProject(project);
 
+            // don't try to delete per-class descriptors if only one descriptor is generated
+            if ( options.isGenerateSeparateDescriptors() ) {
+                this.removePossiblyStaleFiles(scanner.getSources(), options);
+            }
+
             final Result result = generator.execute();
             this.setServiceComponentHeader(result.getScrFiles());
 
             this.updateProjectResources();
             // don't try to delete per-class descriptors if only one descriptor is generated
-            if ( options.isGenerateSeparateDescriptors() )
+            if ( options.isGenerateSeparateDescriptors() ) {
                 this.cleanUpDeletedSources(scanner.getDeletedSources(), options);
+            }
+
             this.refreshMessages(result.getProcessedSourceFiles());
             this.updateBuildContext(result);
 
@@ -251,32 +258,54 @@ public class SCRDescriptorMojo extends A
     }
 
     /**
+     * Remove existing files for the sources which have recently changed
+     * 
+     * <p>This method ensures that files which were generated in a previous run are not
+     * leftover if the source file has changed by:
+     * <ol>
+     *  <li>No longer having a <tt>@Component</tt>annotation</li>
+     *  <li>No longer having the <tt>metatype</tt> property set to true</li>
+     * </ol>
+     * </p>
+     * 
+     * @param sources the changed source files
+     */
+    private void removePossiblyStaleFiles(final Collection<Source> sources, final Options options) {
+        deleteOutputFilesForSources(sources, options);
+    }
+
+    /**
      * @param scrFiles
      */
     private void refreshMessages(List<String> scrFiles) {
-
-        for ( String scrFile : scrFiles )
+        for ( final String scrFile : scrFiles ) {
             buildContext.removeMessages(new File(scrFile));
+        }
     }
 
     private void cleanUpDeletedSources(final Collection<Source> deletedSources, final Options options) {
+        deleteOutputFilesForSources(deletedSources, options);
+    }
 
+    private void deleteOutputFilesForSources(final Collection<Source> sources, final Options options) {
         final File componentDir = options.getComponentDescriptorDirectory();
         final File mtDir = options.getMetaTypeDirectory();
 
-        for ( Source deletedSource : deletedSources ) {
+        for ( final Source source : sources ) {
 
-            File metaTypeFile = new File(mtDir, deletedSource.getClassName() + ".xml");
+            final File metaTypeFile = new File(mtDir, source.getClassName() + ".xml");
             getLog().debug("Deleting " + metaTypeFile + " ");
             boolean deleted = metaTypeFile.delete();
-            if ( deleted )
+            if ( deleted ) {
                 buildContext.refresh(metaTypeFile);
+            }
 
-            File componentDescriptorFile = new File(componentDir, deletedSource.getClassName() + ".xml");
+            final File componentDescriptorFile = new File(componentDir, source.getClassName() + ".xml");
             getLog().debug("Deleting " + componentDescriptorFile);
             deleted = componentDescriptorFile.delete();
-            if ( deleted )
+            if ( deleted ) {
                 buildContext.refresh(componentDescriptorFile);
+            }
         }
     }
 
@@ -284,14 +313,14 @@ public class SCRDescriptorMojo extends A
 
         if ( result.getMetatypeFiles() != null ) {
             for(final String name : result.getMetatypeFiles() ) {
-                File metaTypeFile = new File(this.outputDirectory, name.replace('/', File.separatorChar));
+                final File metaTypeFile = new File(this.outputDirectory, name.replace('/', File.separatorChar));
                 getLog().debug("Refreshing " + metaTypeFile);
                 this.buildContext.refresh(metaTypeFile);
             }
         }
         if ( result.getScrFiles() != null ) {
             for(final String name : result.getScrFiles() ) {
-                File scrFile = new File(this.outputDirectory, name.replace('/', File.separatorChar));
+                final File scrFile = new File(this.outputDirectory, name.replace('/', File.separatorChar));
                 getLog().debug("Refreshing " + scrFile);
                 this.buildContext.refresh(scrFile);
             }
@@ -400,8 +429,7 @@ public class SCRDescriptorMojo extends A
         final String ourRsrcPath = this.outputDirectory.getAbsolutePath();
         boolean found = false;
         @SuppressWarnings("unchecked")
-        final Iterator<Resource> rsrcIterator = this.project.getResources()
-        .iterator();
+        final Iterator<Resource> rsrcIterator = this.project.getResources().iterator();
         while (!found && rsrcIterator.hasNext()) {
             final Resource rsrc = rsrcIterator.next();
             found = rsrc.getDirectory().equals(ourRsrcPath);