【问题标题】:Gradle error with lack of mainClassName property in gradle buildgradle build 中缺少 mainClassName 属性的 Gradle 错误
【发布时间】:2016-04-28 14:22:38
【问题描述】:

我有两个子项目的 graddle 配置,当我想要 build 项目时会抛出以下错误:

Executing external task 'build'...
:core:compileJava
:core:processResources UP-TO-DATE
:core:classes
:core:jar
:core:startScripts FAILED

FAILURE: Build failed with an exception.

* What went wrong:
A problem was found with the configuration of task ':core:startScripts'.
> No value has been specified for property 'mainClassName'.

我的配置: ROOT - build.gradle:

subprojects {

    apply plugin: 'java'
    apply plugin: 'application'

    group = 'pl.morecraft.dev.morepianer'

    repositories {
        mavenLocal()
        mavenCentral()
    }

    run {
        main = project.getProperty('mainClassName')
    }

    jar {
        manifest {
            attributes 'Implementation-Title': project.getProperty('name'),
                    'Implementation-Version': project.getProperty('version'),
                    'Main-Class': project.getProperty('mainClassName')
        }
    }

}

task copyJars(type: Copy, dependsOn: subprojects.jar) {
    from(subprojects.jar)
    into project.file('/jars')
}

ROOT - setting.gradle:

include 'app'
include 'core'

应用项目 - build.gradle:

EMPTY

核心项目 - build.gradle:

dependencies {
    compile 'com.google.dagger:dagger:2.4'
    compile 'com.google.dagger:dagger-compiler:2.4'
}

和两个子项目(类似)- gradle.properties:

version = 0.1-SNAPSHOT
name = MorePianer Core Lib
mainClassName = pl.morecraft.dev.morepianer.core.Core

我尝试在很多地方添加 mainClassName 属性,但它不起作用。这更奇怪,它在几天前就这样工作了。我是第一次使用 gradle,我正在考虑切换回 maven。

【问题讨论】:

    标签: java gradle


    【解决方案1】:

    application 插件在捆绑应用程序时需要知道主类。

    在您的情况下,您为每个子项目应用 application 插件,而不为每个子项目指定主类。

    我遇到了同样的问题,并通过将“mainClassName”指定为与apply plugin: 'application' 相同的级别来解决它:

    apply plugin: 'application'
    mainClassName = 'com.something.MyMainClass'
    

    如果您想在 gradle.properties 文件中指定它,您可能必须将其写为:projectName.mainClassName = ..

    【讨论】:

    • 我尝试在属性中添加:apply plugin: 'application' mainClassName = 'pl.morecraft.dev.morepianer.core.Core'projectName.mainClassName,但不幸的是没有任何帮助:/
    • 您是否尝试将 mainClassName 行放在构建文件中而不是属性文件中?
    • 对于 Kotlin DSL,在 plugins { application /* ... */ } 中指定 application 插件时,还要配置:application { mainClassName = "com.something.MyMainClass" }
    • 我刚刚从build.gradle 中删除了这一行(我正在使用IntelliJ)mainClassName = 'MyProjectName'
    【解决方案2】:

    尝试创建而不是设置 mainClassName

    task run(type: JavaExec, dependsOn: classes) {
        main = 'com.something.MyMainClass'
        classpath = sourceSets.main.runtimeClasspath
    }
    

    请看Gradle fails when executes run task for scala

    【讨论】:

    • 谢谢!这在 gradle 4.4 版和 SpringBoot 1.4.0 中起到了作用
    • 这行得通,但我不能使用名称“run”,所以我使用了“customRun”。 With "run": "Cannot add task 'run' as task with the name already exists."
    【解决方案3】:

    每当我们将 gradle 脚本与 application 插件绑定时,Gradle 都希望我们指出应用程序的起点。这是必要的,因为 Gradle 将使用给定位置作为入口点开始捆绑您的应用程序(jar/zip)。
    抛出此错误仅仅是因为 Gradle 知道您想要捆绑您的应用程序,但不知道从哪里开始捆绑过程。

    【讨论】:

      【解决方案4】:

      可以将mainClassName 指定为项目扩展属性:

      ext {
          mainClassName = 'com.something.MyMainClass'
      }
      

      【讨论】:

        【解决方案5】:

        我在我的项目中遇到了同样的问题,并通过从 gradle.properties 中排除来解决它:

        #ogr.gradle.configurationdemand = true
        

        【讨论】:

        • 建议不正确。这与问题完全无关。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-17
        • 2019-08-31
        相关资源
        最近更新 更多