簡體   English   中英

從Java使用groovy.util.AntBuilder

[英]Using groovy.util.AntBuilder from java

在groovy中,可以使用groovy.util.AntBuilder例如。 將文件解壓縮到文件夾中:

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

現在我想從Java上做同樣的事情。 無法直接調用解壓縮。 我假設那是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:

有任何想法嗎?

我曾嘗試通過google來搜索使用AntBuilder的docs / examples,但僅在groovy中使用過該示例。

是的,進入一台計算機然后試一下:

給定此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;
  }
} });

我已經找到了解決方案。 ;-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM