如何将参数或参数传递给 gradle 任务 [英] How to pass parameters or arguments into a gradle task

查看:42
本文介绍了如何将参数或参数传递给 gradle 任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 gradle 构建脚本,我试图在其中包含 Eric Wendelin 的 css 插件 - http://eriwen.github.io/gradle-css-plugin/

I have a gradle build script into which I am trying to include Eric Wendelin's css plugin - http://eriwen.github.io/gradle-css-plugin/

它很容易实现,而且因为我只想要缩小(而不是组合和 gzip 压缩),所以我的构建脚本的相关部分如下所示:

Its easy enough to implement, and because I only want minification (rather than combining and gzipping), I've got the pertinent parts of the build script looking like this:

minifyCss {
    source = "src/main/webapp/css/brandA/styles.css"
    dest = "${buildDir}/brandA/styles.css"
    yuicompressor {
        lineBreakPos = -1
    }
}

war {
    baseName = 'ex-ren'
}

war.doFirst {
    tasks.myTask.minifyCss.execute()
}

这是完美的 - 当我运行 gradle war 任务时,它调用 minifyCss 任务,获取源 css 文件,并在 buildDir 中创建一个缩小版本

This is perfect - when I run the gradle war task, it calls the minifyCss task, takes the source css file, and creates a minified version in the buildDir

但是,我有一些 css 文件需要缩小,但没有合并到一个文件中(因此我没有使用 combineCss 任务)

However, I have a handful of css files which need minify-ing, but not combining into one file (hence I'm not using the combineCss task)

我希望能够做的是使某种类型的 minifyCss 任务引用变量的 source 和 dest 属性(假设这是正确的术语?) - 在签名中传递到任务中的变量,或全局变量变量,什么的......

What I'd like to be able to do is make the source and dest properties (assuming that's the correct terminology?) of the minifyCss task reference variables of some sort - either variables passed into the task in the signature, or global variables, or something ...

我猜是这样的(不起作用):

Something like this I guess (which doesn't work):

minifyCss(sourceFile, destFile) {
    source = sourceFile
    dest = destFile
    yuicompressor {
        lineBreakPos = -1
    }
}

war {
    baseName = 'ex-ren'
}

war.doFirst {
    tasks.myTask.minifyCss.execute("src/main/webapp/css/brandA/styles.css", "${buildDir}/brandA/styles.css")
    tasks.myTask.minifyCss.execute("src/main/webapp/css/brandB/styles.css", "${buildDir}/brandB/styles.css")
    tasks.myTask.minifyCss.execute("src/main/webapp/css/brandC/styles.css", "${buildDir}/brandC/styles.css")
}

这也不起作用:

def sourceFile = null
def destFile = null

minifyCss {
    source = sourceFile
    dest = destFile
    yuicompressor {
        lineBreakPos = -1
    }
}

war {
    baseName = 'ex-ren'
}

war.doFirst {
    sourceFile = "src/main/webapp/css/brandA/styles.css"
    destFile = "${buildDir}/brandA/styles.css"
    tasks.myTask.minifyCss.execute()
}

在我的一生中,我无法弄清楚如何调用任务并在 :(

For the life of me I cannot work out how to call a task and pass variables in :(

非常感谢任何帮助;

推荐答案

我想你可能希望将每组 css 的缩小视为一个单独的任务

I think you probably want to view the minification of each set of css as a separate task

task minifyBrandACss(type: com.eriwen.gradle.css.tasks.MinifyCssTask) {
     source = "src/main/webapp/css/brandA/styles.css"
     dest = "${buildDir}/brandA/styles.css"
}

etc etc

顺便说一句,在战争任务的一个动作中执行你的缩小任务对我来说似乎很奇怪——让它们成为战争任务的依赖不是更有意义吗?

BTW executing your minify tasks in an action of the war task seems odd to me - wouldn't it make more sense to make them a dependency of the war task?

这篇关于如何将参数或参数传递给 gradle 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆