1   package com.atlassian.maven.plugins.amps;
2   
3   import org.apache.maven.plugin.MojoExecutionException;
4   import org.apache.maven.plugin.MojoFailureException;
5   import org.jfrog.maven.annomojo.annotations.MojoExecute;
6   import org.jfrog.maven.annomojo.annotations.MojoGoal;
7   import org.jfrog.maven.annomojo.annotations.MojoParameter;
8   import org.jfrog.maven.annomojo.annotations.MojoRequiresDependencyResolution;
9   import com.atlassian.maven.plugins.amps.product.ProductHandlerFactory;
10  
11  import java.util.List;
12  
13  /**
14   * Debug the webapp
15   */
16  @MojoGoal ("debug")
17  @MojoExecute (phase = "package")
18  @MojoRequiresDependencyResolution
19  public class DebugMojo extends RunMojo
20  {
21      private static final String DEFAULT_JVM_ARGS = "-Xmx512m -XX:MaxPermSize=160m";
22  
23      /**
24       * port for debugging
25       */
26      @MojoParameter (expression = "${jvm.debug.port}", defaultValue = "5005")
27      protected int jvmDebugPort;
28  
29      /**
30       * Suspend when debugging
31       */
32      @MojoParameter (expression = "${jvm.debug.suspend}")
33      protected boolean jvmDebugSuspend = false;
34  
35  
36      @Override
37      protected void doExecute() throws MojoExecutionException, MojoFailureException
38      {
39          final List<ProductExecution> productExecutions = getProductExecutions();
40  
41          if (jvmArgs == null)
42          {
43              jvmArgs = DEFAULT_JVM_ARGS;
44          }
45  
46          int counter = 0;
47          for (ProductExecution productExecution : productExecutions)
48          {
49              final Product product = productExecution.getProduct();
50  
51              if (product.getJvmDebugPort() == 0)
52              {
53                  product.setJvmDebugPort(jvmDebugPort + counter++);
54              }
55              final int port = product.getJvmDebugPort();
56  
57              String debugArgs = " -Xdebug -Xrunjdwp:transport=dt_socket,address=" +
58                                 String.valueOf(port) + ",suspend=" + (jvmDebugSuspend ? "y" : "n") + ",server=y ";
59  
60              if (product.getJvmArgs() == null)
61              {
62                  product.setJvmArgs(jvmArgs);
63              }
64  
65              product.setJvmArgs(product.getJvmArgs() + debugArgs);
66  
67              if (writePropertiesToFile)
68              {
69                  if (productExecutions.size() == 1)
70                  {
71                      properties.put("debug.port", String.valueOf(port));
72                  }
73  
74                  properties.put("debug." + product.getInstanceId() + ".port", String.valueOf(port));
75              }
76  
77              if (ProductHandlerFactory.FECRU.equals(getDefaultProductId()) && debugNotSet()) {
78                  String message = "You must set the ATLAS_OPTS environment variable to the following string:'" + product.getJvmArgs() + "' when calling atlas-debug to enable Fisheye/Crucible debugging.";
79                  getLog().error(message);
80                  throw new MojoFailureException(message);
81              }
82          }
83  
84          startProducts(productExecutions);
85      }
86  
87      private boolean debugNotSet()
88      {
89          String atlasOpts = System.getenv("ATLAS_OPTS");
90          return atlasOpts == null || !atlasOpts.contains("-Xdebug");
91      }
92  }