首页 > 解决方案 > 使用 ./gradlew appRun 运行 Spring Boot 应用程序

问题描述

我将 Spring Boot 用于学校项目作为个人选择,但自动测试仪用于./gradlew appRun启动整个过程。

在他们使用 servlet 之前这很好,但是在迁移到 Spring Boot 之后,我在尝试这样做时遇到了几个异常。

Execution failed for task ':appRun'.
Could not get unknown property 'mainClass' for object of type org.springframework.boot.gradle.dsl.SpringBootExtension.

这是我目前的build.gradle.

buildscript {
    ext {
        springBootVersion = '2.1.0.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "gradle.plugin.org.akhikhl.gretty:gretty:2.0.0"
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

plugins {
    id 'org.gretty' version '2.2.0'
    id 'war'
}
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: "org.akhikhl.gretty"
apply plugin: 'application'

sourceCompatibility = 8

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web')
    runtimeOnly('com.h2database:h2')
    runtimeOnly('mysql:mysql-connector-java')
    runtimeOnly('org.hsqldb:hsqldb')
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.9.7'
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile group: 'org.apache.ibatis', name: 'ibatis-core', version: '3.0'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
    compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'
    compile group: 'org.apache.ibatis', name: 'ibatis-core', version: '3.0'
    compile group: 'org.gretty', name: 'gretty-runner-jetty94', version: '2.2.0'
    compile group: 'commons-cli', name: 'commons-cli', version: '1.4'
    providedRuntime ('org.springframework.boot:spring-boot-starter-tomcat')
    runtimeOnly('mysql:mysql-connector-java')
    compileOnly('org.projectlombok:lombok')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}

gretty {
    contextPath = '/'
}

这一切在使用时都可以正常工作,./gradlew bootRun但没有通过自动化测试仪。

有没有办法让运行./gradlew appRun启动SpringBoot主类类似或直接./gradlew bootRun

标签: javaspringspring-boot

解决方案


这是一个很好的问题。将此行添加到 gretty 配置中:

gretty {
    contextPath = '/'
    springBoot = true
}

这是文档:http ://akhikhl.github.io/gretty-doc/spring-boot-support.html


推荐阅读