Gradle 不执行任务

时间:2021-06-15 12:52:20

标签: gradle

所以,这是我的 gradle 任务。 我想从 src/main/sql 复制一些 sql 文件到 build/sql,同时根据 .properties 文件过滤一些数据。

import org.apache.tools.ant.filters.ReplaceTokens
task buildSQL(type: Copy) {
    def db_names = ['oracle', 'postgresql', 'h2']
    db_names.each{db -> 
        copy {
            from('src/main/sql') 
            include '*sql'
            def properties = new Properties()
            file('src/main/sql/' + db + '_token.properties').withInputStream{
                properties.load(it);   
            }
            filter(ReplaceTokens, tokens: properties)
            rename 'vorlage', db
            into 'build/sql'
        }
    }
}

当我运行 gradle buildSQL 时,这个任务基本上可以正常工作。 当我运行 gradle clean buildSQL 时,任务不会被执行。

我必须做什么才能运行“buildSQL”任务?

2 个答案:

答案 0 :(得分:2)

任务可能永远不会执行,因为它没有任何输入。唯一运行的是构建配置时的 copy 方法。

  • 当您只运行 buildSQL 任务时,copy 方法会在配置阶段运行,然后构建完成,因为没有带输入的任务。
  • 当您运行 clean buildSQL 时,copy 方法会在配置阶段运行,并且构建会在执行阶段执行 clean 任务 - 这会删除刚刚创建的副本。

如果我的解释没有立即有意义,我建议阅读 Gradle’s build phases。也许运行 gradle --console=verbose clean buildSQL 也会让我们更清楚地了解正在发生的事情。

抛开理论不谈,以下是修复配置的方法:

import org.apache.tools.ant.filters.ReplaceTokens
task buildSQL(type: Copy) {
    def db_names = ['oracle', 'postgresql', 'h2']
    db_names.each { db ->
        def properties = new Properties()
        file("src/main/sql/${db}_token.properties").withInputStream {
            properties.load(it)
        }
        from('src/main/sql') {
            include '*sql'
            filter(ReplaceTokens, tokens: properties)
            rename 'vorlage', db
        }
    }
    into 'build/sql'
}

换句话说,只需删除 the copy method,但为 CopySpec 任务保留其 Copy

答案 1 :(得分:2)

@Chriki 已经解释了(主要)问题,所以我将添加一个可能的解决方案:

reduceApplyListOfArrays<- function(x){
     y<-apply(array(unlist(x), c(dim(x[[1]]), dim(x[[2]]), length(x))), 
              c(1,2), mean)
     colnames(y)<-colnames(x[[1]])
     rownames(y)<-rownames(x[[1]])
  return(y)
}
相关问题