Gradle: 从根项目中获取所有源集
Gradle: Get all sourceSets from root project
我正在编写一个插件来为我目前工作的内部用例做一些自定义文件索引。
做一些修补我发现我可以在 rootProject 的 buildSrc 中创建我的 task/plugin 通过
将任务应用于每个模块
subprojects {
apply plugin: MyCustomIndexerPlugin
}
我的插件实现看起来像这样,并且在单个模块的上下文中工作得很好:
@Override
public void apply(Project project) {
Convention convention = project.getConvention();
System.out.println("Working: " + project.getName());
JavaPluginConvention javaPluginConvention = convention.getPlugin(JavaPluginConvention.class);
SourceSetContainer sourceSets = javaPluginConvention.getSourceSets();
TaskContainer taskContainer = project.getTasks();
MyCustomIndexerTask myCustomIndexerTask = taskContainer.create("myCustomIndexerTask", MyCustomIndexerTask.class, task -> task.setSourceSetContainer(sourceSets));
Task build = taskContainer.getByName("build");
build.dependsOn(myCustomIndexerTask);
}
这是我的任务:
@TaskAction
public void taskAction() {
SortedMap<String, SourceSet> asMap = getSourceSetContainer().getAsMap();
for (String sourceSetName : asMap.keySet()) {
SourceSet sourceSet = asMap.get(sourceSetName);
Set<File> files = sourceSet.getAllJava().getFiles();
for (File file : files) {
System.out.println(sourceSetName + " -> " + file);
}
}
}
这(有点)可以作为概念证明,但我希望在 rootProject 级别执行我的自定义任务。因此,在成功构建所有模块之后,我 运行 我的代码针对所有 sourceSets。这可能吗,或者我是否需要在构建项目时以某种方式将这些数据从一个模块传递到另一个模块?
我很难找到正确的文档来执行我需要执行的正确的元编码。
您可以在根项目的 build.gradle
文件中应用该插件。
然后您可以执行以下操作:
@Override
def apply(Project project) {
if (project != project.rootProject) { throw new IllegalStateException("Can only be applied to rootProject") }
def myCustomIndexerTask = taskContainer.create("myCustomIndexerTask", MyCustomIndexerTask.class)
project.tasks.getByName("build").dependsOn myCustomIndexerTask
project.subprojects.each { sp ->
sp.whenPluginAdded(JavaPlugin) { jp ->
def javaPluginConvention = sp.getConvention().getPlugin(JavaPluginConvention)
def sourceSets = javaPluginConvention.getSourceSets()
myCustomIndexerTask.addSourceSetContainer(sourceSets)
}
}
}
在您的自定义任务中,您需要迭代所有已添加的 SourceSetContainers
。
我正在编写一个插件来为我目前工作的内部用例做一些自定义文件索引。
做一些修补我发现我可以在 rootProject 的 buildSrc 中创建我的 task/plugin 通过
将任务应用于每个模块subprojects {
apply plugin: MyCustomIndexerPlugin
}
我的插件实现看起来像这样,并且在单个模块的上下文中工作得很好:
@Override
public void apply(Project project) {
Convention convention = project.getConvention();
System.out.println("Working: " + project.getName());
JavaPluginConvention javaPluginConvention = convention.getPlugin(JavaPluginConvention.class);
SourceSetContainer sourceSets = javaPluginConvention.getSourceSets();
TaskContainer taskContainer = project.getTasks();
MyCustomIndexerTask myCustomIndexerTask = taskContainer.create("myCustomIndexerTask", MyCustomIndexerTask.class, task -> task.setSourceSetContainer(sourceSets));
Task build = taskContainer.getByName("build");
build.dependsOn(myCustomIndexerTask);
}
这是我的任务:
@TaskAction
public void taskAction() {
SortedMap<String, SourceSet> asMap = getSourceSetContainer().getAsMap();
for (String sourceSetName : asMap.keySet()) {
SourceSet sourceSet = asMap.get(sourceSetName);
Set<File> files = sourceSet.getAllJava().getFiles();
for (File file : files) {
System.out.println(sourceSetName + " -> " + file);
}
}
}
这(有点)可以作为概念证明,但我希望在 rootProject 级别执行我的自定义任务。因此,在成功构建所有模块之后,我 运行 我的代码针对所有 sourceSets。这可能吗,或者我是否需要在构建项目时以某种方式将这些数据从一个模块传递到另一个模块?
我很难找到正确的文档来执行我需要执行的正确的元编码。
您可以在根项目的 build.gradle
文件中应用该插件。
然后您可以执行以下操作:
@Override
def apply(Project project) {
if (project != project.rootProject) { throw new IllegalStateException("Can only be applied to rootProject") }
def myCustomIndexerTask = taskContainer.create("myCustomIndexerTask", MyCustomIndexerTask.class)
project.tasks.getByName("build").dependsOn myCustomIndexerTask
project.subprojects.each { sp ->
sp.whenPluginAdded(JavaPlugin) { jp ->
def javaPluginConvention = sp.getConvention().getPlugin(JavaPluginConvention)
def sourceSets = javaPluginConvention.getSourceSets()
myCustomIndexerTask.addSourceSetContainer(sourceSets)
}
}
}
在您的自定义任务中,您需要迭代所有已添加的 SourceSetContainers
。