Java Gradle不知道自定义自动配置库-在Gradle中测试失败,但在IDE中成功

Java Gradle不知道自定义自动配置库-在Gradle中测试失败,但在IDE中成功,java,spring,spring-boot,gradle,kotlin,Java,Spring,Spring Boot,Gradle,Kotlin,我正在创建自定义启动程序,并希望添加示例应用程序来演示自动配置和测试它。但当我在控制台中运行/gradlew test时,Gradle似乎不知道META-INF/spring.factories Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at lea

我正在创建自定义启动程序,并希望添加示例应用程序来演示自动配置和测试它。但当我在控制台中运行
/gradlew test
时,Gradle似乎不知道
META-INF/spring.factories

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. 
这是我的示例应用程序的
build.gradle
,它是多模块项目中的模块:

plugins {
    id 'org.jetbrains.kotlin.jvm'
    id 'org.jetbrains.kotlin.plugin.spring'
    id 'org.jetbrains.kotlin.plugin.jpa'
    id 'org.springframework.boot'
    id 'io.spring.dependency-management'
}

dependencies {
    implementation project(':custom-spring-boot-starter')
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation "org.flywaydb:flyway-core:5.2.4"
    implementation 'com.h2database:h2:1.4.197'
    // Test
    testImplementation "org.springframework.boot:spring-boot-starter-test"
    testImplementation 'org.springframework.boot:spring-boot-test-autoconfigure:2.1.7.RELEASE'
    testImplementation "org.hamcrest:hamcrest:2.1"
    testImplementation "org.testng:testng:6.14.3"
    testImplementation "com.github.javafaker:javafaker:0.17.2"
    testImplementation "org.awaitility:awaitility:3.0.0"
}

test {
    useTestNG()
    jacoco {
        destinationFile = file("$rootDir/build/jacoco/test.exec")
    }
}
它是纯Spring引导应用程序:

@SpringBootApplication
class Application

fun main(args: Array<String>) {
    SpringApplication.run(Application::class.java, *args)
}
@springboot应用程序
班级申请
趣味主线(args:Array){
run(Application::class.java,*args)
}
我为所有上下文测试配置基类,如下所示:

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
abstract class ApplicationTest : AbstractTestNGSpringContextTests() {

    @Autowired
    lateinit var restTemplate: TestRestTemplate

    @LocalServerPort
    private var port: Int = 0

    fun getRequest(uri: String, responseType: Class<*>) =
        restTemplate.getForEntity("http://localhost:$port$uri", responseType)
}
@SpringBootTest(webEnvironment=webEnvironment.RANDOM\u端口)
抽象类ApplicationTest:AbstractTestNGSpringContextTests(){
@自动连线
lateinit var restTemplate:TestRestTemplate
@本地服务器端口
专用变量端口:Int=0
趣味getRequest(uri:String,responseType:Class)=
restTemplate.getForEntity(“http://localhost:$port$uri“,responseType)
}

那么,我应该怎么做才能在Gradle命令执行下应用自定义自动配置呢?

看看官方Spring指南(源代码)上的Spring boot多模块项目,他们在那里使用了自定义jar任务(“库”模块)。如果运行“
gradle build
”,则生成的工件位于库
模块-->libs
文件夹中。这是没有主类的Spring引导库。另一个模块(“应用程序”)当然是主应用程序,它使用一个简单的自定义“
bootJar
”任务。这两个模块都包含测试。

看看Spring官方指南(源代码)中的Spring boot多模块项目,其中他们使用了自定义jar任务(“库”模块)。如果运行“
gradle build
”,则生成的工件位于库
模块-->libs
文件夹中。这是没有主类的Spring引导库。另一个模块(“应用程序”)当然是主应用程序,它使用一个简单的自定义“
bootJar
”任务。这两个模块都包含测试。

看起来您是使用自定义任务在gradle中自己创建可执行jar,还是依赖gradle spring启动插件?@Shailendra,用于创建简单jar的自定义任务。如果我尝试
bootJar
任务,它将导致错误,因为starter没有主类。
@Autowired
可能需要一个
@限定符
。看起来您是使用自定义任务在gradle中自己创建可执行jar,还是依赖于gradle spring启动插件?@Shailendra,用于创建简单jar的自定义任务。如果我尝试
bootJar
任务,它将导致错误,因为starter没有主类。
@Autowired
可能需要一个
@Qualifier