Reputation: 16287
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());
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);
the above gives:
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:
Any ideas?
I have tried to google docs/examples for using the AntBuilder from java, but I have only found examples where its used from groovy.
Upvotes: 2
Views: 5463
Reputation: 21
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;
}
} });
I have found this solution. ;-)
Upvotes: 2
Reputation: 171084
Right, got to a computer and gave it a go:
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() ) ;
}} ) ;
}
}
You can then compile it with:
javac -cp $GROOVY_HOME/embeddable/groovy-all-2.0.5.jar Test.java
And then run:
java -cp $GROOVY_HOME/lib/*:. Test /path/to/zip /destination/folder
And it should work
Upvotes: 4