如何将参数或参数传递给Gradle任务 [英] How to pass parameters or arguments into a gradle task
问题描述
我有一个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屋!