Could not set unknown property 'mainClass' for object of type org.springframework.boot.gradle.dsl.SpringBootExtension

I have the main build.gradle file for the project

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${spring_boot_version}")
    }
}

def javaProjects = subprojects.findAll {
    it.name in ["common", "core", "web", "app"]
}

allprojects {
    apply plugin: 'idea'
    apply plugin: 'eclipse'

    repositories {
        jcenter()
    }
}

idea {
    project {
        jdkName = "1.8"
        languageLevel = "1.8"
        vcs = "Git"
    }
}

configure(javaProjects) {
    apply plugin: 'java'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'findbugs'

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    dependencies {
        ...
    }
}

and the build.gradle file for the app module, where I have a configuration to run gradle

apply plugin: 'org.springframework.boot'

dependencies {
    ...
}

springBoot {
    mainClass = "com.web.WebApplication"
}

jar {
    manifest {
        attributes 'Implementation-Title': "Rest-Web-Services",
                'Implementation-Version': "1.0",
                'Main-Class': "com.web.WebApplication"
    }
}

when build gradle throws

FAILURE: Build failed with an exception.

* Where:
Build file '...\REST-Web-Services\app\build.gradle' line: 28

* What went wrong:
A problem occurred evaluating project ':app'.
> Could not set unknown property 'mainClass' for object of type org.springframework.boot.gradle.dsl.SpringBootExtension.

How to set the startup mainClass class?


Solution 1:

Configuring the main class

By default, the executable archive’s main class will be configured automatically by looking for a class with a public static void main(String[]) method in directories on the task’s classpath.

The main class can also be configured explicitly using the task’s mainClassName property:

bootJar {
    mainClassName = 'com.example.ExampleApplication'
}

Alternatively, the main class name can be configured project-wide using the mainClassName property of the Spring Boot DSL:

springBoot {
    mainClassName = 'com.example.ExampleApplication'
}

If the application plugin has been applied its mainClassName project property can be used for the same purpose:

mainClassName = 'com.example.ExampleApplication'

Lastly, the Start-Class attribute can be configured on the task’s manifest:

bootJar {
    manifest {
        attributes 'Start-Class': 'com.example.ExampleApplication'
    }
}

Refrance

Solution 2:

Basically, this is about spring-boot gradle plugin:

  • bevore verion springboot2, it useed mainClass property,
  • since v2.0.0.RELEASE, it changed to use mainClassName.

Solution 3:

I followed the Spring Restful Service tutorial that does not have the declaration,and it worked. Try removing the mainClass declaration.

The Spring Boot gradle plugin provides many convenient features:

It collects all the jars on the classpath and builds a single, runnable "über-jar", which makes it more convenient to execute and transport your service.

It searches for the public static void main() method to flag as a runnable class.

It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.