Ant 任务确定文件是否为只读

发布于 2024-11-07 14:35:50 字数 126 浏览 0 评论 0原文

我需要编写一个ant任务来确定某个文件是否是只读的,如果是,则失败。我想避免使用自定义选择器来执行此操作,这符合我们构建系统的性质。有人知道如何去做这件事吗?我正在使用 ant 1.8 + ant-contrib。

谢谢!

I need to write an ant task to determine if a certain file is readonly, and if it is, fail. I would like to avoid using a custom selector to do this do to the nature of our build system. Anyone have any ideas how to go about doing this? I'm using ant 1.8 + ant-contrib.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

残疾 2024-11-14 14:35:50

像这样的事情会起作用吗?

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

这将检查和失败作为一个不同的操作而不是两个操作,因此您可能会发现它更清晰,并且不需要使用属性名称,因此您的命名空间更干净。

Will something like this do the trick?

<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" />

This uses the condition task and the isfileselected condition (not a direct link - you'll have to search down the page) combined with the writable selector (and reversed with a not condition).

Update:

An possibly better alternative would be:

<fail message="${the.file.in.question} is not writeable">
  <condition>
    <not>
      <isfileselected file="${the.file.in.question}">
        <writable />
      </isfileselected>
    </not>
  </condition>
</fail>

This has the check and the fail as one distinct action rather than two, so you may find it clearer, and it doesn't require using a property name, so your namespace is cleaner.

萝莉病 2024-11-14 14:35:50

我确信有更好的方法,但我会提出一些可能的方法。

  • 使用复制任务创建临时副本,然后尝试复制此文件以覆盖原始文件。 failonerror 属性会派上用场
  • 使用 java 任务来执行一个任务,该任务执行一些简单的代码,例如:

    文件 f = 新文件( 路径 );
    f.canWrite()……

I'm sure there are better ways but I'll throw a few possible methods out there.

  • Use the copy task to create a temp duplicate then attempt to copy this file to overwrite the original. The failonerror attribute will come in handy
  • Use the java task to execute a task which executes some simple code such as:

    File f = new File( path );
    f.canWrite()......

天荒地未老 2024-11-14 14:35:50

编写一个供自定义条件使用怎么样? href="http://ant.apache.org/manual/Tasks/conditions.html" rel="nofollow">条件 任务?它更加灵活。

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>

What about writing a custom condition to be used by the condition task? It is more flexible.

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

Integrate with

<typedef
  name="isreadonly"
  classname="IsReadOnly"
  classpath="${myclasses}"/>

and use it like

<condition property="readonly">
  <isreadonly>
    <file file="${file}"/>
  </isreadonly>
</condition>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文