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

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

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?

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}