运行java主类的Groovy脚本



下面是我试图用gradle运行的主类的代码。我在下面附上了java和gradle脚本的示例主类。

public class Hello {
   public static void main(String[] args) throws IOException {
      System.out.println("This is file system watch service"); 
      Path dir = Paths.get("c:\sid\");
      WatchService service = FileSystems.getDefault().newWatchService();
      WatchKey key = dir.register(service, ENTRY_CREATE);
      System.out.println("Watching directory: "+dir.toString());
      //file creation logic
      while(true) {
         for (WatchEvent<?> event: key.pollEvents()) {
            WatchEvent.Kind<?> kind = event.kind();
            if (kind == OVERFLOW) {
               continue;
            }
            WatchEvent<Path> ev = (WatchEvent<Path>)event;
            Path filename = ev.context();
         }
      }
   }
}

我的渐变脚本如下

apply plugin: 'java'
task hello1 << {
   def process = ['java', '-cp', 'sourceSets.main.runtimeClasspath',  'com.test.gradle.Hello'].execute()
   process.in.close()
   process.out.close()
   process.err.close()
}
task hello << {
   ant.java(classpath:'sourceSets.main.runtimeClasspath', classname:'com.test.gradle.Hello', fork:'true')
   ant.echo('Done')
}

当我从命令提示符调用gradle hello1时,它说构建成功,但我的主程序似乎从未执行过。为了查看主程序的执行情况,我使用PrintWriter添加了示例sysout和文件创建逻辑。我也试过用等级为hello的蚂蚁,这也不起作用。

现在,当我使用下面的渐变任务时,从以前的中删除了fork-true

task hello << {
    ant.java(classpath:'sourceSets.main.runtimeClasspath.asPath', classname:'com.test.gradle.Hello.class')
    ant.echo('Done')
}
it is generating below exception
[ant:java] Could not find com.test.gradle.Hello.class. Make sure you have it in
your classpath
        at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:13
8)
        at org.apache.tools.ant.taskdefs.Java.run(Java.java:771)
        at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:221)
        at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:135)
        at org.apache.tools.ant.taskdefs.Java.execute(Java.java:108)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.jav
a:106)
        at groovy.util.AntBuilder.performTask(AntBuilder.java:319)
        at groovy.util.AntBuilder.nodeCompleted(AntBuilder.java:264)
        at org.gradle.api.internal.project.ant.BasicAntBuilder.nodeCompleted(Bas
icAntBuilder.java:72)
        at groovy.util.BuilderSupport.doInvokeMethod(BuilderSupport.java:147)
        at groovy.util.AntBuilder.doInvokeMethod(AntBuilder.java:203)
        at org.gradle.api.internal.project.ant.BasicAntBuilder.doInvokeMethod(Ba
sicAntBuilder.java:87)
        at groovy.util.BuilderSupport.invokeMethod(BuilderSupport.java:64)
        at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaC
lassSite.java:45)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSi
teArray.java:45)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCa
llSite.java:108)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCa
llSite.java:116)
        at build_1dcr9mg141bsc4tjjgplovccq0$_run_closure2.doCall(C:Userssumit
workspace1gradleTestbuild.gradle:13)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:
90)

试试这个

javaexec {
    main = 'com.pack.YourClass'
    classpath(sourceSets.src.output.classesDir, sourceSets.src.compileClasspath)
    args(arg1, arg2)
}

相关内容

  • 没有找到相关文章

最新更新