简体   繁体   English

Gradle compileJava 无法从源文件中找到符号

[英]Gradle compileJava cannot find symbol from source file

I'm new with Gradle, so there's a chance that I am missing something obvious here.我是 Gradle 的新手,所以我有可能在这里遗漏了一些明显的东西。 I am working on using Gradle for automated building of our projects.我正在致力于使用 Gradle 来自动构建我们的项目。 I've tackled most of them, but one is causing me unending trouble.我已经解决了其中的大部分问题,但有一个给我带来了无休止的麻烦。 Since this is for my company, I can't share any real code, but I will do my best to provide realistic pseudo-code.由于这是为我的公司准备的,我不能分享任何真实的代码,但我会尽力提供真实的伪代码。

I am attempting to compile a web application with a flex front-end and java back-end.我正在尝试编译一个带有 flex 前端和 java 后端的 web 应用程序。 Right now, I am focused solely on creating a.war file with the java in it, then I will move into the flex compilation.现在,我只专注于创建一个包含 java 的 .war 文件,然后我将进入 flex 编译。 When I run the following build.gradle script, it gives me countless "cannot find symbol" errors.当我运行以下 build.gradle 脚本时,它给了我无数“找不到符号”错误。

apply plugin: 'eclipse'
apply plugin: 'war'

sourceCompatibility = 1.6

repositories {
   mavenCentral()
}

sourceSets.main.java.srcDir 'src'
sourceSets.main.resources.srcDir 'src'

webAppDirName = "WebContent"

war.archiveName "Gradle_test.war"

dependencies {
    compile fileTree(dir: 'WebContent/WEB-INF/lib', include: '*.jar')
    providedCompile 'org.apache.tomcat:catalina:6.0.13'
    providedCompile 'org.apache.tomcat:dbcp:6.0.13'
}

An example of the errors that were occurring (with fake package and class names):发生错误的示例(使用假包和类名):

/Users/user/Documents/eclipse/ProjectName/src/com/companyName/teamName/projectName/core/release/subProjectName/projectVersion/RenamedObject.java:385: cannot find symbol
symbol  : class OtherObject
location: class com.companyName.teamName.projectName.core.release.subProjectName.projectVersion.RenamedObject
    public class InnerClass extends OtherObject implements Indexable<String>, Serializable {

The class InnerClass is declared within the RenamedObject.java file along with the RenamedObject class.类 InnerClass 与 RenamedObject 类一起在 RenamedObject.java 文件中声明。 The symbol it cannot find, OtherObject, is not from a.jar.它找不到的符号 OtherObject 不是来自 a.jar。 It is a source file stored at /Users/user/Documents/eclipse/ProjectName/src/com/companyName/teamName/projectName/core/release/subProjectName/projectVersion/superTypes/OtherObject.java .它是存储在/Users/user/Documents/eclipse/ProjectName/src/com/companyName/teamName/projectName/core/release/subProjectName/projectVersion/superTypes/OtherObject.java的源文件。 The object is imported at the top of RenamedObject.java and can be built and run properly via eclipse without any special settings.该对象在 RenamedObject.java 的顶部导入,无需任何特殊设置即可通过 eclipse 正确构建和运行。 These errors occur during the:compileJava task after executing gradle war from terminal.这些错误发生在从终端执行gradle war后的 :compileJava 任务期间。

Is there any reason that Gradle would have an issue with handling source imports from a sub-package like this? Gradle 是否有任何理由在处理来自这样的子包的源导入时出现问题? Am I just doing something wrong?我只是做错了什么吗? Let me know if there is any more info that can help and I will provide it if I can.让我知道是否有更多信息可以提供帮助,如果可以的话,我会提供。

EDIT: I got it to work.编辑:我得到它的工作。 All 130 of the errors were resolved with one change.一次更改就解决了所有 130 个错误。 If I add the fully qualified package name to OtherObject in the extends, so it looks like this:如果我将完全限定的包名称添加到扩展中的 OtherObject,那么它看起来像这样:

public class InnerClass extends com.companyName.teamName.projectName.core.release.subProjectName.projectVersion.superTypes.OtherObject implements Indexable<String>, Serializable {

That one change made it all work, but I still don't understand the reason.那个改变使一切正常,但我仍然不明白原因。 That is not the only reference to OtherObject in the project and it is not the only place that an inner class extends a class defined in another source package.这不是项目中对 OtherObject 的唯一引用,也不是内部类扩展另一个源包中定义的类的唯一地方。

Symptoms症状

Could not execute build using Gradle distribution ' https://services.gradle.org/distributions/gradle-2.8-bin.zip '无法使用 Gradle 分发版执行构建 ' https://services.gradle.org/distributions/gradle-2.8-bin.zip '

... .... ..... ………………

Caused by: org.gradle.api.internal.tasks.compile.CompilationFailedException: Compilation failed;引起:org.gradle.api.internal.tasks.compile.CompilationFailedException:编译失败; see the compiler error output for details.有关详细信息,请参阅编译器错误输出。

The Gradle Build Reports that you should check the compiler Java Output. Gradle Build 报告您应该检查编译器 Java 输出。

Check the output and see which class name it cites in the output which it is unable to resolve.检查输出并查看它在无法解析的输出中引用了哪个类名。

symbol:  class <referenced_classname_here
location: class <parent_classname_here

Solution解决方案

Provide the Fully Qualified Domain Name wherever you reference the class that is mentioned as the symbol not resolved.在您引用作为未解析符号提及的类的任何地方提供完全限定域名。

In my case, I have seen this when I defined an Inner Class which extends a class in a library that I had declared as a dependency in my build.gradle.就我而言,我在定义一个内部类时看到了这一点,该类扩展了我在 build.gradle 中声明为依赖项的库中的类。

PS: PS:

I have seen that the solution for this has already been mentioned as a comment to the question by the OP.我已经看到 OP 已经将解决​​方案作为对问题的评论提到了。

I could not find this question since it did not have the right keywords for google and also there was no accepted solution which further reduced its visibility.我找不到这个问题,因为它没有正确的 google 关键字,而且也没有公认的解决方案,这进一步降低了它的可见性。

The result of this is ONE FULL DAY WASTED, several strands of hair plucked out before I realized this stupid hackish solution :( :(结果浪费了一整天,在我意识到这个愚蠢的黑客解决方案之前拔掉了几缕头发:(:(

I have tried everything under the Sun that is remotely related to dependency resolution to fix this "dependency issue" which was never an issue with my codebase.我已经在 Sun 下尝试了所有与依赖解析相关的东西来解决这个“依赖问题”,这在我的代码库中从来都不是问题。

Hope the Gradle Team fixes this ASAP before more people pluck their hair.希望 Gradle 团队在更多人拔头发之前尽快解决这个问题。

Btw... I Love Gradle.顺便说一句......我喜欢Gradle。 :) :)

Just adding to the knowledge base, but I had just the same type of problem with:只是添加到知识库中,但我遇到了相同类型的问题:

public static class GetNetworkInterfacesCallable implements java.util.concurrent.Callable<java.util.Set<java.net.InetAddress>> {
    public GetNetworkInterfacesCallable(String networkPrefix) {
        this.networkPrefix=networkPrefix;
    }
    @Override public Set<InetAddress> call() throws Exception {
        Thread.currentThread().setName(getClass().getName());
        return null; //myInetAddresses(networkPrefix);
    }
    final String networkPrefix;
}

Using gradle 2.9 and 2.10.使用 gradle 2.9 和 2.10。

I had same problem here today.我今天在这里遇到了同样的问题。 If you are working with gradle, be sure your class is in src/main/java not src/test/java otherwise your IDE (for example Eclipse) will say that all compiles find, but when you try to build with gradle, test classes are not included by default (and it's okay!), so it will not compile.如果您正在使用 gradle,请确保您的类在 src/main/java 中而不是 src/test/java 否则您的 IDE(例如 Eclipse)会说所有编译都找到了,但是当您尝试使用 gradle 构建时,测试类默认情况下不包括在内(没关系!),所以它不会编译。

Had almost the same issue: tried to build module with dependency on another one which was changed.有几乎相同的问题:尝试构建依赖于另一个已更改模块的模块。 In first module were calls to methods in second module, and these methods were not visible.在第一个模块中调用了第二个模块中的方法,而这些方法是不可见的。 Solution was to delete folders "build" and "out" in second module, rebuild it and build first module.解决方案是删除第二个模块中的“build”和“out”文件夹,重建它并构建第一个模块。

Got the similar thing, but that's only because I turned on parallel build (org.gradle.parallel=true in ~/.gradle.properties).得到了类似的东西,但这只是因为我打开了并行构建(org.gradle.parallel=true in ~/.gradle.properties)。 Weird though.虽然很奇怪。

Error I got: error: package com.xxx.yyy does not exist error: cannot find symbol我得到的错误:错误:包 com.xxx.yyy 不存在错误:找不到符号

Once I set org.gradle.parallel=false in ~/.gradle.properties, the world starts to seem normal again.一旦我在 ~/.gradle.properties 中设置 org.gradle.parallel=false,世界又开始看起来正常了。

I get this error all the time and even though your problem is likely related to the previous answers, for me this is usually just local caching issues that gets solved by running gradle clean .我一直收到此错误,即使您的问题可能与之前的答案有关,但对我来说,这通常只是本地缓存问题,可以通过运行gradle clean来解决。

Can't hurt to try it.试试吧。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM