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 homeZip;
30  
31      public void doExecute() throws MojoExecutionException, MojoFailureException
32      {
33          Product product = getProduct(instanceId, getProductId());
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 (homeZip == null)
43          {
44              homeZip = new File(productHandler.getBaseDirectory(product), "generated-test-resources.zip");
45          }
46  
47          productHandler.createHomeZip(snapshotDir, homeZip, product);
48  
49          getLog().info("Home directory zip created successfully at " + homeZip.getAbsolutePath());
50  
51          // Make the file the artifact of the project
52          getMavenGoals().attachArtifact(homeZip, "zip");
53  
54      }
55  
56  
57  
58  
59      /**
60       * Configure the Studio product.
61       *
62       * @param studioProduct the studio product. Must not be another product, neither null.
63       * @param studioProductHandler the Studio product handler
64       */
65      private void configureStudio(Product studioProduct, StudioProductHandler studioProductHandler) throws MojoExecutionException
66      {
67          List<ProductExecution> executions = Lists.newArrayList(new ProductExecution(studioProduct, studioProductHandler));
68          includeStudioDependentProducts(executions, getMavenGoals());
69      }
70  
71      /**
72       * Returns the product to snapshot.
73       * @param instanceId the instance to snapshot (preferred solution to reference the product)
74       * @param productId the product to snapshot if instanceId is null. It is not advisable to use this parameter,
75       * as it doesn't reference the instance in a unique manner.
76       *
77       * @return a Product object
78       * @throws MojoExecutionException
79       */
80      private Product getProduct(final String instanceId, final String productId) throws MojoExecutionException
81      {
82          Map<String, Product> contexts = getProductContexts();
83  
84          Product product = null;
85          if (instanceId != null)
86          {
87              product = contexts.get(instanceId);
88              if (product == null)
89              {
90                  throw new MojoExecutionException("There is no instance with name " + instanceId + " defined in the pom.xml");
91              }
92          }
93          else
94          {
95              for (Product candidate : contexts.values())
96              {
97                  if (candidate.getId().equals(productId))
98                  {
99                      product = candidate;
100                     break;
101                 }
102             }
103             if (product == null)
104             {
105                 throw new MojoExecutionException("There is no product with name " + productId + " defined in the pom.xml. Please use -DinstanceId=..." +
106                 		" to set the instance to snapshot.");
107             }
108         }
109 
110         return product;
111     }
112 }