Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle not loading Java class files compiled from Java source placed in buildsrc

I've implemented a custom task in groovy. If I provide a utility class for it implemented in groovy (X.groovy) and place it in buildsrc, the task works. If I implement an equivalent class in Java (Y.java) and place it in the same directory, the task fails with the following error message:

:buildsrc:compileGroovystartup failed:
General error during conversion: Could not load class 'com.myinc.gradle.api.data.Y' 
from file:/project/buildsrc/build/classes/main/com/myinc/gradle/api/data/Y.class.

The Y.class file exists at the location specified in the error message. The build fails when Y.java is in either of the usual places:

buildsrc/src/main/groovy/.../Y.java<br>
buildsrc/src/main/java/.../Y.java

Gradle documentation says "you can just put your build source code in this directory and stick to the layout convention for a Java/Groovy project" and its default buildsrc build script will be applied.
Source: http://www.gradle.org/docs/current/userguide/organizing_build_logic.html#sec:build_sources

Project Layout permits Groovy source directories to contain Groovy and Java code.
Source: http://www.gradle.org/docs/current/userguide/groovy_plugin.html#sec:groovyCompile

To replicate:

project/build.gradle:

task t (type: sample.MyTask) {
  println "configuring task"
}

project/buildsrc/src/main/groovy/sample


MyTask.groovy

package sample

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction

class MyTask extends DefaultTask {
  @TaskAction
  public void task() {
    println 'task action'
    new X().m()
//  new Y().m()    // uncommenting this line should generate an error when you build 't'
  }
}

X.groovy

package sample;
class X {
  void m() {
    println "x.m"
  }
}

Y.java

package sample;
public class Y {
  void m() {
    System.out.println("y.m");
  }
}

OSX 10.8.4, IntelliJ 12.1, Gradle 1.8

like image 348
wjohnson Avatar asked Oct 11 '13 04:10

wjohnson


People also ask

What is buildSrc folder gradle?

What is buildSrc. buildSrc is a directory at the Gradle project root, which can contain our build logic. This allows us to use the Kotlin DSL to write our custom build code with very little configuration and share this logic across the whole project.

Does Gradle use javac?

Gradle is not actually using javac (the executable). Rather, it uses the compiler classes programmatically. By default, Gradle will use the classes from the JDK you use for running Gradle. You can check the version by running gradlew --version .


2 Answers

The problem in the larger context was an incompatibility in bytecode versions between the early access version of JDK8 and what the class loader in groovyCompile in Gradle 1.8 expects. When I changed the language levels in IntelliJ back to JDK7, everything worked fine.

like image 51
wjohnson Avatar answered Nov 03 '22 11:11

wjohnson


just an idea: Maybe it's related to the package declaration. Java is more picky here than groovy and expects the source file in an according directory. I couldn't reproduce your issue. Can you provide a small selfcontained project that demos your issue?

cheers, René

like image 1
Rene Groeschke Avatar answered Nov 03 '22 10:11

Rene Groeschke