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