从Java中使用groovy.util.AntBuilder [英] Using groovy.util.AntBuilder from java

查看:528
本文介绍了从Java中使用groovy.util.AntBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在常规的groovy.util.AntBuilder可用于如。解压缩文件到文件夹:

In groovy the groovy.util.AntBuilder can be used to eg. unzip a file into a folder:

  AntBuilder ant = new AntBuilder();
  ant.unzip(src: file.getPath(), dest: outputFolder.getPath());

现在我想这样做相同的,但在Java。它不可能直接调用解压。我认为,多数民众赞成在invokeMethod中是什么:

Now I would like to do the same but from java. Its not possible to call unzip directly. I assume thats what the invokeMethod is for:

  AntBuilder ant = new AntBuilder();
  String[] args = new String[4];
  args[0] = "src";
  args[1] = file.getPath();
  args[2] = "dest";
  args[3] = outputFolder.getPath();
  ant.invokeMethod("unzip", args);

上面给出了:

 No signature of method: groovy.util.AntBuilder.unzip() is applicable for argument types: (java.lang.String, java.lang.String, java.lang.String, java.lang.String) values:

任何想法?

我曾尝试谷歌文档/示例使用从Java的AntBuilder,但我只找到它的地方,从常规使用的例子。

I have tried to google docs/examples for using the AntBuilder from java, but I have only found examples where its used from groovy.

推荐答案

右键,得到了一台电脑,并给它一去:

Right, got to a computer and gave it a go:

由于这个Java类:

import groovy.util.AntBuilder ;
import java.io.File ;
import java.util.HashMap ;

public class Test {
  public static void main( String[] args ) throws Exception {
    if( args.length != 2 ) {
      System.err.println( "Need 2 args.  Input zip file and output folder" ) ;
      System.exit( 1 ) ;
    }
    final File file = new File( args[ 0 ] ) ;
    final File outputFolder = new File( args[ 1 ] ) ;
    AntBuilder ant = new AntBuilder() ;
    ant.invokeMethod( "unzip", new HashMap() {{
      put( "src", file.getPath() ) ;
      put( "dest", outputFolder.getPath() ) ;
    }} ) ;
  }
}

您可以再用编译:

javac -cp $GROOVY_HOME/embeddable/groovy-all-2.0.5.jar Test.java 

然后运行:

java -cp $GROOVY_HOME/lib/*:. Test /path/to/zip /destination/folder

和它应该工作

这篇关于从Java中使用groovy.util.AntBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆