1   package com.atlassian.maven.plugins.amps;
2   
3   import com.atlassian.maven.plugins.amps.product.ProductHandler;
4   import com.atlassian.maven.plugins.amps.product.ProductHandlerFactory;
5   import com.atlassian.maven.plugins.amps.product.studio.StudioProductHandler;
6   import com.google.common.collect.Lists;
7   import org.apache.maven.plugin.MojoExecutionException;
8   import org.apache.maven.plugin.MojoFailureException;
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.List;
14  import java.util.Map;
15  
16  /**
17   * Creates a zip file containing the previous run's home directory
18   * in the proper format to use as test-resources.
19   *
20   * @since 3.1-m3
21   */
22  @MojoGoal("create-home-zip")
23  public class CreateHomeZipMojo extends AbstractProductHandlerMojo {
24  
25      /**
26       * Generated home directory zip file.
27       */
28      @MojoParameter(expression = "${homeZip}", required = false)
29      protected File homeZipFile;
30  
31      public void doExecute() throws MojoExecutionException, MojoFailureException
32      {
33          Product product = getProduct(instanceId);
34          ProductHandler productHandler = createProductHandler(product.getId());
35  
36          if (ProductHandlerFactory.STUDIO.equals(product.getId()))
37          {
38              configureStudio(product, (StudioProductHandler) productHandler);
39          }
40  
41          final File snapshotDir = productHandler.getSnapshotDirectory(product);
42          if (homeZipFile == null)
43          {
44              homeZipFile = new File(productHandler.getBaseDirectory(product), "generated-test-resources.zip");
45          }
46  
47          productHandler.createHomeZip(snapshotDir, homeZipFile, product);
48  
49          getLog().info("Home directory zip created successfully at " + homeZipFile.getAbsolutePath());
50      }
51  
52  
53  
54  
55      /**
56       * Configure the Studio product.
57       *
58       * @param studioProduct the studio product. Must not be another product, neither null.
59       * @param studioProductHandler the Studio product handler
60       */
61      private void configureStudio(Product studioProduct, StudioProductHandler studioProductHandler) throws MojoExecutionException
62      {
63          List<ProductExecution> executions = Lists.newArrayList(new ProductExecution(studioProduct, studioProductHandler));
64          includeStudioDependentProducts(executions, getMavenGoals());
65      }
66  
67      private Product getProduct(final String instanceId) throws MojoExecutionException
68      {
69          Map<String, Product> contexts = getProductContexts(getMavenGoals());
70  
71          Product product = contexts.get(instanceId);
72          if (product == null)
73          {
74              throw new MojoExecutionException("There is no instance with name " + instanceId + " defined in the pom.xml");
75          }
76          return product;
77      }
78  }