问题
利用SpringBoot开发Web应用,利用gradle进行编译,但是在编译时报错
* What went Wrong:
Execution failed for task ':core:findMainClass'.
> Unable to find a single main class from the following candidates [....]
错误原因是找到了多个main方法,不能确定是哪一个。
项目结构如下:
PROJECT
|__ web // 依赖core层
|__ core
|__ common
|__ dal
|__ sal
|__ build.gradle
但是错误显示在core层找到多个main方法,于是在build.gradle文件中web层我们指定MainClass,core层设置bootRepackage*d的*enabled属性为false, 还是编译错误。
project(':web') {
war {
baseName = 'web'
}
bootRepackage {
mainClass = "com.hello.web.HelloApplication"
}
}
project(':core') {
war {
baseName = 'core'
}
bootRepackage {
enabled = false
}
}
解决
查找资料发现,利用spring-boot插件,可以通过设置mainClass属性,指定main方法所在的类,在解决多个main方法的编译错误。
修改build.gradle文件
// build.gradle文件
subprojects {
apply plugin : 'war'
apply plugin : 'spring-boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
// 指定main方法所在的class
springBoot {
mainClass = "com.hello.web.HelloApplication"
}
...
}