SpringBoot Web项目编译错误: Unable to find a single main class

问题

利用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层我们指定MainClasscore层设置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"
    }
    ...
}

参考文献

https://www.baeldung.com/spring-boot-main-class

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值