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

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

问题描述

在 groovy 中,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.直接调用 unzip 是不可能的.我假设这就是 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:

有什么想法吗?

我曾尝试在 google 文档/示例中使用 java 中的 AntBuilder,但我只找到了使用 groovy 的示例.

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 类:

Given this Java class:

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天全站免登陆