首页 文章

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

提问于
浏览
1

我使用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:error:module not发现: 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()

    }
}

编辑

好吧,我做了改动,现在我在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安装怎么样?

1 回答

  • 5

    问题是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)的部分 . 就个人而言,我倾向于不修改这些任务,因为测试通常需要大量的反思,这反过来需要很多 --add-opens 参数 . 如果你发现's not true (haven' t试了一段时间)或者有更好的方法,请告诉我 .

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

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

    如果你想在Gradle中观察有关Jigsaw支持的更新,他们在GitHub上维护an epic .


    另外,要包含@nullpointer commented,您应该使用其清单中包含 Automatic-Module-Name 条目的Guava版本 . 如果没有此条目(与 module-info 结合使用),模块的名称将受 jar 文件名称的限制;这可能会意外地改变 . 换句话说, Automatic-Module-Name 条目就自动模块的名称提供了更好的 Contract . Guava添加此条目的第一个版本是23.2

    更改日志为Guava添加了JPMS模块名称com.google.common . ...

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

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

    ModuleFinder.of(Path...)的Javadoc

相关问题