Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle build failed: Main class name has not been configured and it could not be resolved

Tags:

spring

gradle

I am building a RESTful web service with Spring by following this guide. I am using grable to build the app but my build is failing.

I used the "build gradle" command on a Windows 10 machine.

This is the gradle code:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.6.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'gs-rest-service'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

I am getting the following error:

Execution failed for task ':bootJar'.

Main class name has not been configured and it could not be resolved

like image 419
Rashik Avatar asked Jul 02 '19 23:07

Rashik


People also ask

What is gradle bootRun?

The Spring Boot gradle plugin provides the bootRun task that allows a developer to start the application in a “developer mode” without first building a JAR file and then starting this JAR file. Thus, it's a quick way to test the latest changes you made to the codebase.


4 Answers

When building a multi-module project which includes a module outside of the springboot project, then the module's build.gradle must contain:

bootJar {
    enabled = false
}

jar {
    enabled = true
}
like image 131
Mephisto Lynn Avatar answered Oct 06 '22 13:10

Mephisto Lynn


I ran into this error when I used kotlin and misplaced the main method.

Wrong:

@SpringBootApplication
class Application {
    fun main(args: Array<String>) {
        runApplication<Application>(*args)
    }
}

Fix (moved main out of Application):

@SpringBootApplication
class Application

fun main(args: Array<String>) {
    runApplication<Application>(*args)
}
like image 25
xilef Avatar answered Oct 06 '22 14:10

xilef


So, you can configure it. Pls try:

apply plugin: 'application'
mainClassName = 'com.example.WebApplication'
like image 11
Zaziro Avatar answered Oct 06 '22 13:10

Zaziro


in my case, (using Kotlin for writing springboot demo) it was caused by the src folder, the main class file did not locate under src/main folder. besides, I've clean everything up and restart IDE. As @Zaziro mentioned, I've also token a try, but without any luck. But there're articles mentioned config mainClassName,

and after comparing with src code in Github that different, it's not about any version of any package.

Wish u luck :-)

like image 5
no7dw Avatar answered Oct 06 '22 13:10

no7dw