java 10 gradle 项目:未找到自动模块

java 10 gradle project : automatic module not found

我使用 gradle 使用 intelliJ 创建了一个 java 10 项目。 我将一些东西复制到其中(一些 "AppFx" class 使用库 guava 和 javaFx,以及个人 build.gradle 文件)。 我还在 src/main/java 中添加了一个模块-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()

    }
}

编辑

好吧,我进行了更改,现在我在模块-info.java 中使用 "com.google.commons" 而不是番石榴,但我仍然收到此错误:

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(默认选项 - 推荐 - 是 "default gradle wrapper")更改为我本地的 gradle (v4.9),但没有任何效果。 "compatible with java" 是什么意思?尝试安装 java 9 怎么样?

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


问题是Gradle仍然(从 4.10-rc-2 开始)没有对 Jigsaw 模块的 first-class 支持。所有任务在执行时都将使用 classpath,而不是 modulepath。这显然会在尝试创建模块化 library/application(使用 module-info.java)时引起问题。

如果您想在项目中使用 Jigsaw 模块,您应该阅读链接文档的 Building Java 9 Modules. Your scenario, as @nullpointer , is best covered by this section。要点是将以下内容添加到您的 build.gradle 文件中:

ext.moduleName = 'your.module'

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

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

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

有一个实验性 Gradle 插件可以为您完成所有这些工作:experimental-jigsaw. The plugin is limited, however, and there is a fork on GitHub, named chainsaw,它添加了更多功能。 注意:我不知道这两个插件是如何维护的。

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

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


此外,包括@nullpointer , you should be using a version of Guava that includes an Automatic-Module-Name entry in its manifest. Without this entry (combined with no module-info) the name of the module is subject to the name of the jar file; which may change unexpectedly. In other words, the Automatic-Module-Name entry makes for a better contract regarding the name of an automatic module. The first version that Guava added this entry is 23.2:

Changelog

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

...

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

可以找到更多关于自动模块的信息: