ant - how to check if a file exist
I was asking my special friend google today if he can help me with a simple thing. I needed an ant task to check if a file exist on the harddisk. But google told me, nothing found so after 5 minutes of thinking I decided ok now write your own ant task for this. shouldn't be so hard at all.
so I check at google again how to do this and thats the result:
package edu.ucdavis.genomics.metabolomics.ant.task;
import java.io.File;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
/**
* @author wohlgemuth
* sets a variable to true if a file exist or to false
*/
public class FileExist extends Task{
private String variableToSet;
private String file;
/**
*
*/
public FileExist() {
super();
}
public String getFile() {
return this.file;
}
public void setFile(String file) {
this.file = file;
}
public String getVariableToSet() {
return this.variableToSet;
}
public void setVariableToSet(String variableToSet) {
this.variableToSet = variableToSet;
}
public void execute() throws BuildException {
super.execute();
if(file == null){
throw new BuildException("file must be set to a value");
}
if(variableToSet == null){
throw new BuildException("variable must be set to a value");
}
File f = new File(file);
if(f.exists()){
this.getProject().setUserProperty(this.getVariableToSet(),"true");
variableToSet = "true";
}
else{
this.getProject().setUserProperty(this.getVariableToSet(),"false");
}
}
}
you just need the baseclass Task, overwrite one method and define a couple of getter and setters.
Done!
an example in an actual buildfile for this task:
<taskdef classname="edu.ucdavis.genomics.metabolomics.ant.task.FileExist" classpathref="lib" name="exist">
</taskdef>
<exist file="lib" variabletoset="test" />
<echo>${test}</echo>
so I check at google again how to do this and thats the result:
package edu.ucdavis.genomics.metabolomics.ant.task;
import java.io.File;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
/**
* @author wohlgemuth
* sets a variable to true if a file exist or to false
*/
public class FileExist extends Task{
private String variableToSet;
private String file;
/**
*
*/
public FileExist() {
super();
}
public String getFile() {
return this.file;
}
public void setFile(String file) {
this.file = file;
}
public String getVariableToSet() {
return this.variableToSet;
}
public void setVariableToSet(String variableToSet) {
this.variableToSet = variableToSet;
}
public void execute() throws BuildException {
super.execute();
if(file == null){
throw new BuildException("file must be set to a value");
}
if(variableToSet == null){
throw new BuildException("variable must be set to a value");
}
File f = new File(file);
if(f.exists()){
this.getProject().setUserProperty(this.getVariableToSet(),"true");
variableToSet = "true";
}
else{
this.getProject().setUserProperty(this.getVariableToSet(),"false");
}
}
}
you just need the baseclass Task, overwrite one method and define a couple of getter and setters.
Done!
an example in an actual buildfile for this task:
<taskdef classname="edu.ucdavis.genomics.metabolomics.ant.task.FileExist" classpathref="lib" name="exist">
</taskdef>
<exist file="lib" variabletoset="test" />
<echo>${test}</echo>
Created by
zwluxx
Last modified 2005-10-06 10:39 AM
Last modified 2005-10-06 10:39 AM