【问题标题】:Could not set unknown property 'mainClass' for object of type org.springframework.boot.gradle.dsl.SpringBootExtension无法为 org.springframework.boot.gradle.dsl.SpringBootExtension 类型的对象设置未知属性“mainClass”
【发布时间】:2018-09-30 15:11:41
【问题描述】:

我有该项目的主要build.gradle 文件

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

app 模块的build.gradle 文件,我有一个运行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"
    }
}

当构建 gradle 抛出时

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.

如何设置启动mainClass类?

【问题讨论】:

  • 我在使用 Spring 示例代码 here. 时遇到了同样的问题

标签: java spring spring-boot gradle


【解决方案1】:

我遵循了没有声明的 Spring Restful Service tutorial,并且它有效。尝试删除 mainClass 声明。

Spring Boot gradle 插件提供了许多方便的功能:

它收集类路径上的所有 jar,并构建一个可运行的“über-jar”,从而更方便地执行和传输您的服务。

它搜索公共静态 void main() 方法以标记为可运行类。

它提供了一个内置的依赖解析器,可以设置版本号以匹配 Spring Boot 依赖。你可以覆盖任何你想要的版本,但它会默认为 Boot 选择的一组版本。

【讨论】:

    【解决方案2】:

    配置主类

    默认情况下,可执行存档的主类将通过在任务类路径的目录中查找具有public static void main(String[]) 方法的类来自动配置。

    也可以使用任务的mainClassName 属性显式配置主类:

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

    或者,可以使用 Spring Boot DSL 的 mainClassName 属性在项目范围内配置主类名称:

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

    如果应用程序插件已应用,则其 mainClassName 项目属性可用于相同目的:

    mainClassName = 'com.example.ExampleApplication'
    

    最后,Start-Class 属性可以在任务的清单上配置:

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

    Refrance

    【讨论】:

      【解决方案3】:

      基本上,这是关于spring-boot gradle plugin

      • bevore verion springboot2,它使用了mainClass属性,
      • v2.0.0.RELEASE改用mainClassName

      【讨论】:

        【解决方案4】:

        TL;DR

        在 Gradle 构建脚本中定义以下块:

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

        错误

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

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

        我有类似的问题。

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

        Spring 再次改变了如何提供主类的约定,因为@leo 提供了答案,因此我将对其进行扩展。现在根据 springframework.boot 插件的版本,您必须按以下方式定义主类值(从最新到最旧排序):


        SpringFramework 2.4.0 and newer

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

        Release/Migration notes for 2.4.0


        SpringFramework 2.3.12 and older

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

        Release/Migration notes for 2.0.0


        SpringFramework 1.5.22.RELEASE and older(希望你不再使用它)

        bootRepackage {
          mainClass = 'demo.Application'
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-02
          • 2022-08-14
          • 1970-01-01
          • 2020-06-15
          • 2020-01-02
          • 2020-04-15
          相关资源
          最近更新 更多