Java 是否有Ant任务来检查符号链接是否悬空?
我的构建系统中有一些符号链接指向JAR,如果JAR不存在,我需要构建这些JAR。i、 e.如果符号链接悬空。是否有Ant任务或解决方法来检查这一点Java 是否有Ant任务来检查符号链接是否悬空?,java,ant,Java,Ant,我的构建系统中有一些符号链接指向JAR,如果JAR不存在,我需要构建这些JAR。i、 e.如果符号链接悬空。是否有Ant任务或解决方法来检查这一点 至于为什么我不能将适当的Ant依赖性包含到这些JAR中,原因是它们的构建过程很长,涉及从ftp存储库中动态下载Internet,这需要很长时间,我无法控制。好的,所以最后我实现了一个自定义Ant任务(最后的代码),可以从Ant中使用,如下所示: <file-pronouncer filePath="path/to/file" retProper
至于为什么我不能将适当的Ant依赖性包含到这些JAR中,原因是它们的构建过程很长,涉及从ftp存储库中动态下载Internet,这需要很长时间,我无法控制。好的,所以最后我实现了一个自定义Ant任务(最后的代码),可以从Ant中使用,如下所示:
<file-pronouncer filePath="path/to/file" retProperty="prop-holding-type-of-that-file"/>
FileFinator自定义Ant任务的代码
发现了一个相关的openjdk错误,这可能会使这个需求产生问题:我建议使用ivy进行依赖关系管理。它可以在本地缓存从远程存储库(ftp、http等)下载的文件
<echo message="the file-pronouncer task for file 'path/to/file' returned: ${prop-holding-type-of-that-file}"/>
[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);
}
}
}