Gradle 构建失败:主类名尚未配置且无法解析

Ras*_*hik 25 spring gradle

我正在按照指南使用 Spring 构建 RESTful Web 服务。我正在使用 grable 来构建应用程序,但我的构建失败了。

我在 Windows 10 机器上使用了“build gradle”命令。

这是gradle代码:

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')
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

任务 ':bootJar' 执行失败。

主类名未配置,无法解析

小智 40

如果你正在构建多模块项目,并且包含一个不是 springboot 项目的模块,那么你应该把它放在非 springboot 模块中的 gradle.build 上。

bootJar {
    enabled = false
}

jar {
    enabled = true
}
Run Code Online (Sandbox Code Playgroud)


xil*_*lef 19

当我使用 kotlin 并放错了main方法时,我遇到了这个错误。

错误的:

@SpringBootApplication
class Application {
    fun main(args: Array<String>) {
        runApplication<Application>(*args)
    }
}
Run Code Online (Sandbox Code Playgroud)

修复(移动main出来Application):

@SpringBootApplication
class Application

fun main(args: Array<String>) {
    runApplication<Application>(*args)
}
Run Code Online (Sandbox Code Playgroud)

  • 彻底救了我!我是 Kotlin 新手,也犯过同样的错误。 (2认同)

小智 9

所以,你可以配置它。请尝试:

apply plugin: 'application'
mainClassName = 'com.example.WebApplication'
Run Code Online (Sandbox Code Playgroud)


hsr*_*srv 9

根据https://spring.io/guides/gs/multi-module/,库build.gradle(使用 Spring)应该使用 Spring Boot 插件而不应用它(apply false):

plugins {
    id 'org.springframework.boot' version '3.0.0' apply false
    id 'io.spring.dependency-management' version '1.1.0'
    id 'java'
}
Run Code Online (Sandbox Code Playgroud)

这样就不需要:

bootJar {
    enabled = false
}

jar {
    enabled = true
}
Run Code Online (Sandbox Code Playgroud)

另外,对于使用该库的 Spring Boot 应用程序,我们需要指定scanBasePackages该库的组:

plugins {
    id 'org.springframework.boot' version '3.0.0' apply false
    id 'io.spring.dependency-management' version '1.1.0'
    id 'java'
}
Run Code Online (Sandbox Code Playgroud)


Ren*_*cic 7

使用org.springframework.boot Gradle 插件版本 2.6.2。

plugins {
  id 'org.springframework.boot' version '2.4.0'
}
Run Code Online (Sandbox Code Playgroud)

我有类似的问题。

任务“:bootJar”执行失败。无法计算任务“:bootJar”属性“mainClass”的值。主类名尚未配置,无法解析

现在,根据 springframework boot 插件的版本,您必须按以下方式定义主类值。

SpringFramework 2.3.12 及更早版本

springBoot {
  mainClassName = 'com.example.ExampleApplication'
}
Run Code Online (Sandbox Code Playgroud)

SpringFramework 2.4.0 及更高版本

springBoot {
  mainClass = 'com.example.ExampleApplication'
}
Run Code Online (Sandbox Code Playgroud)

提及属性变更的发行说明


no7*_*7dw 5

就我而言,(使用Kotlin 编写 springboot demo)这是由 src 文件夹引起的,主类文件没有位于 src/main 文件夹下。此外,我已经清理了所有内容并重新启动了 IDE。正如@Zaziro 提到的,我也尝试过,但没有任何运气。但有文章提到配置mainClassName

与 Github 中的 src 代码进行比较后发现不同之处在于,它与任何包的任何版本无关。

祝你好运:-)