有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

多模块项目中的java spring引导gradle插件错误查找mainClass

我使用的是Spring boot gradle插件版本1.5.1版本,如下所示。webProject上的构建失败,抱怨缺少属性“mainClass”,并且只有在我运行“webProject:build”时才有效。这是预期用途吗

编辑:更新了构建脚本,并从所有项目中删除了“spring boot”插件。不得不在web项目中添加“bootRepackage”,因为这一步失败了,并且出现了相同的错误。添加“bootRepackage”没有帮助

buildscript {
    ext {
        springBootVersion = '1.5.1.RELEASE'
    }
    repositories {
        mavenLocal()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE")
    }
}

plugins {
    id 'org.springframework.boot' version '1.5.1.RELEASE'
}

defaultTasks 'clean', 'build'
apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility = 1.7
targetCompatibility = 1.7

allprojects {
    apply plugin: 'java'
    //apply plugin: 'org.springframework.boot' -- Commented out based on the answer
    repositories {
        mavenLocal()
    }
    dependencies {
        compile('org.springframework.boot:spring-boot-starter-data-jpa')
        //all dependencies
    }
}

project('aProject') {
    dependencies {
        compile(project(':bProject'))

    }
}

project('webProject') {

    apply plugin: 'war'
    apply plugin: 'org.springframework.boot'
    war {
        baseName = 'webProject'
        version = '1.0.0-SNAPSHOT'
    }
    dependencies {
        compile(project(':aproject'))
        compile(project(':bProject'))
        compile 'org.springframework.boot:spring-boot-starter-tomcat'
    }
    springBoot {
        mainClass = 'com.abc.SomeApplication'
    }
bootRepackage{
    enabled = false
    mainClass = 'com.abc.SomeApplication'
}

}

共 (2) 个答案

  1. # 1 楼答案

    我在一个多模块Spring boot项目中遇到了类似的问题。 编译没有主类的模块A时。主类位于另一个模块(模块B)中

    我在模块a的构建中添加了一个插件。格雷德尔

    apply plugin: 'application'
    
    mainClassName = "module B.ITS_MAIN_CLASS"
    

    那就行了

  2. # 2 楼答案

    不要在主项目中使用Spring Boot gradle插件,只在webProject子模块中使用