从另一个gradle项目的配置中扩展

时间:2019-02-22 15:24:35

标签: gradle

我不会用项目a中的配置扩展项目b中的配置。

如果两种配置都在同一项目中,则可以执行以下操作:

configurations.conf1.extendsFrom configurations.conf2

如何在不同项目中实现相同的配置?

下面是一个最小的示例,由于不继承特权,因此当前不起作用。

示例文件夹设置:
a / build.gradle
b / build.gradle
settings.gradle

a / build.gradle:

repositories {
    mavenCentral()
}
configurations {
    aconf
}
dependencies {
    aconf 'abbot:abbot:1.4.0'
}

b / build.gradle

configurations {
    bconf
}
configurations.bconf.extendsFrom project(':a').configurations.aconf 

settings.gradle

include ':a'
include ':b'

执行./gradlew:a:dep:b:dep -i显示bconf不会从aconf继承依赖项。

Included projects: [root project 'projectDeps', project ':a', project ':b']

> Configure project :
Evaluating root project 'projectDeps' using build file '/projectDeps/build.gradle'.

> Configure project :a
Evaluating project ':a' using build file '/projectDeps/a/build.gradle'.

> Configure project :b
Evaluating project ':b' using build file '/projectDeps/b/build.gradle'.
All projects evaluated.
Selected primary task ':a:dependencies' from project :a
Selected primary task ':b:dependencies' from project :b
Tasks to be executed: [task ':a:dependencies', task ':b:dependencies']
:a:dependencies (Thread[Task worker for ':',5,main]) started.

> Task :a:dependencies
Task ':a:dependencies' is not up-to-date because:
  Task.upToDateWhen is false.

------------------------------------------------------------
Project :a
------------------------------------------------------------

aconf
\--- abbot:abbot:1.4.0
     \--- junit:junit:4.8.2

> Task :b:dependencies
Task ':b:dependencies' is not up-to-date because:
  Task.upToDateWhen is false.

------------------------------------------------------------
Project :b
------------------------------------------------------------

bconf
No dependencies

2 个答案:

答案 0 :(得分:1)

在多项目构建中,Gradle将以给定顺序评估/配置子项目,默认情况下,该顺序是项目名称的字母顺序(我不知道是否可以更改此顺序),所以:

  • 子项目projectA将在子项目projectB之前进行评估/配置
  • projectA配置中,您引用了projectB中尚未被Gradle掌握的配置,这就是为什么您会收到错误消息(应该类似Could not get unknown property 'conf2' ...

一种解决方案是在创建配置之间的依赖关系之前让Gradle配置projectB:例如,可以如下使用afterEvaluate {} API:

projectA build.gradle 中:

project(":projectB").afterEvaluate { proj ->
    configurations.conf1.extendsFrom proj.configurations.confg2

}

答案 1 :(得分:0)

最好的方法是拥有root =>子项目体系结构。

因此,在根项目中,您可以定义“共享”配置。

根据https://remonsinnema.com/2016/05/09/how-to-manage-dependencies-in-a-gradle-multi-project-build/

根项目build.gradle

subprojects {
  configurations {
    commonsIo
  }

  dependencies {
    commonsIo 'commons-io:commons-io:2.5'
  }
}

子项目build.gradle

configurations {
  compile.extendsFrom commonsIo
}