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

标签 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主类?

最佳答案

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

gretty {
    contextPath = '/'
    springBoot = true
}

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

关于java - 使用 ./gradlew appRun 运行 Spring Boot 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53419391/

相关文章:

java - 如何配置 RabbitMQ 为多个消费者平等地提供多个队列

java - Java 中的 Volatile 关键字

spring-boot - 如何使用 spring boot 2.0 配置 redis ttl

java - 使用 JPA 和 Spring Boot 查询实体类型(鉴别器)

java - Maven 编译器插件错误(maven-compiler-plugin :2. 5.1:testCompile)

spring - 当执行器处于事件状态时,我应该使用哪个注释来防止 Spring Boot 保护我的 Controller

spring - 如何在 spring boot 中从同一个应用程序调用另一个 api

java - 如何使用 Spring Batch 并行步骤分割流配置上述用例?

java - Redis:为使用@Cacheable 注释的方法设置不同的生存时间

java - 可能的精度损失误差与类型不匹配误差