Gradle ant 构建文件在配置阶段不可用

Gradle ant build file not not available during configuration phase

如果 ant build.xml 文件在配置阶段 不可用,我不知道如何执行 ant 目标。因为它是一个远程资源(Maven Remote Resources Plugin)。

所以基本上首先我需要像这样获取这个远程资源:

configurations {
    remoteResources
}

dependencies.remoteResources 'group:my-remote-resource:version'

task getRemoteResources(type: Copy) {
    from(zipTree(configurations.remoteResources.first()))
    into("$buildDir/remote-resources")

    // replace placeholders
    filter(org.apache.tools.ant.filters.ReplaceTokens, , tokens: remotePlaceholders)
}

才有build.xml在

"$buildDir/remote-resources"

但我不能使用 ant.importBuild,因为我希望 build.xml 在配置期间可用,而我的情况并非如此。

我正在考虑将远程资源 "download" 移至初始化阶段,但我有一个多模块项目,虽然只有一些子项目正在使用此远程资源,但它们都有自己的占位符来替换.

有什么方法可以在这种特殊情况下执行 ant 目标吗?

编辑:我发现 solution 使用 Ant 的 ProjectHelper 和项目 类 非常好。所以我想这就是我的答案..

所以这是最终的解决方案。 (正如已经提到的学分转到 groovy-almanac

import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;

task executeAntTarget(dependsOn: getRemoteResources) << {
    def antFile = new File("$buildDir/remote-resources/build.xml")
    def antProject = new Project()
    antProject.init()
    ProjectHelper.projectHelper.parse(antProject, antFile)

    antProject.executeTarget('<target-to-execute>');
}