建设有脚本可执行的JAR和嵌入式Groovy是与Gradle简单。
的优点是验证该脚本是可编译和单元部署到生产环境之前进行测试。
下面是一个例子:
脚本/ SRC /主/常规/ script1.groovy
println "Hello World!"
def aTestableMethod() {
1 + 1
}
脚本/ SRC /测试/常规/ script1Test.groovy
import org.junit.Test
import static org.junit.Assert.*
class Script1Test {
@Test
public void checkATestableMethod() {
def script1 = new script1()
assertEquals 2, script1.aTestableMethod()
}
}
scripts/build。gradle这个
apply plugin: 'groovy'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.2.2'
testCompile 'junit:junit:4.11'
}
jar {
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
configurations.runtime.collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes 'Main-Class': 'script1'
}
}
编译并执行:
$ gradle build
$ java -jar build/libs/scripts/scripts.jar
Hello World!
的
Groovy Environment Manager (GVM)使得它易于安装和管理Groovy和相关工具,包括
Gradle。
你应该保持逻辑的分离,并创建一个“/目标”文件夹w /你的构建工具(gradle或其他)来收集生成的类在标准化文件夹内。 –
我一直在考虑使用gradle进行构建,但考虑到.groovy文件只能直接执行,因此它在工作流程中添加了一个步骤。我想我不能两种方式(直接从文本和干净的部署灵活执行)。感谢您的建议。 – haventchecked
我同意,我认为从长远来看,干净部署更加灵活和可靠。 –