简体   繁体   English

Gradle没有加载从buildrc中放置的Java源编译的Java类文件

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

I've implemented a custom task in groovy. 我在groovy中实现了一个自定义任务。 If I provide a utility class for it implemented in groovy (X.groovy) and place it in buildsrc , the task works. 如果我为它在groovy(X.groovy)中实现它提供了一个实用程序类并将它放在buildsrc中 ,那么任务就可以了。 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: 如果我在Java(Y.java)中实现等效类并将其放在同一目录中,则任务失败并显示以下错误消息:

: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. Y.class文件存在于错误消息中指定的位置。 The build fails when Y.java is in either of the usual places: 当Y.java位于任何常用位置时,构建失败:

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. Gradle文档说“您可以将构建源代码放在此目录中并坚持Java / Groovy项目的布局约定”,并将应用其默认的buildsrc构建脚本。
Source: http://www.gradle.org/docs/current/userguide/organizing_build_logic.html#sec:build_sources 来源: 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. Project Layout允许Groovy源目录包含Groovy和Java代码。
Source: http://www.gradle.org/docs/current/userguide/groovy_plugin.html#sec:groovyCompile 资料来源: http//www.gradle.org/docs/current/userguide/groovy_plugin.html#sec : groovyCompile

To replicate: 复制:

project/build.gradle: 项目/的build.gradle:

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

project/buildsrc/src/main/groovy/sample 项目/ buildsrc / src目录/主/常规/样本


MyTask.groovy 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 X.groovy

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

Y.java Y.java

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

OSX 10.8.4, IntelliJ 12.1, Gradle 1.8 OSX 10.8.4,IntelliJ 12.1,Gradle 1.8

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. 更大的上下文中的问题是JDK8的早期访问版本与Gradle 1.8中的groovyCompile中的类加载器所期望的字节码版本不兼容。 When I changed the language levels in IntelliJ back to JDK7, everything worked fine. 当我将IntelliJ中的语言级别更改回JDK7时,一切正常。

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. Java比groovy更挑剔,并期望源文件在相应的目录中。 I couldn't reproduce your issue. 我无法重现你的问题。 Can you provide a small selfcontained project that demos your issue? 您能否提供一个小型的自包含项目来演示您的问题?

cheers, René 欢呼,勒内

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以将自定义 Java 库导入到 Gradle buildSrc Java class? - Is it possible to import custom Java library to a Gradle buildSrc Java class? IntelliJ IDEA:如何使从源文件(.java)编译的Java字节码文件(.class)与源文件位于同一目录中? - IntelliJ IDEA: how to make the java bytecode files (.class) compiled from the source files (.java) be in the same directory as the source files? 将编译后的 class 文件添加到类路径中,以便在 java gradle 项目中导入 - Add compiled class files to classpath for import in java gradle project Gradle自定义插件:读取编译的Java类 - Gradle Custom Plugins: Read Compiled Java Class 用Java加载已编译的类并从文件反序列化其实例 - Loading a compiled class in Java and deserializing it's instance from a file 列出由javac编译的Java源文件 - List the Java source files compiled by javac 为什么编译的Java类文件小于C编译文件? - Why are compiled Java class files smaller than C compiled files? Java gradle build 不重新编译源文件 - Java gradle build not recompiling source files Grails无法获取已编译的Java .class文件 - Grails not picking up compiled java .class files java-设置手动编译的类文件的位置 - java -setting the location of manually compiled class files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM