1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.felix.bundleplugin;
20  
21  
22  import java.io.FileOutputStream;
23  import java.io.OutputStream;
24  import java.util.Iterator;
25  import java.util.Map;
26  import java.util.Properties;
27  
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.project.MavenProject;
30  import org.codehaus.plexus.util.FileUtils;
31  import org.codehaus.plexus.util.IOUtil;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  import aQute.lib.osgi.Builder;
35  import aQute.lib.osgi.Jar;
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  public class AntPlugin extends BundlePlugin
46  {
47      static final String BUILD_XML = "/build.xml";
48      static final String BUILD_BND = "/maven-build.bnd";
49  
50  
51      protected void execute( MavenProject currentProject, Map originalInstructions, Properties properties,
52          Jar[] classpath ) throws MojoExecutionException
53      {
54          final String artifactId = getProject().getArtifactId();
55          final String baseDir = getProject().getBasedir().getPath();
56  
57          try
58          {
59              
60              Builder builder = buildOSGiBundle( currentProject, originalInstructions, properties, classpath );
61              Properties bndProperties = builder.getProperties();
62  
63              
64              for ( Iterator i = bndProperties.values().iterator(); i.hasNext(); )
65              {
66                  if ( !( i.next() instanceof String ) )
67                  {
68                      i.remove();
69                  }
70              }
71  
72              
73              bndProperties.setProperty( "-output", "${maven.build.dir}/${maven.build.finalName}.jar" );
74  
75              OutputStream out = new FileOutputStream( baseDir + BUILD_BND );
76              bndProperties.store( out, " Merged BND Instructions" );
77              IOUtil.close( out );
78  
79              
80              String buildXml = IOUtil.toString( getClass().getResourceAsStream( BUILD_XML ) );
81              buildXml = StringUtils.replace( buildXml, "BND_VERSION", builder.getVersion() );
82              buildXml = StringUtils.replace( buildXml, "ARTIFACT_ID", artifactId );
83  
84              FileUtils.fileWrite( baseDir + BUILD_XML, buildXml );
85  
86              
87              builder.close();
88          }
89          catch ( Exception e )
90          {
91              throw new MojoExecutionException( "Problem creating Ant script", e );
92          }
93  
94          getLog().info( "Wrote Ant bundle project for " + artifactId + " to " + baseDir );
95      }
96  }