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
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.
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 .
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.
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é
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With