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.File;
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.plugins.annotations.Mojo;
30 import org.apache.maven.plugins.annotations.ResolutionScope;
31 import org.apache.maven.shared.dependency.graph.DependencyNode;
32 import org.codehaus.plexus.util.FileUtils;
33 import org.codehaus.plexus.util.IOUtil;
34 import org.codehaus.plexus.util.StringUtils;
35
36 import aQute.bnd.osgi.Builder;
37 import aQute.bnd.osgi.Jar;
38
39
40
41
42
43
44 @Mojo( name = "ant", requiresDependencyResolution = ResolutionScope.TEST )
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 @Override
52 protected void execute( Map<String, String> originalInstructions,
53 ClassPathItem[] classpath ) throws MojoExecutionException
54 {
55 final String artifactId = getProject().getArtifactId();
56 final String baseDir = getProject().getBasedir().getPath();
57
58 try
59 {
60
61 Builder builder = buildOSGiBundle(project,
62 originalInstructions, classpath );
63 Properties bndProperties = builder.getProperties();
64
65
66 for ( Iterator i = bndProperties.values().iterator(); i.hasNext(); )
67 {
68 if ( !( i.next() instanceof String ) )
69 {
70 i.remove();
71 }
72 }
73
74
75 bndProperties.setProperty( "-output", "${maven.build.dir}/${maven.build.finalName}.jar" );
76
77 OutputStream out = buildContext.newFileOutputStream( new File(baseDir + BUILD_BND) );
78 bndProperties.store( out, " Merged BND Instructions" );
79 IOUtil.close( out );
80
81
82 String buildXml = IOUtil.toString( getClass().getResourceAsStream( BUILD_XML ) );
83 buildXml = StringUtils.replace( buildXml, "BND_VERSION", builder.getVersion() );
84 buildXml = StringUtils.replace( buildXml, "ARTIFACT_ID", artifactId );
85
86 FileUtils.fileWrite( baseDir + BUILD_XML, buildXml );
87
88
89 builder.close();
90 }
91 catch ( Exception e )
92 {
93 throw new MojoExecutionException( "Problem creating Ant script", e );
94 }
95
96 getLog().info( "Wrote Ant bundle project for " + artifactId + " to " + baseDir );
97 }
98 }