簡體   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