【问题标题】:creating spring boot gradle mutli project创建spring boot gradle mutli项目
【发布时间】:2020-01-16 05:49:06
【问题描述】:

我是 gradle 新手。我正在尝试在 STS 中创建 spring boot gradle mutli 项目,但我不明白如何创建。我能够创建单个项目。 所以帮助我如何创建多项目。

根项目:nn-backend

build.gradle:

  plugins {
        id 'org.springframework.boot' version '2.1.8.RELEASE'
        id 'io.spring.dependency-management' version '1.0.8.RELEASE'
        id 'java'
    }

    group = 'com.nn'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '1.8'

    repositories {
        mavenCentral()
            maven { url "https://repo.spring.io/snapshot" }
            maven { url "https://repo.spring.io/milestone" }
    }



    allprojects {

        group = 'com.nn'
        //version = rootProject.version
        //sourceCompatibility = 1.8

        apply plugin: 'application'
        apply plugin: 'java'
        apply plugin: 'jacoco'
        apply plugin: 'maven'
        apply plugin: 'io.spring.dependency-management'
        apply plugin: 'org.springframework.boot'

        mainClassName = 'com.cc.CcAdminApplication'

        version = rootProject.version



        ext {
            set('springCloudVersion', 'Greenwich.RC2')
        }

        configurations {
            provided
            compile.extendsFrom provided
            compile.exclude module: 'log4j-slf4j-impl'
        }

        repositories {
            mavenCentral()
            maven { url "http://repo.spring.io/release" }
            maven { url "http://repo.spring.io/milestone" }
            maven { url "http://repo.spring.io/snapshot" }
            maven { url "http://repo.maven.apache.org/maven2" }
            maven { url 'https://repo.spring.io/libs-release' }

        }

        dependencies {

            compile('org.springframework.boot:spring-boot-starter-actuator')
            compile('org.springframework.boot:spring-boot-starter-log4j2')
            testCompile('org.springframework.boot:spring-boot-starter-test')
            compile('org.springframework.boot:spring-boot-starter-logging')
            compile('org.apache.commons:commons-lang3:3.6')
            provided group: 'org.projectlombok', name: 'lombok', version: '1.16.20'
            compile group: 'com.vaadin.external.google', name: 'android-json', version: '0.0.20131108.vaadin1'

        }
    }

settings.gradle:

 rootProject.name = 'nn-backend'

    include ':nn-model'
    include ':nn-admin'
    include ':nn-common'
    include ':nn-filters'

子项目:nn-common build.gradle:

plugins {
    id 'org.springframework.boot' version '2.1.8.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

group = 'com.nn'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {

    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

子项目:nn-过滤器 build.gradle:

plugins {
    id 'org.springframework.boot' version '2.1.8.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

group = 'com.nn'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
compile project(':nn-common')

    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

错误:

【问题讨论】:

  • 首先你需要看看所有的依赖会被共享,然后你在根项目上创建。
  • @JonathanJohx 如何创建根项目
  • 我发布了我提到的答案

标签: spring spring-boot gradle spring-boot-gradle-plugin


【解决方案1】:

你应该先定义root project的build.gradle应该有如下配置:

build.gradle

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

allprojects {

    repositories {
        mavenCentral()
        maven { url "http://repo.spring.io/milestone" }
    }

    apply plugin: 'java'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'

    group = projectGroup
    version = projectVersion
    sourceCompatibility = javaVersion

    dependencies {

        // Here add the dependencies shared for each subproject.
        compile('org.springframework.boot:spring-boot-autoconfigure')
        compile('org.springframework.boot:spring-boot-starter')
        compile('org.springframework.boot:spring-boot-starter-web')
        // Here the the dependecies shared for testing
        testCompile('org.springframework.boot:spring-boot-starter-test')
    }

    dependencyManagement {
        imports {
            mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
        }
    }    
}

gradle.properties

javaVersion=1.8
projectGroup = com.example
projectName = project-name
projectVersion = 0.1-SNAPSHOT

springBootVersion = 2.0.0.RELEASE
springCloudVersion = Finchley.RELEASE

settings.gradle

rootProject.name = 'project-name'
include 'sub-project-1', 'sub-project-2', ..

让我们定义 sub-project-1 的设置

build.gradle

bootJar {
    mainClassName = 'com.example.sub-project-1.Application'
}

基本上这是多项目gradlesettings/propertiessub-project-1 的最后一个 build.gradle 共享了 allproject 正文中的所有依赖项。

【讨论】:

  • 我已经完成了所有这些事情,但是当我将一个项目作为依赖项添加到另一个项目(项目名称:“nn-filters”)时,在刷新 gradle 时出错:带有路径的项目:nn-根项目 'nn-filters' 找不到 common'
  • 您能否添加更多详细信息?不知道你在说什么,很难帮助你。
  • 好吧,对不起,我忘了评论你,你需要一个根文件夹,里面有等级设置,在这个文件夹或根项目中,你需要添加子项目,似乎是你的问题
  • 对不起,我没听懂你在说什么。
猜你喜欢
  • 2018-09-29
  • 2020-04-13
  • 2021-11-01
  • 2017-11-03
  • 2021-08-01
  • 2018-09-24
  • 1970-01-01
  • 2017-12-15
  • 2020-05-16
相关资源
最近更新 更多