我想做的是在 build.gradle 中创建一个任务,该任务将执行一个主类(具有 main 方法的类),但我不知道如何。
我做了一个测试项目来测试如何做到这一点。这是文件结构布局:
testProject/
build.gradle
src/main/groovy/hello/world/HelloWorld.groovy
下面是 build.gradle 的内容:
apply plugin: 'groovy'
apply plugin: 'maven'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.0.6'
}
task( hello, dependsOn: jar, type: JavaExec ) {
main = 'hello.world.HelloWorld'
}
下面是 HelloWorld.groovy 的内容:
package hello.world
class HelloWorld {
public static void main(String[] args) {
println "Hello World!"
}
}
这是我从 shell 中得到的:
testProject>$ gradle hello
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:hello
Error: Could not find or load main class hello.world.HelloWorld
:hello FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':hello'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 4.232 secs
所以,我的问题是:我怎样才能gradle hello
工作?非常感谢你。