skip to content

What does the core `groovy` plugin add on top of `java`, and what is joint compilation?

level: middleimportance: nice to knowfreq 38%

answer

  1. applies java, adds groovy source dir
  2. GroovyCompile tasks per source set
  3. you supply the groovy dependency
  4. joint compilation = stubs + javac + groovy
  5. Java must be under src/.../groovy to join

basics

~10 s

The groovy plugin applies java, adds a groovy source directory to each source set, registers compileGroovy/compileTestGroovy tasks, and supports joint compilation — compiling interdependent Groovy and Java sources together.

solid answer

~40 s

The core `groovy` plugin extends `java` to support Groovy. For every source set it adds a **`groovy`** source directory (e.g. `src/main/groovy`) and registers a `GroovyCompile` task — `compileGroovy`, `compileTestGroovy`. You add the Groovy library itself via the `implementation` configuration (Gradle doesn't bundle a Groovy version for you). Its standout feature is **joint compilation**: Groovy and Java sources placed under the `groovy` directory can reference each other circularly. The Groovy compiler builds stubs for the Java classes, lets `javac` compile the real Java, then finishes the Groovy — resolving mutual dependencies a separate two-pass `javac`-then-`groovyc` couldn't. Java files left in `src/main/java` are compiled by plain `javac` (no joint compilation). It's commonly used for Spock test suites and mixed Groovy/Java codebases.

code

kotlin · 11 lines
kotlin
plugins {
    groovy
}

dependencies {
    implementation("org.apache.groovy:groovy:4.0.21")
    testImplementation("org.spockframework:spock-core:2.3-groovy-4.0")
}

// Groovy + Java that reference each other go under src/main/groovy
// for joint compilation; plain src/main/java is compiled by javac only.

go deeper

for a junior

Know it adds Groovy source dirs and compile tasks on top of java.

for a middle

Explain joint compilation's stub→javac→groovy phases and the requirement that mutually-referencing Java live under the groovy dir.

for a senior

Discuss Spock integration, configuring GroovyCompile, and migration scenarios mixing Java and Groovy.

for a principal

Weigh Groovy's place in a polyglot JVM build strategy and standards for test frameworks across teams.

## What the plugin adds Applying `id("groovy")` first applies `java`, then for each source set: - adds a **`groovy`** source directory set (default `src/<sourceSet>/groovy`), - registers a **`GroovyCompile`** task: `compileGroovy`, `compileTestGroovy`, etc., - wires those into `classes`/`testClasses` so they run as part of the normal build. You must still declare the Groovy dependency yourself: ```kotlin plugins { groovy } dependencies { implementation("org.apache.groovy:groovy:4.0.21") testImplementation("org.spockframework:spock-core:2.3-groovy-4.0") } ``` ## Joint compilation **Joint compilation** lets Groovy and Java classes in the same source set depend on each other — even circularly — which a naive "compile Java then Groovy" (or vice versa) cannot handle. The Groovy compiler orchestrates it in phases: 1. Parse the Groovy sources and generate **Java stubs** (signatures only) for the Groovy types. 2. Invoke `javac` to compile the real Java sources against those stubs. 3. Complete compilation of the Groovy sources, now able to see the compiled Java types. The catch: joint compilation only applies to **Java files located under the `groovy` source directory**. Java files in the conventional `src/main/java` directory are compiled by the ordinary `compileJava` task with plain `javac` and do **not** participate in joint compilation. So to let a Groovy class and a Java class reference each other, put both under `src/main/groovy`. ## Configuring the Groovy compiler ```kotlin tasks.withType<GroovyCompile>().configureEach { groovyOptions.isFork = true } ``` ## Why it matters The biggest real-world use is **Spock** specifications: a Groovy-based BDD test framework. You apply `groovy`, add `spock-core`, and write `*.groovy` specs under `src/test/groovy`. Joint compilation also helps gradual migrations where Java and Groovy classes coexist and reference each other.

  • Does Gradle bundle a Groovy version when you apply the `groovy` plugin?
    No. The plugin adds the compile tasks and source dirs, but you must declare the Groovy library yourself (e.g. `implementation("org.apache.groovy:groovy:4.0.21")`).
  • Where must Java files live to participate in joint compilation with Groovy?
    Under the `groovy` source directory (e.g. `src/main/groovy`). Java in `src/main/java` is compiled separately by `compileJava` and won't join.

saying these in an interview costs you the question

  • Claiming the `groovy` plugin provides the Groovy runtime/version automatically.
  • Saying joint compilation works for Java files left in `src/main/java`.
  • Describing joint compilation as just 'compile Groovy then Java' without the stub phase.

context