首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java 10级项目:未找到自动模块

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

Stack Overflow用户
提问于 2018-08-19 11:20:07
回答 1查看 7.9K关注 0票数 7

我使用intelliJ创建了一个java 10项目,使用gradle。我在其中复制了一些东西(一些使用番石榴和javaFx库的“javaFx”类和一个个人build.gradle文件)。我还在src/main/java中添加了一个模块info.java文件,其中包含以下内容:

代码语言:javascript
复制
module biblio5.main {
    requires javafx.graphics;
    requires javafx.controls;
    requires javafx.base;
    requires guava;
}

其中grava是一个自动模块。

以下是build.gradle的相关部分:

代码语言:javascript
复制
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile 'com.google.guava:guava:23.0'
}

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

13:12:46:执行任务‘编译task’。 任务:编译C:\Users\lolve\Documents\gradle_java\biblio5\src\main\java\module-info.java:5:失败的错误:未找到模块:番石榴需要番石榴;^1错误

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

谢谢

ps:这是整个build.gradle:

代码语言:javascript
复制
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“代替了番石榴,但是我仍然得到了这个错误:

测试于14:20开始. 14:20:14:执行任务‘检查’. 任务:编译C:\Users\lolve\Documents\gradle_java\biblio5\src\main\java\module-info.java:5:失败的com.google.common错误:未找到模块:com.google.common需要com.google.common;^1错误

我将intelliJ中的gradle (推荐的默认选项是“默认gradle包装器”)更改为本地gradle (v4.9),但没有任何效果。你所说的“兼容java”是什么意思?如何尝试安装java 9呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-19 13:43:13

更新: Gradle 6.4添加了对Jigsaw模块的基本支持。请参阅文档中的https://docs.gradle.org/current/samples/sample_java_modules_multi_project.html (该文档还链接到其他相关文档)。请注意,在这个答案中链接到的BuildingJava9Modules文章自从这个答案发布以来已经发生了很大的变化。

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

如果您想在项目中使用Jigsaw模块,您应该阅读构建Java 9模块。您的场景(@null指针提及 )最好由链接文档的本节来处理。需要将以下内容添加到build.gradle文件中:

代码语言:javascript
复制
ext.moduleName = 'your.module'

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

它们还包含修改compileTestJava任务(这里)和test任务(这里)的部分。就我个人而言,我倾向于不修改这些任务,因为测试通常需要大量的反射,而这又需要大量的--add-opens参数。如果你发现这不是真的(已经有一段时间没试过了)或者有更好的方法,请告诉我。

如果您的Gradle项目是一个application,您还需要阅读涵盖runassemble任务的节段

有一个试验性的Gradle插件为您做了所有这些:experimental-jigsaw。不过,这个插件是有限的,GitHub上有一个名为chainsaw的分支,它增加了更多的功能。注意:我不知道这两个插件是如何维护的。

另一个Gradle插件可用:分级模块插件

如果您想查看关于Gradle中Jigsaw的更新,那么它们将在史诗上维护GitHub。

此外,要包含什么@null指针评论,您应该使用一个在其清单中包含一个Automatic-Module-Name条目的Guava版本。如果没有这个条目(结合没有module-info),模块的名称将取决于jar文件的名称;这可能会意外地改变。换句话说,Automatic-Module-Name条目为自动模块的名称提供了更好的契约。Guava添加这个条目的第一个版本是23.2

Changelog

  • 为番石榴添加了日圆模块名com.google.common

..。

然而,最近的版本(撰写此答案时)是26.0

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

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51917106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档