FELIX-247: add bundle:ant goal, which creates a custom Ant script to build the bundle (run ant:ant first)

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@629050 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/AntPlugin.java b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/AntPlugin.java
new file mode 100644
index 0000000..fb06511
--- /dev/null
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/AntPlugin.java
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.bundleplugin;
+
+
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.StringUtils;
+
+import aQute.lib.osgi.Builder;
+import aQute.lib.osgi.Jar;
+
+
+/**
+ * Generate Ant script to create the bundle (you should run ant:ant first).
+ *
+ * @goal ant
+ * @requiresDependencyResolution runtime
+ * @description generate Ant script to create the bundle
+ */
+public class AntPlugin extends BundlePlugin
+{
+    static final String BUILD_XML = "/build.xml";
+    static final String BUILD_BND = "/maven-build.bnd";
+
+
+    protected void execute( MavenProject currentProject, Map originalInstructions, Properties properties,
+        Jar[] classpath ) throws MojoExecutionException
+    {
+        final String artifactId = getProject().getArtifactId();
+        final String baseDir = getProject().getBasedir().getPath();
+
+        try
+        {
+            // assemble bundle as usual, but don't save it - this way we have all the instructions we need
+            Builder builder = buildOSGiBundle( currentProject, originalInstructions, properties, classpath );
+            Properties bndProperties = builder.getProperties();
+
+            // cleanup and remove all non-strings from the builder properties
+            for ( Iterator i = bndProperties.values().iterator(); i.hasNext(); )
+            {
+                if ( !( i.next() instanceof String ) )
+                {
+                    i.remove();
+                }
+            }
+
+            // save the BND generated bundle to the same output directory that maven uses
+            bndProperties.setProperty( "-output", "${maven.build.dir}/${maven.build.finalName}.jar" );
+
+            OutputStream out = new FileOutputStream( baseDir + BUILD_BND );
+            bndProperties.store( out, " Merged BND Instructions" );
+            IOUtil.close( out );
+
+            // modify build template
+            String buildXml = IOUtil.toString( getClass().getResourceAsStream( BUILD_XML ) );
+            buildXml = StringUtils.replace( buildXml, "ARTIFACT_ID", artifactId );
+
+            FileUtils.fileWrite( baseDir + BUILD_XML, buildXml );
+        }
+        catch ( Exception e )
+        {
+            throw new MojoExecutionException( "Problem creating Ant script", e );
+        }
+
+        getLog().info( "Wrote Ant bundle project for " + artifactId + " to " + baseDir );
+    }
+}
diff --git a/bundleplugin/src/main/resources/build.xml b/bundleplugin/src/main/resources/build.xml
index 24dce51..5cee4f1 100644
--- a/bundleplugin/src/main/resources/build.xml
+++ b/bundleplugin/src/main/resources/build.xml
@@ -20,7 +20,7 @@
     <echo message="Please run: $ant -projecthelp"/>
   </target>
 
-  <property name="bnd.version" value="BND_VERSION"/>
+  <property name="bnd.version" value="0.0.238"/>
 
   <target name="get-bnd"
           depends="test-offline"
@@ -42,7 +42,7 @@
 
   <target name="package" depends="compile,test,get-bnd" description="Package the bundle">
     <pathconvert property="bnd.classpath" refid="build.classpath" pathsep=","/>
-    <bnd files="maven-build.bnd" classpath="${bnd.classpath}"/>
+    <bnd files="maven-build.bnd" classpath="${maven.build.outputDir},${bnd.classpath}"/>
   </target>
 
 </project>
diff --git a/bundleplugin/src/main/resources/maven-build.bnd b/bundleplugin/src/main/resources/maven-build.bnd
deleted file mode 100644
index 49e9161..0000000
--- a/bundleplugin/src/main/resources/maven-build.bnd
+++ /dev/null
@@ -1,2 +0,0 @@
--output: ${maven.build.dir}/${maven.build.finalName}.jar
-