java 10 gradle 项目 : automatic module not found

标签 java gradle intellij-idea guava java-module

我使用 gradle 创建了一个带有 intelliJ 的 java 10 项目。
我将一些东西复制到其中(使用库 guava 和 javaFx 的一些“AppFx”类,以及个人 build.gradle 文件)。
我还在 src/main/java 中添加了一个 module-info.java 文件,其中包含以下内容:

module biblio5.main {
    requires javafx.graphics;
    requires javafx.controls;
    requires javafx.base;
    requires guava;
}

其中 grava 是一个自动模块。

这是 build.gradle 的相关部分:
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile 'com.google.guava:guava:23.0'
}

intelliJ 可以编译项目(使用类似锤子的图标),但是当我从 intelliJ 运行 compileJava gradle 任务时,出现错误:

13:12:46: Executing task 'compileJava'...

Task :compileJava FAILED C:\Users\lolve\Documents\gradle_java\biblio5\src\main\java\module-info.java:5: error: module not found: guava requires guava; ^ 1 error



我在网上花了很多时间,但没有找到答案。

谢谢你

ps:这是整个 build.gradle:
buildscript {
    dependencies {
        classpath group: 'de.dynamicfiles.projects.gradle.plugins', name: 'javafx-gradle-plugin', version: '8.8.2'
        classpath 'eu.appsatori:gradle-fatjar-plugin:0.3'
    }
    repositories {

        maven {url "https://mvnrepository.com/artifact/de.dynamicfiles.projects.gradle.plugins/javafx-gradle-plugin"}
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
        jcenter()
    }
}


plugins {
    id 'java'
    id 'application'
    id 'edu.sc.seis.launch4j' version '2.4.4'
}
apply plugin: 'javafx-gradle-plugin'
apply plugin: 'eu.appsatori.fatjar'

group 'lorry'
version '1'

sourceCompatibility = 1.10

repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    maven {url "https://mvnrepository.com/artifact/de.dynamicfiles.projects.gradle.plugins/javafx-gradle-plugin"}
    jcenter()
    mavenCentral()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }

}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile 'com.google.guava:guava:23.0'
}

//********************************************************************************************
launch4j {
    outfile='bibliotek-v3.exe'
    mainClassName = 'lorry.AppFx'
    icon = "${projectDir}\\icons\\hands2.ico"
    copyConfigurable = project.tasks.fatJar.outputs.files
    //jar = "lib/${project.tasks.fatJar.archiveName}"
    //headerType = "console"
    jar = "${buildDir}\\productFatJar\\fat.jar"
}

jar {
    baseName = 'executable3'
    version =  ''
    manifest {
        attributes(
                'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
                'Main-Class': 'lorry.AppFx'
        )
    }
}

task copyExecutable(type: Copy) {
    from file("${buildDir}\\launch4j\\bibliotek-v3.exe")
    into file("c:\\Users\\lolve\\Documents\\gradle_java\\produits")
}

task copyJar(type: Copy) {
    from file("${buildDir}\\jfx\\app\\bibliotek-v3.jar")
    into file("c:\\Users\\lolve\\Documents\\gradle_java\\produits")
}

task copyFatJar(type: Copy) {
    from file("${buildDir}\\productFatJar\\fat.jar")
    into file("c:\\Users\\lolve\\Documents\\gradle_java\\produits")
}

createExe.doLast{
    tasks.copyExecutable.execute()

}

task createJar(){
    doLast{
        tasks.jfxJar.execute()
        tasks.jfxNative.execute()
        tasks.copyJar.execute()
    }
}



jfx {
    jfxMainAppJarName = "bibliotek-v3.jar"
    // minimal requirement for jfxJar-task
    mainClass = 'lorry.AppFx'

    // minimal requirement for jfxNative-task
    vendor = 'lolveley'
}

fatJar {
    destinationDir=file("${buildDir}\\productFatJar")
    archiveName="fat.jar"
    manifest {
        attributes(
                'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
                'Main-Class': 'lorry.AppFx'
        )
    }
}

task createFats(){
    doLast{
        tasks.fatJar.execute()
        tasks.copyFatJar.execute()

        tasks.createExe.execute()

    }
}

编辑

好吧,我进行了更改,现在我在 module-info.java 中有“com.google.commons”而不是 Guava ,但我仍然收到此错误:

Testing started at 14:20 ... 14:20:14: Executing task 'check'...

Task :compileJava FAILED C:\Users\lolve\Documents\gradle_java\biblio5\src\main\java\module-info.java:5: error: module not found: com.google.common requires com.google.common; ^ 1 error



我将 intelliJ 中的 gradle (默认选项 - 推荐 - 是“默认 gradle wrapper”)更改为我的本地 gradle(v4.9),但没有任何效果。
“与java兼容”是什么意思?尝试安装 java 9 怎么样?

最佳答案

更新: Gradle 6.4 添加了对 Jigsaw 模块的基本支持。见 this sample在文档中(还链接到其他相关文档)。请注意,自发布此答案以来,此答案中链接到的 Building Java 9 Modules 文章已发生重大变化。

问题是 Gradle 仍然(截至 4.10-rc-2)没有对 Jigsaw 模块的一流支持。所有任务在执行时都将使用类路径,而不是模块路径。这显然会在尝试创建模块化库/应用程序(使用 module-info.java )时引起问题。

如果你想在你的项目中使用 Jigsaw 模块,你应该阅读 Building Java 9 Modules .您的场景,如@nullpointer mentions , 最好由 this section 覆盖的链接文档。要点是将以下内容添加到您的 build.gradle文件:

ext.moduleName = 'your.module'

compileJava {
    inputs.property('moduleName', moduleName)
    doFirst {
        options.compilerArgs = [
            '--module-path', classpath.asPath
        ]
        classpath = files()
    }
}

他们也有修改 compileTestJava 的部分。任务 ( here ) 和 test任务(here)。就个人而言,我倾向于不修改这些任务,因为测试通常需要大量反射(reflection),这反过来又需要大量 --add-opens论据。如果您发现这不是真的(有一段时间没有尝试过)或者有更好的方法,请告诉我。

如果您的 Gradle 项目是 application您还想阅读the section覆盖runassemble任务。

有一个实验性的 Gradle 插件可以为您完成所有这些工作: experimental-jigsaw .但是插件是有限的,在 GitHub 上有一个 fork,名为 chainsaw ,增加了更多的功能。注意:我不知道这两个插件的维护情况。

另一个 Gradle 插件可用:Gradle Modules Plugin .

如果您想查看 Gradle 中有关 Jigsaw 支持的更新,他们会维护 an epic在 GitHub 上。

另外,要包括 @nullpointer commented ,您应该使用包含 Automatic-Module-Name 的 Guava 版本。进入其 list 。如果没有这个条目(与没有 module-info 结合),模块的名称将受制于 jar 的名称。文件;这可能会出乎意料地改变。换句话说,Automatic-Module-Name entry 可以就自动模块的名称签订更好的契约(Contract)。 Guava 添加此条目的第一个版本是 23.2 :

Changelog

  • Added JPMS module name com.google.common for Guava.

...



但是,最新版本(截至撰写此答案时)是 26.0 .

有关自动模块的更多信息,请参见:
  • ModuleFinder.of(Path...) 的 Javadoc 中
  • this section模块系统状态
  • this Stack Overflow question
  • 关于java 10 gradle 项目 : automatic module not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51917106/

    相关文章:

    java - Intellij Idea 13 UI Designer 和自动 Gradle 构建

    java - 为什么 Intellij 在 pom.xml 中找不到任何 Maven 依赖项?

    java - spring security不允许用户登录,不显示任何错误

    java - Wildfly 9 忽略 web.xml 条目

    java - java中如何将子类的输入提供给父类?

    android - 尝试将本地 android 库模块发布到 Artifactory

    java - 如何将已解析的依赖项复制到新位置,根据依赖项详细信息动态创建目录

    Java转换字符串,具有毫秒至今的对象

    java - JSP 找不到 i18n 的默认包

    Goland:运行任意代码质量工具,获得可点击的结果