FELIX-505: stop bundleall from repacking existing bundles

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@632925 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundleAllPlugin.java b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundleAllPlugin.java
index a6b080f..aee46b3 100644
--- a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundleAllPlugin.java
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundleAllPlugin.java
@@ -51,6 +51,7 @@
 import org.apache.maven.shared.dependency.tree.DependencyNode;
 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
+import org.codehaus.plexus.util.FileUtils;
 
 import aQute.lib.osgi.Analyzer;
 import aQute.lib.osgi.Jar;
@@ -130,6 +131,13 @@
 
     private Set m_artifactsBeingProcessed = new HashSet();
 
+    /**
+     * Process up to some depth 
+     * 
+     * @parameter
+     */
+    private int depth = Integer.MAX_VALUE;
+
 
     public void execute() throws MojoExecutionException
     {
@@ -146,7 +154,7 @@
      */
     private BundleInfo bundleAll( MavenProject project ) throws MojoExecutionException
     {
-        return bundleAll( project, Integer.MAX_VALUE );
+        return bundleAll( project, depth );
     }
 
 
@@ -154,10 +162,10 @@
      * Bundle a project and its transitive dependencies up to some depth level
      * 
      * @param project
-     * @param depth how deep to process the dependency tree
+     * @param maxDepth how deep to process the dependency tree
      * @throws MojoExecutionException
      */
-    protected BundleInfo bundleAll( MavenProject project, int depth ) throws MojoExecutionException
+    protected BundleInfo bundleAll( MavenProject project, int maxDepth ) throws MojoExecutionException
     {
 
         if ( alreadyBundled( project.getArtifact() ) )
@@ -233,11 +241,11 @@
             node.getArtifact().setFile( artifact.getFile() );
 
             int nodeDepth = node.getDepth();
-            if ( nodeDepth > depth )
+            if ( nodeDepth > maxDepth )
             {
                 /* node is deeper than we want */
-                getLog()
-                    .debug( "Ignoring " + node.getArtifact() + ", depth is " + nodeDepth + ", bigger than " + depth );
+                getLog().debug(
+                    "Ignoring " + node.getArtifact() + ", depth is " + nodeDepth + ", bigger than " + maxDepth );
                 continue;
             }
 
@@ -266,7 +274,7 @@
             if ( ( Artifact.SCOPE_COMPILE.equals( artifact.getScope() ) )
                 || ( Artifact.SCOPE_RUNTIME.equals( artifact.getScope() ) ) )
             {
-                BundleInfo subBundleInfo = bundleAll( childProject, depth - 1 );
+                BundleInfo subBundleInfo = bundleAll( childProject, maxDepth - 1 );
                 if ( subBundleInfo != null )
                 {
                     bundleInfo.merge( subBundleInfo );
@@ -343,6 +351,8 @@
 
             Jar osgiJar = new Jar( project.getArtifactId(), project.getArtifact().getFile() );
 
+            outputFile.getAbsoluteFile().getParentFile().mkdirs();
+
             Collection exportedPackages;
             if ( isOsgi( osgiJar ) )
             {
@@ -352,18 +362,17 @@
                         + project.getVersion() );
                 String exportHeader = osgiJar.getManifest().getMainAttributes().getValue( Analyzer.EXPORT_PACKAGE );
                 exportedPackages = analyzer.parseHeader( exportHeader ).keySet();
+                FileUtils.copyFile( project.getArtifact().getFile(), outputFile );
             }
             else
             {
-                /* else generate the mainfest from the packages */
+                /* else generate the manifest from the packages */
                 exportedPackages = analyzer.getExports().keySet();
                 Manifest manifest = analyzer.getJar().getManifest();
                 osgiJar.setManifest( manifest );
+                osgiJar.write( outputFile );
             }
 
-            outputFile.getAbsoluteFile().getParentFile().mkdirs();
-            osgiJar.write( outputFile );
-
             BundleInfo bundleInfo = addExportedPackages( project, exportedPackages );
 
             return bundleInfo;
diff --git a/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java b/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java
index fa2c5f8..c88090f 100644
--- a/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java
+++ b/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java
@@ -29,6 +29,7 @@
 import org.apache.maven.shared.osgi.DefaultMaven2OsgiConverter;
 
 import aQute.lib.osgi.Analyzer;
+import aQute.lib.osgi.Builder;
 import aQute.lib.osgi.Jar;
 
 
@@ -49,6 +50,9 @@
         super.setUp();
         plugin = new BundlePlugin();
         plugin.setMaven2OsgiConverter( new DefaultMaven2OsgiConverter() );
+        plugin.setBasedir( new File( "." ) );
+        plugin.setBuildDirectory( "." );
+        plugin.setOutputDirectory( new File( "." ) );
     }
 
 
@@ -157,4 +161,11 @@
         assertEquals( "", transformedInstructions.get( "z" ) );
         assertEquals( "", transformedInstructions.get( "-z" ) );
     }
+
+
+    public void testVersion() throws Exception
+    {
+        String cleanupVersion = Builder.cleanupVersion( "0.0.0.4aug2000r7-dev" );
+        assertEquals( "0.0.0.4aug2000r7-dev", cleanupVersion );
+    }
 }