title: Debug Gradle plugins with IntelliJ
body: <h2>Problem</h2>
  
  <p><strong>I want to use the interactive debugger with IntelliJ.</strong> <strike>Unfortunately, I can't convince IntelliJ to load and compile the plugin. However,</strike> I can do <code>gradle clean build</code> and the plugin builds and runs its tests as expected.</p>
  
  <p>Specifically, I'm trying to debug local changes to <a href="https://github.com/eriwen/gradle-js-plugin.git" rel="noreferrer">gradle-js-plugin</a> <strike>and IntelliJ says it can't find <code>com.google.javascript.jscomp.CompilerOptions</code> as well as <code>spock.lang.Specification</code>. (I'm thinking maybe it's something about the way they are loaded, but that's a guess.)</strike> </p>
  
  <p><br></p>
  
  <h2>Things I've tried</h2>
  
  <p><em>NOTE:</em> I didn't revert any processes between steps.</p>
  
  <h3>0. My First Guess</h3>
  
  <p>I noticed <a href="http://docs.codehaus.org/display/GRADLE/How+to+run+the+Gradle+project+in+IntelliJ+against+any+Gradle+build" rel="noreferrer">a howto</a> on <a href="http://docs.codehaus.org/" rel="noreferrer">docs.codehaus.org</a>. IntelliJ couldn't find <code>org.gradle.launcher.GradleMain</code>, so I've adapted it to use <code>GradleLauncher</code> with the following:</p>
  
  <pre><code>import org.gradle.GradleLauncher
  
  class GradleScriptRunner {
      public static void main(String[] args) {
          GradleLauncher.newInstance(
              "-p", 
              "/path/to/gradle-js-plugin/src/test/resources/build.gradle", 
              "clean assemble"
          )
      }
  }
  </code></pre>
  
  <p>Per <a href="http://gradle.org/docs/current/javadoc/org/gradle/GradleLauncher.html" rel="noreferrer">GradleLauncher's documentation</a>.</p>
  
  <p><strong>Outcome:</strong> IntelliJ won't compile the project.</p>
  
  <p><hr></p>
  
  <h3>1. Per <a href="https://stackoverflow.com/a/11129708/455581">Peter Niederwieser's answer</a> Fix idea project &amp; debug via plugin</h3>
  
  <h3>Steps</h3>
  
  <ol>
  <li><code>~# cd /path/to/gradle-js-plugin &amp;&amp; gradle cleanIdea idea</code></li>
  <li>Opened the newly created project and attempted to debug using the ScriptRunner from step 0.</li>
  </ol>
  
  <p><strong>Outcome:</strong> Project compiles (<em>yay!</em>), but I can only hit breakpoints in <code>GradleScriptRunner.groovy</code>.</p>
  
  <p><hr></p>
  
  <h3>2. Per <a href="https://stackoverflow.com/a/11129708/455581">Peter Niederwieser's answer</a> run gradle CLI w/ special options</h3>
  
  <p>1 &amp; 2. Merged for clarity:</p>
  
  <pre><code>~# export GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
  ~# gradle clean assemble
  Listening for transport dt_socket at address: 5005
  </code></pre>
  
  <ol start="3">
  <li>Configure IntelliJ to connect to this port and start debugging (see image):
  <img src="https://i.stack.imgur.com/aDwVA.png" alt="How I configured the debugger"></li>
  </ol>
  
  <p><strong>For this step I tried the following .gradle file configurations:</strong></p>
  
  <p><em>1. Use only build.gradle</em></p>
  
  <p>--build.gradle--</p>
  
  <pre><code>apply plugin: 'groovy'
  apply plugin: 'java'
  apply plugin: 'idea'
  apply plugin: 'maven'
  apply plugin: 'js'
  
  buildscript {
      repositories {
          mavenLocal()
          mavenCentral()
      }
  
      dependencies {
          compile findProject "/path/to/gradle-js-plugin"
      }
  }
  
  repositories {
      mavenLocal()
      mavenCentral()
  }
  </code></pre>
  
  <p><strong>Outcome:</strong> </p>
  
  <pre><code>FAILURE: Build failed with an exception.
  
  * Where:
  Build file '/path/to/gradle-js-plugin/src/test/resources/build.gradle' line: 13
  
  * What went wrong:
  A problem occurred evaluating root project 'resources'.
  &gt; No such property: findProject for class: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler
  
  * Try:
  Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
  
  BUILD FAILED
  
  Total time: 8 mins 50.498 secs
  </code></pre>
  
  <p><em>2. Use both build.gradle and settings.gradle</em></p>
  
  <p>--settings.gradle--</p>
  
  <pre><code>include "/path/to/gradle-js-plugin"
  </code></pre>
  
  <p>--build.gradle--</p>
  
  <pre><code>apply plugin: 'groovy'
  apply plugin: 'java'
  apply plugin: 'idea'
  apply plugin: 'maven'
  apply plugin: 'js'
  
  buildscript {
      repositories {
          mavenLocal()
          mavenCentral()
      }
  }
  
  repositories {
      mavenLocal()
      mavenCentral()
  }
  </code></pre>
  
  <p><strong>Outcome:</strong></p>
  
  <pre><code>FAILURE: Build failed with an exception.
  
  * Where:
  Build file '/path/to/gradle-js-plugin/src/test/resources/build.gradle' line: 5
  
  * What went wrong:
  A problem occurred evaluating root project 'resources'.
  &gt; Plugin with id 'js' not found.
  
  * Try:
  Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
  
  BUILD FAILED
  
  Total time: 13.553 secs
  </code></pre>
  
  <p><hr></p>
  
  <h2>My Setup</h2>
  
  <h3>Gradle</h3>
  
  <pre><code>~# gradle -v
  ------------------------------------------------------------
  Gradle 1.0
  ------------------------------------------------------------
  
  Gradle build time: Tuesday, June 12, 2012 12:56:21 AM UTC
  Groovy: 1.8.6
  Ant: Apache Ant(TM) version 1.8.2 compiled on December 20 2010
  Ivy: 2.2.0
  JVM: 1.7.0_04 (Oracle Corporation 23.0-b21)
  OS: Linux 3.2.0-2-amd64 amd64
  </code></pre>
  
  <h3>Java</h3>
  
  <pre><code>~# java -version
  java version "1.7.0_04"
  Java(TM) SE Runtime Environment (build 1.7.0_04-b20)
  Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)
  </code></pre>
  
  <h3>IntelliJ</h3>
  
  <pre><code>IntelliJ IDEA Ultimate 117.499 w/ Bundled Gradle plugin
  </code></pre>
  
  <p></p>
  
  <h1>Hoping for</h1>
  
  <p>Any tips that'll get me into debug mode within the plugin.</p>