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