我使用gradle使用intelliJ创建了一个Java 10项目。
我将一些东西复制到其中(使用了 Guava 和javaFx库以及一个个人build.gradle文件的一些“AppFx”类)。
我还在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任务时,出现错误:



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

谢谢你

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 ,但是仍然出现此错误:



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

最佳答案

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

问题是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上有一个名为 chainsaw 的fork,它添加了更多功能。注意:我不知道两个插件的维护情况。

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

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

另外,要包括@nullpointer commented,您应该使用在 list 中包含Automatic-Module-Name条目的Guava版本。没有此条目(不带module-info),模块名称以jar文件的名称为准;可能会发生意料之外的变化。换句话说,Automatic-Module-Name条目为自动模块的名称提供了更好的约定。 Guava添加此条目的第一个版本是23.2:



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

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

ModuleFinder.of(Path...) 的Javadoc中的
  • 模块系统状态的
  • this section
  • this Stack Overflow question
  • 10-07 19:04
    查看更多