获取包含构建的 rootPoject.name,或对 settings.gradle 的引用
Get rootPoject.name, or reference to settings.gradle, of included builds
我正在使用 includeBuild
将我自己的库中的模块包含在 settings.gradle
:
rootProject.name = "MyApp"
include ':app'
includeBuild '/usr/local/library/Android/Event'
includeBuild '/usr/local/library/Android/Location'
includeBuild '/usr/local/library/Android/Widget'
我知道我可以稍后重复这些:
gradle.includedBuilds.each{ includeBuild ->
println includeBuild.name
}
然而,打印:
Event
Location
Widget
有没有一种简单的方法来获取我在每个单独的库项目的 settings.gradle
文件中定义的 rootProject.name
?
我知道我可以执行以下操作来提供替代名称:
includeBuild('/usr/local/library/Android/Event', {name = 'com.example.android.event'})
includeBuild('/usr/local/library/Android/Location', {name = 'com.example.android.location'})
includeBuild('/usr/local/library/Android/Widget', {name = 'com.example.android.widget'})
...但是当我已经将它们定义为 rootProject.name
是它们各自的 settings.gradle
.
时,这是多余且麻烦的
相反,我正在寻找类似于:
gradle.includedBuilds.each{ includeBuild ->
println includeBuild.rootProject.name
}
例如,我知道includeBuild.projectDir
。我能以某种方式获得对该目录中 settings.gradle
文件的(已解析)引用吗?
我已经设法用 org.gradle.tooling.GradleConnector
解决了它:
import org.gradle.tooling.GradleConnector
import org.gradle.tooling.ProjectConnection
import org.gradle.tooling.model.GradleProject
def getIncludedProjectNamesMap(Project project) {
def projectNamesMap = new HashMap<String, String>()
project.gradle.includedBuilds.each { includedBuild ->
ProjectConnection connection = GradleConnector.newConnector()
.forProjectDirectory(includedBuild.projectDir)
.connect()
GradleProject includedProject = connection.getModel(GradleProject.class);
def name = includedProject.getName();
connection.close();
projectNamesMap.put includedBuild.name, name;
}
return projectNamesMap
}
println getIncludedProjectNamesMap(project)
... 打印:
{Event=com.example.android.event, Location=com.example.android.location, Widget=com.example.android.widget}
...但这似乎相当慢,可能是因为它需要建立所有连接。它现在可以完成工作,但我仍在寻找替代方法(如果可用)。
我正在使用 includeBuild
将我自己的库中的模块包含在 settings.gradle
:
rootProject.name = "MyApp"
include ':app'
includeBuild '/usr/local/library/Android/Event'
includeBuild '/usr/local/library/Android/Location'
includeBuild '/usr/local/library/Android/Widget'
我知道我可以稍后重复这些:
gradle.includedBuilds.each{ includeBuild ->
println includeBuild.name
}
然而,打印:
Event
Location
Widget
有没有一种简单的方法来获取我在每个单独的库项目的 settings.gradle
文件中定义的 rootProject.name
?
我知道我可以执行以下操作来提供替代名称:
includeBuild('/usr/local/library/Android/Event', {name = 'com.example.android.event'})
includeBuild('/usr/local/library/Android/Location', {name = 'com.example.android.location'})
includeBuild('/usr/local/library/Android/Widget', {name = 'com.example.android.widget'})
...但是当我已经将它们定义为 rootProject.name
是它们各自的 settings.gradle
.
相反,我正在寻找类似于:
gradle.includedBuilds.each{ includeBuild ->
println includeBuild.rootProject.name
}
例如,我知道includeBuild.projectDir
。我能以某种方式获得对该目录中 settings.gradle
文件的(已解析)引用吗?
我已经设法用 org.gradle.tooling.GradleConnector
解决了它:
import org.gradle.tooling.GradleConnector
import org.gradle.tooling.ProjectConnection
import org.gradle.tooling.model.GradleProject
def getIncludedProjectNamesMap(Project project) {
def projectNamesMap = new HashMap<String, String>()
project.gradle.includedBuilds.each { includedBuild ->
ProjectConnection connection = GradleConnector.newConnector()
.forProjectDirectory(includedBuild.projectDir)
.connect()
GradleProject includedProject = connection.getModel(GradleProject.class);
def name = includedProject.getName();
connection.close();
projectNamesMap.put includedBuild.name, name;
}
return projectNamesMap
}
println getIncludedProjectNamesMap(project)
... 打印:
{Event=com.example.android.event, Location=com.example.android.location, Widget=com.example.android.widget}
...但这似乎相当慢,可能是因为它需要建立所有连接。它现在可以完成工作,但我仍在寻找替代方法(如果可用)。