【问题标题】:Why does gradle init generate variable mainClass, while mainClassName is needed?为什么gradle init生成变量mainClass,而需要mainClassName?
【发布时间】:2021-02-07 16:31:44
【问题描述】:

当我使用 Gradle 6.7.1 运行 gradle init 时,会构建一个包含 mainClass 变量的 gradle.build 文件。然而这需要mainClassName,如果使用mainClass,它会失败。请参阅下面的详细信息。

为什么gradle init 会生成不可用的代码?

gradle.build的相关部分:

application {
    // Define the main class for the application.
    mainClassName = 'com.mycompany.MyFileKt'
}

jar {
    manifest {
        attributes 'Main-Class': mainClassName
    }
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
}

如果我们将两个出现的mainClassName 替换为mainClass,则会出现错误消息。

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/joshua/dev/multicloud/app/build.gradle' line: 47

* What went wrong:
A problem occurred evaluating project ':app'.
> Could not get unknown property 'mainClass' for object of type org.gradle.api.java.archives.internal.DefaultManifest.

【问题讨论】:

    标签: kotlin gradle jar


    【解决方案1】:

    “生成不可用的代码”是什么意思? jar { ... } 部分是由 gradle init 生成的吗?在我看来,它就像是从旧脚本中复制粘贴的。 application 对象的 mainClassName 属性已弃用,应改为使用 mainClass。不同之处在于它不能再作为根级属性访问。

    以下应该有效:

    application {
        // Define the main class for the application.
        mainClass = 'com.mycompany.MyFileKt'
    }
    
    jar {
        manifest {
            attributes 'Main-Class': application.mainClass
        }
        from {
            configurations.compile.collect {
                it.isDirectory() ? it : zipTree(it)
            }
        }
    }
    

    【讨论】:

    • configurations.compile 部分仍然是错误的(它应该使用runtimeClasspath)。
    • @BjørnVester 你说得对,compile 也已被弃用,但在 6.7.1 中它仍然有效。
    • 谢谢。我在上面添加了错误消息。我正在使用 6.7.1,但也尝试过 6.8.2。感谢您提供有关compile 的提示。我将转移到implementationmainClass 之前使用application. 可以解决问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    相关资源
    最近更新 更多