Gradle构建中缺少mainClassName属性的Gradle错误

时间:2016-04-28 14:22:39

标签: java gradle

我有两个子项目的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'

APP PROJECT - build.gradle:

EMPTY

CORE PROJECT - build.gradle:

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

AND BOTH SUBPROJECTS(SIMILAR) - gradle.properties:

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

我尝试在很多地方添加mainClassName属性,但它不起作用。这甚至是陌生的,它在几天前就已经发挥作用了。我第一次使用gradle,我正在考虑切换回maven。

5 个答案:

答案 0 :(得分:46)

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

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

我遇到了同样的问题并通过指定" mainClassName"来修复它。与apply plugin: 'application'处于同一级别:

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

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

答案 1 :(得分:4)

而不是设置mainClassName尝试创建

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

请查看Gradle fails when executes run task for scala

答案 2 :(得分:0)

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

#ogr.gradle.configurationdemand = true

答案 3 :(得分:0)

每当我们将Gradle脚本与 application 插件绑定时,Gradle都希望我们指出应用程序的起点。这是必需的,因为Gradle将使用给定的位置作为入口点开始捆绑您的应用程序(jar / zip)。
仅仅因为 Gradle 知道您要捆绑应用程序,却对从何处开始捆绑过程一无所知,因此引发了该错误。

答案 4 :(得分:0)

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

ext {
    mainClassName = 'com.something.MyMainClass'
}
相关问题