Gradle 为什么我在尝试将依赖任务添加到由C插件创建的任务时会出现错误?

Gradle 为什么我在尝试将依赖任务添加到由C插件创建的任务时会出现错误?,gradle,Gradle,我有一个使用C插件生成本机可执行文件的构建脚本。当我试图添加一个依赖任务(从flex输入文件生成flex输出文件的任务)时,我要么得到一个错误,要么我的依赖任务没有执行。我认为现在发生的是,在模型创建其任务之前,正在评估添加依赖任务的声明。如果是这样的话,那么我需要知道如何使依赖任务声明更懒惰(或者向模型中添加一些我还不了解的东西)。我应该用一个单独的项目来做这个吗 以下是我认为构建脚本的基本部分。我已经注释掉了我声明依赖关系的尝试 $ cat build.gradle apply plugin

我有一个使用C插件生成本机可执行文件的构建脚本。当我试图添加一个依赖任务(从flex输入文件生成flex输出文件的任务)时,我要么得到一个错误,要么我的依赖任务没有执行。我认为现在发生的是,在模型创建其任务之前,正在评估添加依赖任务的声明。如果是这样的话,那么我需要知道如何使依赖任务声明更懒惰(或者向模型中添加一些我还不了解的东西)。我应该用一个单独的项目来做这个吗

以下是我认为构建脚本的基本部分。我已经注释掉了我声明依赖关系的尝试

$ cat build.gradle
apply plugin: 'c'

model {
    components {
        test(NativeExecutableSpec) {
            sources {
                c {
                    source {
                        srcDir "."
                        include "*.c"
                    }
                }
            }
        }
    }
}

task compileLang (type: Exec) {
    inputs.file(project.file('test.l'))
    outputs.file(project.file('lex.test.c'))
    commandLine 'flex', '-f', '-L', '-8', '-i', '-P', 'test', 'test.l'
}

//buildDependentsTestExecutable.dependsOn compileLang
//project.task('buildDependentsTestExecutable').dependsOn compileLang
//project.tasks.getByName('buildDependentsTestExecutable').dependsOn compileLang
//tasks['buildDependentsTestExecutable'].dependsOn compileLang
我认为,这些是我执行“gradle任务”时配置的相关任务:

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
clean - Deletes the build directory.
installTestExecutable - Installs a development image of executable 'test:executable'
testExecutable - Assembles executable 'test:executable'.

Build Dependents tasks
----------------------
assembleDependentsTest - Assemble dependents of native executable 'test'.
assembleDependentsTestExecutable - Assemble dependents of executable 'test:executable'.
buildDependentsTest - Build dependents of native executable 'test'.
buildDependentsTestExecutable - Build dependents of executable 'test:executable'.
如果test.c和test.l需要有人更容易地回答问题

$ cat test.c
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]) {
    puts("hello, world");
    testwrap();
}
$ cat test.l
%s OLC
%%
<INITIAL>-- {
    BEGIN(OLC);
}
<OLC>\n\r? {
    BEGIN(INITIAL);
}
<OLC>. {
}
%%
int yywrap () {
    return 1;
}
$cat test.c
#包括
#包括
int main(int argc,char*argv[]){
放置(“你好,世界”);
testwrap();
}
$cat测试
%s OLC
%%
-- {
开始(OLC);
}
\n\r\n?{
开始(首字母);
}
. {
}
%%
int-yywrap(){
返回1;
}

我使用的是gradle 5.1.1。

我不必定义多个项目,但我必须定义两个构建脚本。当然,存在脆性,但至少它是有效的

这是
build.gradle

task LC (type: Exec) {
    inputs.file(project.file('test.l'))
    outputs.dir(buildDir)
    outputs.file("$buildDir/lex.test.c")
    commandLine 'flex', '-f', '-L', '-8', '-i', '-P', 'test', '-o', "$buildDir/lex.test.c", 'test.l'
}

task copyC (type: Copy) {
    from "."
    into buildDir
    include "*.c"
}

task build (type: GradleBuild) {
    dependsOn 'LC', 'copyC'
    buildFile = 'compileC.gradle'
    tasks = ['build']
}
apply plugin: 'c'

model {
    components {
        test(NativeExecutableSpec) {
            sources {
                c {
                    source {
                        srcDir buildDir
                        include "*.c"
                    }
                }
            }
        }
    }
}
这是compileC.gradle:

task LC (type: Exec) {
    inputs.file(project.file('test.l'))
    outputs.dir(buildDir)
    outputs.file("$buildDir/lex.test.c")
    commandLine 'flex', '-f', '-L', '-8', '-i', '-P', 'test', '-o', "$buildDir/lex.test.c", 'test.l'
}

task copyC (type: Copy) {
    from "."
    into buildDir
    include "*.c"
}

task build (type: GradleBuild) {
    dependsOn 'LC', 'copyC'
    buildFile = 'compileC.gradle'
    tasks = ['build']
}
apply plugin: 'c'

model {
    components {
        test(NativeExecutableSpec) {
            sources {
                c {
                    source {
                        srcDir buildDir
                        include "*.c"
                    }
                }
            }
        }
    }
}

该任务似乎是在Gradle配置阶段的稍后阶段创建的

我建议在tasks容器中添加一个侦听器,这在您的示例中非常有效:

project.tasks.whenObjectAdded { Task t -> 
    println 'Task added: ' + t.name
    if ('buildDependentsTestExecutable'.equals(t.name)) {
        t.dependsOn compileLang
    }
}
我不熟悉'c'插件或模型{}配置,因此我的建议是基于我对Gradle的一般熟悉程度