繁体   English   中英

有Ant任务来检查符号链接是否悬空吗?

[英]is there an Ant task to check if a symlink is dangling or not?

我的构建系统中有一些符号链接,指向如果不存在jar则需要构建的jar。 即符号链接是否悬空。 是否有Ant任务或解决方法来检查?

至于为什么我不能对这些jar包含适当的Ant依赖,原因是它们的构建过程很长,涉及从ftp存储库中进行即时Internet下载,这花费的时间太长,而且超出了我的控制范围。

好的,最后我实现了一个自定义的Ant任务(最后是代码),可以像这样从Ant中使用它:

<file-pronouncer filePath="path/to/file" retProperty="prop-holding-type-of-that-file"/>

然后可以使用以下命令读取它:

<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

FilePronouncer定制Ant任务的代码

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);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM