25
votes

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

6

6 Answers

40
votes

If you are building multi-module project, and including a module that is not the springboot project, then you should put this on gradle.build which in the non-springboot module.

bootJar {
    enabled = false
}

jar {
    enabled = true
}
19
votes

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

So, you can configure it. Pls try:

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

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

3
votes

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

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

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

0
votes

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 {
        ...
    }
}