Gradle does not load Java class files compiled from a Java source hosted in buildsrc

I completed a custom task in groovy. If I provided a utility class for it, implemented in groovy (X.groovy) and placing it in buildsrc, the task will work. If I implement an equivalent class in Java (Y.java) and put it in the same directory, the task will end 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 indicated in the error message. Build fails when Y.java is in any of the usual places:

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

The Gradle Documentation says: “You can just put the assembly source code in this directory and stick to the layout agreement for the Java / Groovy project,” and its default buildsrc construction script will be applied by default.
Source: http://www.gradle.org/docs/current/userguide/organizing_build_logic.html#sec:build_sources

The project layout allows groovy source directories to contain groovy and Java code.
Source: http://www.gradle.org/docs/current/userguide/groovy_plugin.html#sec:groovyCompile

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 "xm" } } 

Y.java

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

OSX 10.8.4, IntelliJ 12.1, Gradle 1.8

+6
source share
2 answers

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

+2
source

just an idea: maybe this is due to the package declaration. Java is more picky here than groovy and expects the source file in the appropriate directory. I could not reproduce your problem. Can you provide a small standalone project that will demonstrate your problem?

amuses Rene

+1
source

Source: https://habr.com/ru/post/955736/


All Articles