【问题标题】:How to set up Spring boot with submodules in gradle如何在 gradle 中使用子模块设置 Spring Boot
【发布时间】:2017-08-01 18:06:11
【问题描述】:

所以我只是在玩(实验)gradle 和 spring-boot。

当我关注 hello world 时,我很容易让项目运行起来,这让我很开心。

现在我想要一个结构化的项目;为此,我正在使用 Intellij 社区(不确定是否相关)。我有以下结构。

/项目 - 构建.gradle - settings.gradle(仅包括服务) /项目/服务/ - 构建.gradle - settings.gradle(仅包括 MyService) /项目/服务/我的服务 - build.gradle

现在我可以共享我的一些 build.gradle 文件,但我正在尝试在互联网上找到的随机东西。我的问题是 MyService 中没有 Spring Boot 类。Myservice 中的以下目录结构是标准的Java (/src/main/java)

如果可能,我会尝试将依赖项和版本放在我的主 bu​​ild.gradle 中。谁能指出我做错了什么。

目前我只将 gradle 用于简单的 android 开发工作。

/Project/build.gradle

group 'nl.msegers.project'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'idea'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

/Project/Services/build.gradle

group 'nl.msegers.project.services'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE")
    }
}

/Project/Services/MyService/build.gradle

group 'nl.msegers.project.services.myservice'
version parent.version

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'Navigation'
    version =  parent.version
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

【问题讨论】:

    标签: java gradle spring-boot


    【解决方案1】:

    我已经在网上搜索了一个小时左右,以使这样的结构能够正常工作。与此同时,我也从头开始重建我的项目(尽管我没有太多代码)。

    我的代码目前允许简单的 Spring boot 子项目而无需过多的冗余配置,并且它们可以很容易地编译 Libraries 项目,但这可能需要一些工作。

    我现在得到的是这样的结构。

    /项目 /项目/库 /项目/库/模型 /项目/服务/ /项目/服务/服务

    只有值得注意的 build.gradle 文件(其他几乎是空的):

    /项目/服务/

    group 'nl.msegers.project.services'
    version parent.version
    
    buildscript {
        ext {
            springBootVersion = "1.5.2.RELEASE"
        }
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        }
    }
    
    subprojects {
        buildscript {
            ext {
                springBootVersion = "1.5.2.RELEASE"
            }
            repositories {
                mavenCentral()
            }
            dependencies {
                classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
            }
        }
        apply plugin: 'java'
        apply plugin: 'org.springframework.boot'
    
    
        dependencies {
    
            compile("org.springframework.boot:spring-boot-starter-web") {
                exclude module: "spring-boot-starter-tomcat"
            }
            compile("org.springframework.boot:spring-boot-starter-jetty")
            compile("org.springframework.boot:spring-boot-starter-actuator")
    
            compile project(':Libraries:Models')
    
            testCompile 'junit:junit:4.12'
        }
    
        task wrapper(type: Wrapper) {
            gradleVersion = '3.1'
        }
    }
    

    /项目/服务/我的服务

    group 'nl.msegers.project.services.myservice'
    version parent.version
    
    jar {
        baseName = 'myservice'
        version =  parent.version
    }
    
    repositories {
        mavenCentral()
    }
    

    【讨论】:

      【解决方案2】:

      要让每个项目都从根项目继承依赖项,您可以在根项目上使用 allprojects 脚本块,例如:

      allprojects {
                  dependencies {
              compile group: 'commons-collections', name: 'commons-collections', version: '3.+'
              ...
          }
      }
      

      【讨论】:

      • 这也适用于子项目?虽然我的问题是在我的 build.gradle 在同一级别定义时甚至没有加载依赖项。
      • 这个问题是因为你在一个地方为springboot定义了一个buildscript,而在另一个地方应用插件,见:docs.gradle.org/current/userguide/…
      • 虽然您的回答可能让我朝着正确的方向前进。我确实以稍微不同的方式解决了它。还有一个我的服务中包含的 /Libraries/Modules 项目。我将根据我今天在某处所做的事情创建一个单独的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 2019-01-09
      • 2017-06-06
      • 1970-01-01
      • 2014-12-29
      • 2015-11-03
      • 2015-02-14
      相关资源
      最近更新 更多