自定义任务没有 运行

Custom task does not run

我的以下项目结构如下:

.
├── build.gradle
├── out.txt
├── Server
│   ├── build.gradle
│   ├── plugins
│   │   └── TestPlugin-0.1.0.zip
│   └── src
├── pluginApi
│   ├── build
│   ├── build.gradle
│   └── src
├── plugins
│   └── testPlugin
│       ├── build
│       │   ├── libs
│       │   │   └── TestPlugin-0.1.0.zip
│       ├── build.gradle
│       └── src
└── settings.gradle

现在我想在主 build.gradle 中创建一个 gradle 任务,它应该做这样的事情:

我设法写了一个像这样的简单任务:

task compilePluginsAndCopy() {
    delete 'Server/plugins'
    mkdir 'Server/plugins'

    subprojects.each { p ->
        if (p.path.contains("plugins/")) {

            p.createZip

            doLast {
                copy {
                    from p.path.substring(1) + '/build/libs'
                    into 'Server/plugins'
                }
            }
        }
    }
}

当我现在 运行 此任务时,不会创建 zip 文件,也不会删除任何内容。 但是,当我 运行 子项目之一的 "createZip" 任务时,任务 compilePluginsAndCopy 被执行。

有人可以帮帮我吗?

您的任务定义应如下所示:

task compilePluginsAndCopy() << {
...
}

注意 <<

<<doLast 的简写。执行此操作的非速记方法是:

task compilePluginsAndCopy() {
    doLast {
        ...
    }
}

如果您的代码未包含在 doLast 块中,它将在配置阶段执行,这就是您在 运行 不同任务时看到的情况。