Rashik
Rashik

Reputation: 1154

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

I am building a RESTful web service with Spring by following this guide. I am using gradle 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

Upvotes: 71

Views: 129806

Answers (11)

Dmitrii
Dmitrii

Reputation: 43

The best answer for multi-module projects would be https://github.com/gradle/gradle/issues/12789#issuecomment-612638399

quote: I don’t think this is a gradle bug (I’m not a maintainer also).

You are applying the spring boot plugin at root level by doing.

Since you are doing this, the spring boot project expects that your root project is a sb app requiring a mainClassName For the bootJar.

You should only apply the spring boot project to the projects that are SB apps. In this case, your products-service.

The fact that you disable bootJar in the root project is a smell that shouldn’t be applied there.

Upvotes: 0

Zaziro
Zaziro

Reputation: 541

So, you can configure it. Please, try:

apply plugin: 'application'
mainClassName = 'com.example.WebApplication'

Upvotes: 14

rpajaziti
rpajaziti

Reputation: 181

I have multi module project, and for me it was adding

bootJar { enabled = false }

to the main build.gradle, since there is no main method there.

Upvotes: 1

hsrv
hsrv

Reputation: 1570

According to https://spring.io/guides/gs/multi-module/, the library's build.gradle (that uses Spring) should use Spring Boot Plugin without applying it (apply false):

plugins {
    id 'org.springframework.boot' version '3.0.0' apply false
    id 'io.spring.dependency-management' version '1.1.0'
    id 'java'
}

That eliminates the need for:

bootJar {
    enabled = false
}

jar {
    enabled = true
}

Also, for the Spring Boot application that uses the library, we need to specify scanBasePackages with the library's group:

package com.example.multimodule.application;

import com.example.multimodule.service.MyService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication(scanBasePackages = "com.example.multimodule")
@RestController
public class DemoApplication {

  private final MyService myService;

  public DemoApplication(MyService myService) {
    this.myService = myService;
  }

  @GetMapping("/")
  public String home() {
    return myService.message();
  }

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

Upvotes: 11

Haim Raman
Haim Raman

Reputation: 12043

If we're using the Spring Boot Gradle plugin and Kotlin you can use the snippet below:

plugins {
    id("org.springframework.boot") version "2.6.7"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.5.31"
    kotlin("plugin.spring") version "1.5.31"
}

...

springBoot {
    mainClass.value("com.mycompany.MyApplication")
}

Upvotes: 1

RenatoIvancic
RenatoIvancic

Reputation: 2092

With the org.springframework.boot Gradle plugin version 2.6.2.

plugins {
  id 'org.springframework.boot' version '2.4.0'
}

I had similar issue.

Execution failed for task ':bootJar'. Failed to calculate the value of task ':bootJar' property 'mainClass'. Main class name has not been configured and it could not be resolved

Now depending on the version of the springframework boot plugin you have to define the main class value on following way.

SpringFramework 2.3.12 and older

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

SpringFramework 2.4.0 and newer

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

Release notes mentioning change of the property

Upvotes: 10

Mephisto Lynn
Mephisto Lynn

Reputation: 1156

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
}

Upvotes: 114

aakoch
aakoch

Reputation: 1174

I was trying to follow the instructions at https://spring.io/guides/gs/spring-boot/ that has a CommandLineRunner in the application. The code inside the CommandLineRunner would only run if I wrapped that bean and the main method in the class. I could get tests to run but not via the command line. I was able to get around this by using a companion object:

@SpringBootApplication
class TestApplication {

    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            runApplication<TestApplication>(*args)
        }
    }

    @Bean
    fun commandLineRunner(ctx: ApplicationContext): CommandLineRunner {
        ...
    }
}

Upvotes: 0

Alkeno
Alkeno

Reputation: 69

As @Zaziro mentioned, it's possible to define a configuration.

This site describes the different kinds of configuration very well: https://www.baeldung.com/spring-boot-main-class#gradle

IF YOU ARE USING KOTLIN:

Be aware that your class name changes. You need to append your mainClassName with "Kt".

example: "com.example.Application" -> "com.example.ApplicationKt"

Upvotes: 2

xilef
xilef

Reputation: 2397

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)
}

Upvotes: 41

no7dw
no7dw

Reputation: 431

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 :-)

Upvotes: 5

Related Questions