1   package com.atlassian.maven.plugins.amps.osgi;
2   
3   import aQute.lib.osgi.Constants;
4   import com.atlassian.maven.plugins.amps.AbstractAmpsMojo;
5   import static com.atlassian.maven.plugins.amps.util.FileUtils.*;
6   import org.apache.maven.plugin.MojoExecutionException;
7   import org.apache.maven.plugin.MojoFailureException;
8   import org.apache.maven.project.MavenProject;
9   import org.jfrog.maven.annomojo.annotations.MojoGoal;
10  import org.jfrog.maven.annomojo.annotations.MojoParameter;
11  
12  import java.io.File;
13  import java.util.HashMap;
14  import java.util.Map;
15  
16  @MojoGoal("generate-manifest")
17  public class GenerateManifestMojo extends AbstractAmpsMojo
18  {
19      /**
20       * The BND instructions for the bundle.
21       */
22      @MojoParameter
23      private Map instructions = new HashMap();
24  
25      public void execute() throws MojoExecutionException, MojoFailureException
26      {
27          final MavenProject project = getMavenContext().getProject();
28          if (!instructions.isEmpty())
29          {
30              getLog().info("Generating a manifest for this plugin");
31  
32              if (!instructions.containsKey(Constants.EXPORT_PACKAGE))
33              {
34                  instructions.put(Constants.EXPORT_PACKAGE, "");
35              }
36  
37              File metainfLib = file(project.getBuild().getOutputDirectory(), "META-INF", "lib");
38              if (metainfLib.exists())
39              {
40                  StringBuilder sb = new StringBuilder(".");
41                  for (File lib : metainfLib.listFiles())
42                  {
43                      sb.append(",").append("META-INF/lib/" + lib.getName());
44                  }
45                  instructions.put(Constants.BUNDLE_CLASSPATH, sb.toString());
46              }
47              getMavenGoals().generateManifest(instructions);
48          }
49          else if  (OsgiHelper.isAtlassianPlugin(project))
50          {
51              getLog().warn("Atlassian plugin detected as the organisation name includes the string 'Atlassian'.  If " +
52                            "this is meant for production, you should add bundle " +
53                            "instructions specifically configuring what packages are imported and exported.  This " +
54                            "helps catch manifest generation bugs during the build rather than upon install.  The " +
55                            "bundle generation configuration can be specified " +
56                            "via the <instructions> element in the maven-" + getPluginInformation().getId()+"-plugin configuration.  For example:\n" +
57                            "    <configuration>\n" +
58                            "        <Import-Package>\n" +
59                            "            com.atlassian.myplugin*,\n" +
60                            "            com.library.optional.*;resolution:=optional,\n" +
61                            "            *\n" +
62                            "        </Import-Package>\n" +
63                            "    </configuration>\n\n" +
64                            "See the Maven bundle plugin (which is used under the covers) for more info: " +
65                            "http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html#ApacheFelixMavenBundlePlugin%28BND%29-Instructions");
66          }
67          else
68          {
69              getLog().info("No manifest instructions found, skipping manifest generation");
70          }
71      }
72  }