无法为org.springframework.boot.gradle.dsl.SpringBootExtension类型的对象设置未知属性“ mainClass”

Sup*_*oks 5 java spring gradle spring-boot

我有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 {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

和模块的build.gradle文件app,在这里我有一个配置要运行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"
    }
}
Run Code Online (Sandbox Code Playgroud)

当构建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.
Run Code Online (Sandbox Code Playgroud)

如何设置启动mainClass类?

leo*_*leo 6

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

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


ati*_*mpi 5

配置主类

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

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

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

另外,可以使用mainClassNameSpring Boot DSL 的属性在项目范围内配置主类名称:

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

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

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

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

bootJar {
    manifest {
        attributes 'Start-Class': 'com.example.ExampleApplication'
    }
}
Run Code Online (Sandbox Code Playgroud)

弗朗西斯科


Mik*_*keF 1

我按照没有声明的Spring Restful Service教程进行操作,并且它有效。尝试删除该mainClass声明。

\n
\n

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

\n

它收集类路径上的所有 jar 并构建一个可运行的“\xc3\xbcber-jar”,这使得执行和传输服务更加方便。

\n

它搜索 public static void main() 方法来标记为可运行类。

\n

它提供了一个内置的依赖解析器,用于设置版本号以匹配 Spring Boot 依赖项。您可以覆盖您想要的任何版本,但它将默认为 Boot\xe2\x80\x99s 选择的版本集。

\n
\n