首页 > 解决方案 > 如何在 build.gradle 中嵌入 gradle 的共同价值

问题描述

我在 common.build 中定义了一个公共依赖项,如下所示(Gradle 6.0.1):

ext {
    java = [
            compileSdkVersion: 1.8,
            minSdkVersion    : 1.8,
            targetSdkVersion : 1.8,
            versionCode      : 1.8,
    ]

    version = [
            mybatisGeneratorCoreVersion       : '1.3.7',
            itfswMybatisGeneratorPluginVersion: '1.3.8'
    ]

    dependencies = [
            mybatisGeneratorCore       : "org.mybatis.generator:mybatis-generator-core:${version["mybatisGeneratorCoreVersion"]}",
    ]
}

并在根项目 build.gradle 中使用,如下所示:

subprojects {
    apply from: "${rootProject.projectDir}/common.gradle"
dependencies {
        implementation rootProject.ext.dependencies.mybatisGeneratorCore
    }
}

并像这样构建项目:

./gradlew clean :soa-illidan-mini:soa-illidan-mini-service:build -x test

并给我这个错误:

~/Library/Mobile Documents/com~apple~CloudDocs/Document/source/dabai/microservice/soa-illidan-mini on  master! ⌚ 10:59:03
$ ./gradlew clean :soa-illidan-mini:soa-illidan-mini-service:build -x test

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/dolphin/Library/Mobile Documents/com~apple~CloudDocs/Document/source/dabai/microservice/build.gradle' line: 99

* What went wrong:
A problem occurred evaluating root project 'microservice'.
> Cannot get property 'dependencies' on extra properties extension as it does not exist

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 12s

我跟着网络教程,我应该怎么做才能解决这个问题?

标签: gradle

解决方案


将您的应用命令移动到 build.gradle 的根目录,如下所示:

apply from: "${rootProject.projectDir}/common.gradle"

只需将其放在项目配置的顶层即可。有用!


推荐阅读