1   package com.atlassian.maven.plugins.amps;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.maven.plugin.MojoExecutionException;
5   import org.apache.maven.plugin.MojoFailureException;
6   import org.jfrog.maven.annomojo.annotations.MojoExecute;
7   import org.jfrog.maven.annomojo.annotations.MojoGoal;
8   import org.jfrog.maven.annomojo.annotations.MojoParameter;
9   import org.jfrog.maven.annomojo.annotations.MojoRequiresDependencyResolution;
10  import com.atlassian.maven.plugins.amps.product.ProductHandlerFactory;
11  import com.atlassian.maven.plugins.amps.util.GoogleAmpsTracker;
12  
13  import java.util.List;
14  
15  /**
16   * Debug the webapp
17   */
18  @MojoGoal ("debug")
19  @MojoExecute (phase = "package")
20  @MojoRequiresDependencyResolution
21  public class DebugMojo extends RunMojo
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          getGoogleTracker().track(GoogleAmpsTracker.DEBUG);
40  
41          final List<ProductExecution> productExecutions = getProductExecutions();
42          setParallelMode(productExecutions);
43  
44          int counter = 0;
45          for (ProductExecution productExecution : productExecutions)
46          {
47              final Product product = productExecution.getProduct();
48  
49              if (product.getJvmDebugPort() == 0)
50              {
51                  product.setJvmDebugPort(jvmDebugPort + counter++);
52              }
53              final int port = product.getJvmDebugPort();
54  
55              String debugArgs = " -Xdebug -Xrunjdwp:transport=dt_socket,address=" +
56                                 String.valueOf(port) + ",suspend=" + (jvmDebugSuspend ? "y" : "n") + ",server=y ";
57  
58              if (product.getJvmArgs() == null)
59              {
60                  product.setJvmArgs(StringUtils.defaultString(jvmArgs));
61              }
62  
63              product.setJvmArgs(product.getJvmArgs() + debugArgs);
64  
65              if (writePropertiesToFile)
66              {
67                  if (productExecutions.size() == 1)
68                  {
69                      properties.put("debug.port", String.valueOf(port));
70                  }
71  
72                  properties.put("debug." + product.getInstanceId() + ".port", String.valueOf(port));
73              }
74  
75              if (ProductHandlerFactory.FECRU.equals(getDefaultProductId()) && debugNotSet()) {
76                  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.";
77                  getLog().error(message);
78                  throw new MojoFailureException(message);
79              }
80          }
81  
82          startProducts(productExecutions);
83      }
84  
85      private boolean debugNotSet()
86      {
87          String atlasOpts = System.getenv("ATLAS_OPTS");
88          return atlasOpts == null || !atlasOpts.contains("-Xdebug");
89      }
90  }