I have some symlinks in my build system that point to jars which I need to build if the jars don't exist. ie if the symlinks are dangling. Is there an Ant task or workaround to check that?
As to why I can't include a proper Ant dependency to those jars, the reason is that their build process is lengthy, involving on-the-fly Internet downloads from ftp repositories that take too long and is out of my control.
Ok, so in the end I implemented a custom Ant task (code at the end), that can be used from Ant like this:
<file-pronouncer filePath="path/to/file" retProperty="prop-holding-type-of-that-file"/>
It can then be read with:
<echo message="the file-pronouncer task for file 'path/to/file' returned: ${prop-holding-type-of-that-file}"/>
With the following possible outcomes:
[echo] the file-pronouncer task for file 'a' returned: regular-file
[echo] the file-pronouncer task for file 'b' returned: symlink-ok
[echo] the file-pronouncer task for file 'c' returned: symlink-dangling
[echo] the file-pronouncer task for file 'd' returned: not-exists
import java.io.IOException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.file.FileSystems;
import java.nio.file.LinkOption;
import java.nio.file.attribute.BasicFileAttributes;
import org.apache.tools.ant.BuildException;
public class FilePronouncer extends Task {
private String filePath = null;
private String retProperty = null;
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getRetProperty() {
return retProperty;
}
public void setRetProperty(String property) {
this.retProperty = property;
}
public void execute() {
try {
Path path = FileSystems.getDefault().getPath(filePath);
boolean fileExists = Files.exists(path, LinkOption.NOFOLLOW_LINKS);
Boolean isSymlink = null;
Boolean filePointedToExists = null;
if (fileExists) {
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
isSymlink = attrs.isSymbolicLink();
if (isSymlink)
filePointedToExists = Files.exists(path);
}
Project project = getProject();
String rv = null;
if (!fileExists)
rv = "not-exists";
else {
if (!isSymlink)
rv = "regular-file";
else {
if (filePointedToExists)
rv = "symlink-ok";
else
rv = "symlink-dangling";
}
}
project.setProperty(retProperty, rv);
} catch (IOException e) {
throw new BuildException(e);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.