【问题标题】:IntellijIDEA 2021.2.2 does not recognize classes imported from jar file to gradle moduleIntellij IDEA 2021.2.2 无法识别从 jar 文件导入到 gradle 模块的类
【发布时间】:2021-12-10 09:00:34
【问题描述】:

项目结构如下图所示:

tictactoe-server-infrastructure 模块有一个名为libs 的文件夹,其中包含两个带有一些库代码的 jar 文件。在build.gradle 模块的build.gradle 文件的dependencies 部分中,我有下一行implementation fileTree(include: ['*.jar'], dir: 'libs'),它允许gradle 识别该jar 文件中的类并在tictactoe-server-infrastructure 模块中使用它们的类。

当我想将tictactoe-server-infrastructure 模块作为对tictactoe-server-presentation 模块的依赖项添加时,我将implementation project(':tictactoe-server-infrastructure') 行放入build.gradle 文件的dependencies 块中,用于tictactoe-server-presentation 模块。

当我尝试构建一个项目时,它构建得很好,没有任何错误。

但是,一旦我尝试在 tictactoe-server-presentation 模块中使用该 jar 文件中的任何类,这些类意味着作为与 tictactoe-server-infrastructure 模块的依赖项添加,IDEA 似乎无法识别它们并且 gradle 将不再构建项目.

我已经尝试过使缓存无效并重新启动、重新导入 gradle 项目并删除 gradle 缓存。我还尝试将两个 jars 添加为全局库,并通过 IDEA 的项目结构菜单将它们添加到 tictactoe-server-presentation,但我想像以前一样完全从 gradle 中完成。

奇怪的是,在另一个具有较旧 gradle 版本的项目中,我使用了 compile 方法,并且它起作用了。

tictactoe-server-infrastructure build.gradle

plugins {
    id 'java'
}

group 'com.ttt'
version '0.1.0'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compileOnly 'org.projectlombok:lombok:1.18.20'
    annotationProcessor 'org.projectlombok:lombok:1.18.20'

    testCompileOnly 'org.projectlombok:lombok:1.18.20'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'

    implementation fileTree(include: ['*.jar'], dir: 'libs')
}
tictactoe-server-presentation build.gradle


plugins {
    id 'java'
}

group 'com.ttt'
version '0.1.0'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    implementation project(':tictactoe-server-infrastructure')

    compileOnly 'org.projectlombok:lombok:1.18.20'
    annotationProcessor 'org.projectlombok:lombok:1.18.20'

    testCompileOnly 'org.projectlombok:lombok:1.18.20'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
}

【问题讨论】:

    标签: java gradle intellij-idea


    【解决方案1】:

    在又一小时重新阅读 gradle 文档和其他一些文章后,我找到了答案

    我得出的结论是,我需要从这两个 jar 文件中分类才能在客户端类路径中可见(意思是 tictactoe-server-presentation 模块)。

    implementation 不提供执行此操作所需的传递性,这意味着该 jar 文件中的类将仅对 tictactoe-server-infrastructure 模块可见,而对依赖它的模块不可见。

    另一方面,apiElements 提供向客户端类路径公开类的传递性。

    tl;博士

    我要做的就是更改tictactoe-server-infrastructure build.gradle 文件:将implementation fileTree(include: ['*.jar'], dir: 'libs') 替换为 apiElements fileTree(dir: 'libs', include: ['*.jar'])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-29
      • 2017-12-14
      • 2015-07-28
      • 2015-05-22
      • 2015-04-03
      • 1970-01-01
      相关资源
      最近更新 更多