1   package com.atlassian.maven.plugins.amps;
2   
3   import org.apache.commons.io.FileUtils;
4   import org.apache.maven.plugin.MojoExecutionException;
5   import org.apache.maven.plugin.MojoFailureException;
6   import org.jfrog.maven.annomojo.annotations.MojoGoal;
7   import org.jfrog.maven.annomojo.annotations.MojoParameter;
8   
9   import java.io.File;
10  import java.io.IOException;
11  
12  import static com.atlassian.maven.plugins.amps.util.FileUtils.file;
13  
14  
15  /**
16   * Jars the tests into an OSGi bundle.  Only builds the jar if the {@link #buildTestPlugin} flag is set or it detects
17   * an atlassian-plugin.xml file in the target/test-classes directory.
18   *
19   * Note, this test jar will not have its resources filtered or a manifest generated for it.  If no manifest is present,
20   * a dummy manifest that does a dynamic package import on everything will be used.
21   *
22   * @since 3.3
23   */
24  @MojoGoal("test-jar")
25  public class TestJarMojo extends AbstractAmpsMojo
26  {
27  
28      /**
29       * Whether the test plugin should be built or not.  If not specified, it detects an atlassian-plugin.xml in the
30       * test classes directory and builds if exists.
31       */
32      @MojoParameter
33      private Boolean buildTestPlugin;
34  
35      /**
36       * The final name for the test plugin, without the "-tests" suffix.
37       */
38      @MojoParameter(expression = "${project.build.finalName}")
39      private String finalName;
40  
41      public void execute() throws MojoExecutionException, MojoFailureException
42      {
43          boolean shouldBuild = false;
44          if (buildTestPlugin != null)
45          {
46              if (buildTestPlugin.booleanValue())
47              {
48                  shouldBuild = true;
49              }
50          }
51          else if (file(getMavenContext().getProject().getBuild().getTestOutputDirectory(), "atlassian-plugin.xml").exists())
52          {
53              shouldBuild = true;
54          }
55  
56          if (shouldBuild)
57          {
58              File mf = file(getMavenContext().getProject().getBuild().getTestOutputDirectory(), "META-INF", "MANIFEST.MF");
59              if (!mf.exists())
60              {
61                  try
62                  {
63                      FileUtils.writeStringToFile(mf,
64                             "Manifest-Version: 1.0\n" +
65                             "Bundle-SymbolicName: plugin-tests\n" +
66                             "Bundle-Version: 1.0\n" +
67                             "DynamicImport-Package: *\n");
68                  }
69                  catch (IOException e)
70                  {
71                      throw new MojoFailureException("Unable to write manifest");
72                  }
73              }
74              getMavenGoals().jarTests(finalName);
75          }
76      }
77  }
78