Gradle多项目存储库配置对于spring-boot应用程序不起作用

huangapple go评论78阅读模式
英文:

Gradle multi-project repository configuration not working for spring-boot application

问题

我有一个简单的多项目结构,如下所示:

|- gps-framework
|- build.gradle
|- settings.gradle
|- gps-web-service
|----build.gradle
|----src/main....

在位于 gps-framework/ 下的根 build.gradle 如下所示:

plugins {
    id 'java'
    id 'java-library'
    id 'maven-publish'
    id 'jacoco'
    id 'org.springframework.boot' version '2.3.4.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
}

subprojects {

    subproject ->

        apply plugin: 'maven-publish'
        apply plugin: 'jacoco'
      
        def isApp = subproject.name == 'gps-web-service'

        if (isApp) {
            apply plugin: 'java'
        } else {
            apply plugin: 'java-library'
        }

        group = 'com.mycompnay'
        version = project.hasProperty('customVersion') ? project['customVersion'] : 'integration-SNAPSHOT'

        //System.out.println(subproject.name)

        compileJava {
            sourceCompatibility = '1.8'
            targetCompatibility = '1.8'

            options.encoding = 'UTF-8'
     
        }

        compileTestJava {
            sourceCompatibility = '1.8'
            targetCompatibility = '1.8'

            options.encoding = 'UTF-8'

        }

        repositories {
            mavenCentral()
            mavenLocal()

            maven {
                name "release"
                url "my-custom-mvn-repo/release"
                authentication {
                    //
                }
            }

            maven {
                name "snapshot"
                url "my-custoom-mvn-repo/snapshot"
                authentication {
                    //
                }
            }
        }

        configurations {
            runtime.exclude(group: "org.slf4j", module:"slf4j-log4j12")
        }

        publishing {
            repositories {
                maven {
                    authentication {
                        //
                    }
                    def isSnapshot = subproject.version.contains('SNAPSHOT')
                    url =  isSnapshot ? "my-custom-mvn-repo/snapshot" : "my-custom-mnv-repo/release"
                }
            }
        }

        jacocoTestReport {
            reports {
                xml.enabled true
                html.enabled true
            }
        }

        if (subproject.hasProperty('jacocoTestCoverageVerification')) {
            subproject.check.dependsOn subproject.jacocoTestCoverageVerification
        }

        test {
            testLogging {
                exceptionFormat = 'full'
            }
        }
}

子项目 gps-web-servicebuild.gradle 位于 gps-framework/gps-web-service,如下所示:

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

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

根据我已阅读的内容,由于仓库设置在根 build.gradle 中定义,并且这些设置被注入到这些子项目中,我不需要重新定义仓库设置。但事实似乎并非如此,因为当我运行

./gradlew build

我收到的错误消息是:

Could not find org.springframework.boot:spring-boot-starter-web:.
Required by:
project :gps-web-service
Possible solution:
- Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

我是否误解了项目属性注入的整个概念,还是我漏掉了什么?我将尝试在子项目的 build.gradle 中复制仓库设置,但希望不需要这样做。

英文:

I have a simple multi-project structure as follows:

|- gps-framework
|- build.gradle
|- settings.gradle
|- gps-web-service
|----build.gradle
|----src/main....

Under the root build.gradle located in gps-framework/ looks like this:

plugins {
id 'java'
id "java-library"
id "maven-publish"
id "jacoco"
id 'org.springframework.boot' version '2.3.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
}
subprojects {
subproject ->
apply plugin: 'maven-publish'
apply plugin: 'jacoco'
def isApp = subproject.name == 'gps-web-service'
if (isApp) {
apply plugin: 'java'
} else {
apply plugin: 'java-library'
}
group = 'com.mycompnay'
version = project.hasProperty('customVersion') ? project['customVersion'] : 'integration-SNAPSHOT'
//System.out.println(subproject.name)
compileJava {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
options.encoding = 'UTF-8'
}
compileTestJava {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
options.encoding = 'UTF-8'
}
repositories {
mavenCentral()
mavenLocal()
maven {
name "release"
url "my-custom-mvn-repo/release"
authentication {
//
}
}
maven {
name "snapshot"
url "my-custoom-mvn-repo/snapshot"
authentication {
//
}
}
}
configurations {
runtime.exclude(group: "org.slf4j", module:"slf4j-log4j12")
}
publishing {
repositories {
maven {
authentication {
//
}
def isSnapshot = subproject.version.contains('SNAPSHOT')
url =  isSnapshot ? "my-custom-mvn-repo/snapshot" : "my-custom-mnv-repo/release"
}
}
}
jacocoTestReport {
reports {
xml.enabled true
html.enabled true
}
}
if (subproject.hasProperty('jacocoTestCoverageVerification')) {
subproject.check.dependsOn subproject.jacocoTestCoverageVerification
}
test {
testLogging {
exceptionFormat = 'full'
}
}
}

The build.gradle of sub-project gps-web-service located in gps-framework/gps-web-service looks like this:

group = 'com.mycompany'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}

From what I have read so far, since the repository settings are defined in the root build.gradle and these settings are injected into these subprojects, I do not need to redefine repository settings. This does not appear to be the case as when I run

./gradlew build

The error I get is:

Could not find org.springframework.boot:spring-boot-starter-web:.
Required by:
project :gps-web-service
Possible solution:
- Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

Am I misunderstanding the whole concept of injection of project properties or what am I missing here? I will try duplicating the repo settings in the sub-projects build.gradle but was hoping this is not necessary.

答案1

得分: 1

为解决这个特定的问题,我不得不在子项目的 build.gradle 中定义 Spring Boot 插件,如下所示:

plugins {
    id 'org.springframework.boot' version '2.3.4.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
}

生成的文件如下所示:

plugins {
    id 'org.springframework.boot' version '2.3.4.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
}

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

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}
英文:

To solve this specific problem, I had to define the spring boot plugin in the sub-project's build.gradle as follows:

plugins {
id 'org.springframework.boot' version '2.3.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
}

The resulting file looks like this:

plugins {
id 'org.springframework.boot' version '2.3.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
}
group = 'com.mycompany'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}

huangapple
  • 本文由 发表于 2020年10月14日 00:09:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64339033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定