【问题标题】:java 10 gradle project : automatic module not foundjava 10 gradle项目:找不到自动模块
【发布时间】:2019-01-25 18:40:48
【问题描述】:

我使用 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:正在执行任务“compileJava”...

任务:compileJava FAILED C:\Users\lolve\Documents\gradle_java\biblio5\src\main\java\module-info.java:5: 错误:找不到模块:番石榴 需要番石榴; ^ 1 个错误

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

谢谢

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”而不是番石榴,但我仍然收到此错误:

测试开始于 14:20 ... 14:20:14:正在执行任务“检查”...

任务:compileJava FAILED C:\Users\lolve\Documents\gradle_java\biblio5\src\main\java\module-info.java:5: 错误:找不到模块:com.google.common 需要 com.google.common; ^ 1 个错误

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

【问题讨论】:

  • Gradle 在编译/运行时默认使用 classpath,而不是 modulepath。我相信这会导致您的问题。见Building Java 9 Modules。不幸的是,根据该链接,Gradle still 没有对 Jigsaw 模块的一流支持(截至 4.10-rc-2)。
  • 我的回答不好,我假设 gradle(如果更新)将与 Java9+ 模块化代码的模块路径兼容。仅通过上述文档的概述,this 应该是要研究的部分之一
  • 谢谢你,Slaw & nullpointer,添加短代码使它工作。我无法将答案标记为已接受,因为只有评论,但如果将其作为答案,我很快就会将其标记为已接受。

标签: java gradle intellij-idea guava java-module


【解决方案1】:

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


问题是 Gradle 仍然(从 4.10-rc-2 开始)没有对 Jigsaw 模块的一流支持。所有任务在执行时都将使用 classpath,而不是 modulepath。这显然会在尝试创建模块化库/应用程序(使用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) 的部分。就个人而言,我倾向于不修改这些任务,因为测试通常需要大量反思,而这反过来又需要大量 --add-opens 参数。如果您发现这不是真的(有一段时间没有尝试过)或者有更好的方法,请告诉我。

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

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

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

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


另外,要包含 @nullpointer commented,您应该使用在其清单中包含 Automatic-Module-Name 条目的 Guava 版本。没有这个条目(没有module-info),模块的名称以jar 文件的名称为准;这可能会出乎意料地改变。换句话说,Automatic-Module-Name 条目在自动模块名称方面做出了更好的约定。 Guava 添加此条目的第一个版本是23.2

更新日志

  • 为 Guava 添加了 JPMS 模块名称 com.google.common

...

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

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

【讨论】:

  • 只是添加到以上所有内容。关于使用库的更新版本的部分仍然有意义,因为明确添加的Automatic-Module-Name: com.google.common 是使用库作为自动模块的更清晰的合同。
猜你喜欢
  • 1970-01-01
  • 2019-01-20
  • 2018-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多