[英]Gradle: Get all sourceSets from root project
我正在寫一個插件來為我們目前使用的內部用例做一些自定義文件索引。
進行一些修補,我發現我可以在rootProject的buildSrc中創建任務/插件,將任務通過
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級別執行自定義任務。 因此,在所有模塊成功構建之后,我將針對所有sourceSet運行我的代碼。 這是否可能,或者我需要在項目構建時以某種方式在模塊之間傳遞此數據?
我很難找到合適的文檔來執行需要執行的正確元編碼。
您可以在rootProject的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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.