简体   繁体   中英

Gradle error with lack of mainClassName property in gradle build

I have graddle configuration with two subprojects and when I want do build the project the following error is thrown:

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'.

My configuration: 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

I've tried to add the mainClassName property in many places, but it does not work. This is even stranger, that It worked a couple days ago as it is. I'm using gradle for first time and I'm thinking about switching back to maven.

The application plugin needs to know the main class for when it bundles the application.

In your case, you apply the application plugin for each subproject without specifying the main class for each of those subprojects.

I had the same problem and fixed it by specifying "mainClassName" at the same level as apply plugin: 'application' :

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

If you want to specify it in the gradle.properties file you might have to write it as : projectName.mainClassName = ..

Instead of setting up mainClassName try to create

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

Please look at Gradle fails when executes run task for scala

Whenever we bind a gradle script with the application plugin, Gradle expects us to point out the starting point of the application. This is neccessary because, Gradle will start bundling your application (jar/zip) using the given location as the entry point.
This error is thrown simply because Gradle knows that you want to bundle your application but is clueless about where to start the bundling process.

One can specify the mainClassName as a project extended property:

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

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

#ogr.gradle.configurationdemand = true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM