我终于让Gradle下载了依赖,并将这些依赖传递给了一个我从gralde任务调用的groovy脚本(因为gradle似乎不允许我使用葡萄)。
以下代码是我能够使其正常运行的唯一方式。这是正确的方法吗?
的build.gradle:
configurations {
shell
}
// Specify dependancies
dependencies {
// Groovy Script task dependancies
shell 'org.codehaus.groovy.modules.http-builder:http-builder:0.6'
shell 'org.codehaus.groovy:groovy-all:2.3.0'
// actual application dependancies
compile ...
}
task cleanupArtifactory (dependsOn: configurations.shell) << {
//Now add those dependencies to the root classLoader:
URLClassLoader loader = GroovyObject.class.classLoader
configurations.shell.each {File file -> loader.addURL(file.toURL()) }
new GroovyShell().run(file('scripts/artifactory.groovy'))
}
答案 0 :(得分:1)
只需将您的groovy脚本视为类,然后使用JavaExec运行它们。
这是一个例子
task yourTask(type: JavaExec, dependsOn: classes) {
description = "Does some stuff"
if (project.hasProperty('args')) {
// this is just a fancy regex to get all the args from '-Pargs="-a -b -c"' and passing them to the main class
def myArgs = (project.args =~ /([^\s"']+)|["']([^'"]*)["']/).collect{it[1] ?: it[2]}
args myArgs
}
main = 'your.GroovyClass'
classpath configurations.compile, configurations.runtime, sourceSets.main.output
}
“args”位正好可以用
调用任务gradle yourTask -Pargs="-a -b somevalue"
将值直接传递给类。
关于葡萄的主题,我有一个工作配置,它允许我从gradle调用/编译groovy类,并允许Grab符号工作,以便能够直接调用脚本。
在你的groovy类中添加适当的抓取和导入,例如:
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.6')
import groovyx.net.http.*
在项目的build.gradle中,添加以下内容:
configurations {
ivy
}
dependencies {
ivy 'org.apache.ivy:ivy:2.3.0'
}
tasks.withType(GroovyCompile) { groovyClasspath += configurations.ivy }
现在我可以直接用
调用groovy脚本groovy /path/to/the/GroovyClass -a -b somevalue
或使用之前定义的gradle任务。
如果我不添加那些常春藤代码,那么抓住它的类就无法通过gradle编译。