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

lol*_*ley 2 java intellij-idea gradle 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;
}
Run Code Online (Sandbox Code Playgroud)

其中grava是一个自动模块.

这是build.gradle的相关部分:

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile 'com.google.guava:guava:23.0'
}
Run Code Online (Sandbox Code Playgroud)

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

13:12:46:执行任务'compileJava'......

任务:compileJava FAILED C:\ Users\lolve\Documents\gradle_java\biblio5\src\main\java\module-info.java:5:错误:找不到模块:guava需要guava; ^ 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()

    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

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

测试于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包装器")更改为我的本地gradle(v4.9),但没有任何效果."兼容java"是什么意思?尝试使用java 9安装怎么样?

Sla*_*law 8

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

如果要在项目中使用Jigsaw模块,则应阅读构建Java 9模块.正如@nullpointer所提到的,您的方案最好由链接文档的这一部分介绍.要点是将以下内容添加到您的build.gradle文件中:

ext.moduleName = 'your.module'

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

它们还有用于修改compileTestJava任务(此处)和test任务(此处)的部分.就个人而言,我倾向于不修改这些任务,因为测试通常需要大量的反思,这反过来需要大量的--add-opens论据.如果你发现那不是真的(暂时没试过)或者有更好的方法,请告诉我.

如果您的Gradle项目是一个,application您还想阅读涵盖和任务的部分.runassemble

有一个实验性的Gradle插件可以为您完成所有这些:experimental-jigsaw.但是,该插件是有限的,GitHub上有一个名为fork的分叉chainsaw,它增加了更多功能.注意:我不知道插件是如何维护的.

如果你想在Gradle中观看关于Jigsaw支持的更新,他们会在GitHub上保留一个史诗.


另外,要包含@nullpointer 评论的内容,您应该使用Guava版本,其中包含Automatic-Module-Name清单中的条目.如果没有此条目(与否合并module-info),模块的名称将取决于jar文件的名称; 这可能会意外地改变.换句话说,该Automatic-Module-Name条目使得关于自动模块的名称的合同更好.Guava添加此条目的第一个版本是23.2:

更新日志

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

...

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

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

  • 只是添加到以上所有内容。关于使用库的更新版本的部分仍然有意义,因为显式添加的“Automatic-Module-Name:com.google.common”是将库用作自动模块的更清晰的契约。 (2认同)