Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Debugging Gradle plugins with IntelliJ IDEA

DZone's Guide to

Debugging Gradle plugins with IntelliJ IDEA

· DevOps Zone
Free Resource

Learn more about how CareerBuilder was able to resolve customer issues 5x faster by using Scalyr, the fastest log management tool on the market. 

When you develop Gradle plugins it's important to have an ability to debug them. There are already a couple of articles online about how to debug Gradle plugins in IDEA with remote debug, but I found it not very convenient since I need to remember JVM options for remote debug and pass it to Gradle. So, I decided to run Gradle from IDEA like a normal Java application. 

For example, you have a project with a structure like:

  • example/
    • build.gradle
  • gradle/
    • wrapper/
      • gradle-wrapper.jar
      • gradle-wrapper.properties
  • src/
    • main/
      • java/
  • build.gradle
  • gradlew
  • gradlew.bat
Where build.gradle and src/ - your plugin's build file and sources, and example/build.gradle - example project with content like:
buildscript {
  repositories {
    ivy {
      artifactPattern "../build/libs/[artifact]-[revision].[ext]"
    }
  }
  
  dependencies {
    classpath "your.company.name:your-plugin:version"    
  }
}

apply plugin: 'your-plugin'
Note that the project should have Gradle Wrapper. 

Now we want to add the ability to debug it in IDEA. Add to your plugin's build.gradle these lines:

apply plugin: 'idea'

configurations {
  gradleRuntime
}

idea {
  module {
    scopes.RUNTIME.plus += [ configurations.gradleRuntime ]
  }
}

dependencies {
  gradleRuntime files("gradle/wrapper/gradle-wrapper.jar")
}
Now Gradle Wrapper is available at runtime in IDEA (this will not add dependency to gradle-wrapper to your plugin, it's only for IDEA).

Now, only one thing left - IDEA run configuration. Just create a usual Run Configuration for your application, with the following configuration:


Where:

  • MainClass: org.gradle.wrapper.GradleWrapperMain
  • VM options:
  • -DdistributionBase=GRADLE_USER_HOME
    -DdistributionPath=wrapper/dists
    -DzipStoreBase=GRADLE_USER_HOME
    -DzipstorePath=wrapper/dists
  • Program arguments: all that goes after "gradle" command, for example "clean build"
  • Working directory: where you want to run your Gradle build ("example" folder in our case)
  • Before launch: remove "Make" and add Gradle task "jar"
That's it! Now you can debug just created configuration. Easy, isn't it?

Find out more about how Scalyr built a proprietary database that does not use text indexing for their log management tool.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}