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  /**
12   * Debug the webapp
13   */
14  @MojoGoal ("debug")
15  @MojoExecute (phase = "package")
16  @MojoRequiresDependencyResolution
17  public class DebugMojo extends RunMojo
18  {
19      /**
20       * port for debugging
21       */
22      @MojoParameter (expression = "${jvm.debug.port}", defaultValue = "5005")
23      protected int jvmDebugPort;
24  
25      /**
26       * Suspend when debugging
27       */
28      @MojoParameter (expression = "${jvm.debug.suspend}")
29      protected boolean jvmDebugSuspend = false;
30  
31  
32      @Override
33      protected void doExecute() throws MojoExecutionException, MojoFailureException
34      {
35      	String debugArgs = " -Xdebug -Xrunjdwp:transport=dt_socket,address=" +
36      				    String.valueOf(jvmDebugPort) + ",suspend=" + (jvmDebugSuspend ? "y" : "n") + ",server=y ";
37          
38          // add the debug jvm args for the global config
39          if (jvmArgs == null)
40          {
41              jvmArgs = "-Xmx512m -XX:MaxPermSize=160m";
42          }
43          jvmArgs += debugArgs;
44  
45          // add the debug jvm args for each of the product configs
46          for (Product product : products)
47          {
48              if (product.getJvmArgs() == null)
49              {
50                  product.setJvmArgs("-Xmx512m -XX:MaxPermSize=160m");
51              }
52              product.setJvmArgs(product.getJvmArgs() + debugArgs);
53          }
54          
55          if (writePropertiesToFile)
56          {
57              properties.put("debug.port", String.valueOf(jvmDebugPort));
58          }
59  
60          if (ProductHandlerFactory.FECRU.equals(getDefaultProductId()) && debugNotSet()) {
61              String message = "You must set the ATLAS_OPTS environment variable to the following string:'" + jvmArgs + "' when calling atlas-debug to enable Fisheye/Crucible debugging.";
62              getLog().error(message);            
63              throw new MojoFailureException(message);
64          }
65  
66          super.doExecute();
67      }
68  
69      private boolean debugNotSet()
70      {
71          String atlasOpts = System.getenv("ATLAS_OPTS");
72          return atlasOpts == null || !atlasOpts.contains("-Xdebug");
73      }
74  }