特定のファイルが読み取り専用かどうかを判断する Ant タスクを作成する必要があり、読み取り専用の場合は失敗します。ビルドシステムの性質上、カスタムセレクターを使用してこれを行うことは避けたいと思います。誰でもこれを行う方法について何か考えがありますか? ant 1.8 + ant-contrib を使用しています。
ありがとう!
特定のファイルが読み取り専用かどうかを判断する Ant タスクを作成する必要があり、読み取り専用の場合は失敗します。ビルドシステムの性質上、カスタムセレクターを使用してこれを行うことは避けたいと思います。誰でもこれを行う方法について何か考えがありますか? ant 1.8 + ant-contrib を使用しています。
ありがとう!
このようなものはトリックを行いますか?
<condition property="file.is.readonly">
<not>
<isfileselected file="${the.file.in.question}">
<writable />
</isfileselected>
</not>
</condition>
<fail if="file.is.readonly" message="${the.file.in.question} is not writeable" />
これは、condition
タスクとisfileselected
条件(直接リンクではありません。ページを下方向に検索する必要があります) をwritable
セレクターと組み合わせて使用します(条件で反転しnot
ます)。
おそらくより良い代替手段は次のとおりです。
<fail message="${the.file.in.question} is not writeable">
<condition>
<not>
<isfileselected file="${the.file.in.question}">
<writable />
</isfileselected>
</not>
</condition>
</fail>
これには、チェックと失敗が 2 つではなく 1 つの別個のアクションとして含まれているため、より明確であることがわかります。また、プロパティ名を使用する必要がないため、名前空間がよりクリーンになります。
条件タスクで使用するカスタム条件を作成するのはどうですか? より柔軟です。
public class IsReadOnly extends ProjectComponent implements Condition
{
private Resource resource;
/**
* The resource to test.
*/
public void add(Resource r) {
if (resource != null) {
throw new BuildException("only one resource can be tested");
}
resource = r;
}
/**
* Argument validation.
*/
protected void validate() throws BuildException {
if (resource == null) {
throw new BuildException("resource is required");
}
}
public boolean eval() {
validate();
if (resource instanceof FileProvider) {
return !((FileProvider)resource).getFile().canWrite();
}
try {
resource.getOutputStream();
return false;
} catch (FileNotFoundException no) {
return false;
} catch (IOException no) {
return true;
}
}
}
統合する
<typedef
name="isreadonly"
classname="IsReadOnly"
classpath="${myclasses}"/>
そしてそれを次のように使用します
<condition property="readonly">
<isreadonly>
<file file="${file}"/>
</isreadonly>
</condition>
もっと良い方法があると確信していますが、いくつかの可能な方法を紹介します。
java タスクを使用して、次のような単純なコードを実行するタスクを実行します。
ファイル f = 新しいファイル(パス); f.canWrite()......