【问题标题】:Why do I get an error when I try to add a dependent task to one created by the C plugin?当我尝试将依赖任务添加到由 C 插件创建的任务时,为什么会出现错误?
【发布时间】:2019-04-17 17:11:16
【问题描述】:

我有一个使用 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 tasks”时配置的相关任务:

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;
}

我正在使用 gradle 5.1.1。

【问题讨论】:

    标签: gradle


    【解决方案1】:

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

    我建议在任务容器上添加一个侦听器,它在您的示例中对我有用:

    project.tasks.whenObjectAdded { Task t -> 
        println 'Task added: ' + t.name
        if ('buildDependentsTestExecutable'.equals(t.name)) {
            t.dependsOn compileLang
        }
    }
    

    我不熟悉“c”​​插件或模型{}配置,所以我的建议是基于我对 Gradle 的一般熟悉程度。

    【讨论】:

      【解决方案2】:

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

      这是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']
      }
      

      这是compileC.gradle

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

      【讨论】:

        猜你喜欢
        • 2015-07-08
        • 2014-03-30
        • 1970-01-01
        • 1970-01-01
        • 2016-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多