使用./gradlew appRun运行Spring Boot应用程序

时间:2018-11-21 19:36:48

标签: java spring 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类似地或直接./gradlew bootRun启动SpringBoot主类?

1 个答案:

答案 0 :(得分:1)

这是一个很大的问题。将此行添加到gretty config中:

gretty {
    contextPath = '/'
    springBoot = true
}

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