Using groovy.util.AntBuilder from java



在groovy中,groovy.util.antbuilder可用于例如。将文件解压缩到文件夹中:

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

现在我想做同样的事情,但是来自爪哇。它不可能直接致电解压缩。我认为这就是InvokeMethod的目的:

  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的蚂蚁Builder使用Google Docs/示例,但我只找到了它使用的示例。

对,到了计算机,然后给了它:

给定此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

它应该起作用

import groovy.lang.Closure;
import groovy.util.AntBuilder;

...

final AntBuilder ant = new AntBuilder();
ant.invokeMethod("echo", "copy & sync gestartet...");
ant.invokeMethod("sync", new Object[] { new HashMap<String, String>() {
  {
    this.put("todir", "./myordner2");
    this.put("verbose", "yes");
  }
}, new Closure<Object>(null) {
  @Override
  public Object call(Object... args) {
    ant.invokeMethod("fileset", new Object[] {
        new HashMap<String, String>() {
          {
            this.put("dir", "c:/myordner1/test");
          }
        }});
    return null;
  }
} });

我找到了这个解决方案。; - )

最新更新