Ant 文件集扩展不起作用

发布于 2024-09-10 14:59:45 字数 518 浏览 10 评论 0原文

我从 ant 构建文件中得到了非常令人困惑的反应,我想知道是否我不够聪明,或者这实际上可能是一个错误。

我在我的项目中全局设置了以下属性:

<property name="lib.dir" location="lib"/>

然后我将尝试通过文件集(多个资源)将此目录中的一些文件添加到 jar 文件中:

<fileset dir="${basedir}" includes="lib/*filename*"/>

应该有(并且存在)3 个不同的库,就是这样匹配的。但是,如果我尝试使用以下内容,它不起作用,并且不包含任何文件:

<fileset dir="${basedir}" includes="${lib.dir}/*filename*"/>

请注意,唯一的区别在于全局属性的使用。现在有一个简单的问题:为什么第一个版本可以像宣传的那样工作,但第二个版本却不能?

I get a very confusing reaction from my ant build-file and I'm wondering whether I'm just not clever enough or this might actually be a bug.

I've got the following property set globally in my project:

<property name="lib.dir" location="lib"/>

Then I'll try to add some files out of this directory into a jar file via fileset (more than one resource):

<fileset dir="${basedir}" includes="lib/*filename*"/>

There should be (and exist) 3 different libraries, which are matched that way. However, if I try to use the following, it doesn't work and no files are included:

<fileset dir="${basedir}" includes="${lib.dir}/*filename*"/>

Note that the only differences lies in the usage of the global property. Now the simple question: why does the first version work as advertised, but the second doesn't?

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

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

发布评论

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

评论(4

泪之魂 2024-09-17 14:59:46

请在使用“fileset”表达式的任务之前或之后检查“lib.dir”的实际值。只是为了确保在全局设置后它没有被意外更改。 任务可以提供帮助。


也许我找到了解决方案。 location属性的描述是:

将该属性设置为给定文件的绝对文件名。如果此属性的值是绝对路径,则保持不变(将 / 和 \ 字符转换为当前平台约定)。否则,它将被视为相对于项目的 basedir 的路径并进行扩展。

只需使用 value 属性而不是 location 即可。这是一个测试脚本来显示差异:

<project name="test">

    <property name="test1" location="lib"></property>
    <property name="test2" value="lib"></property>

    <target name="target" description="description">
       <echo>${test1}</echo>
       <echo>${test2}</echo>
    </target>

</project>

我的系统上的输出如下:

Buildfile: D:\Develop\workspace-jabber\scrapbook\build.xml
target:
     [echo] D:\Develop\workspace-jabber\scrapbook\lib
     [echo] lib
BUILD SUCCESSFUL
Total time: 307 milliseconds

Please check the actual value of "lib.dir" just before and maybe after the task that uses the "fileset" expression. Just to make sure, that it hasn't been changed accidently after you've set it globally. The <echo/> task can help.


Maybe I got the solution. The description of the locationattribute is:

Sets the property to the absolute filename of the given file. If the value of this attribute is an absolute path, it is left unchanged (with / and \ characters converted to the current platforms conventions). Otherwise it is taken as a path relative to the project's basedir and expanded.

Simply use the value attribute instead of location. Here's a test script to show the difference:

<project name="test">

    <property name="test1" location="lib"></property>
    <property name="test2" value="lib"></property>

    <target name="target" description="description">
       <echo>${test1}</echo>
       <echo>${test2}</echo>
    </target>

</project>

The output on my system is as follows:

Buildfile: D:\Develop\workspace-jabber\scrapbook\build.xml
target:
     [echo] D:\Develop\workspace-jabber\scrapbook\lib
     [echo] lib
BUILD SUCCESSFUL
Total time: 307 milliseconds
ま昔日黯然 2024-09-17 14:59:46

我已经找到了答案的线索,但还没有找到全部。

我使用 ant -debug 运行了两个版本的文件集,结果如下。

在工作的、不使用属性的版本中,我得到以下输出:

fileset: Setup scanner in dir [pathToDir] with patternSet{ includes: [lib/*filename*] excludes: [] }

而在应该工作但不工作的版本中,我得到:

fileset: Setup Scanner in dir [pathToDir] with patternSet{ include: [ [pathToDir]/lib/*filename*] excepts: [] }

如您所见,ant add 的是正则表达式中的 [pathToDir],因此搜索

[pathToDir]/[pathToDir]/lib/*filename*

显然不存在的路径。现在的问题是:我该如何修改我的版本才能使其正常工作?

I have found a clue to the answer, but not the whole thing yet.

I runned both versions of the fileset with ant -debug and here is what happens.

In the working, not-using-property version, I get the following output:

fileset: Setup scanner in dir [pathToDir] with patternSet{ includes: [lib/*filename*] excludes: [] }

whereas in the should-be-working-but-doesn't version I get:

fileset: Setup scanner in dir [pathToDir] with patternSet{ includes: [ [pathToDir]/lib/*filename*] excludes: [] }

As you can see, ant add's the [pathToDir] in the regexp, thus searching for

[pathToDir]/[pathToDir]/lib/*filename*

which obviously doesn't exist. Problem now: how do I have to modify my version to have it working properly?

樱娆 2024-09-17 14:59:46

创建属性时(是在全局还是在目标中完成?),目录 lib 是否存在?如果不是,则 location 属性不起作用 - 请改用 value 属性,或者更好地在创建目录后定义该属性。

When creating the property (is it done global or in a target?), does the directory lib exist? If not, the location attribute does not work - use a value attribute instead or better define the property after creating the directory.

煞人兵器 2024-09-17 14:59:46

如上所述,问题是 ${lib.dir} 也包含整个路径,因此搜索 [pathToDir]/[pathToDir]/lib/filename

为了剪掉 ${lib.dir} 属性中不需要的 [pathToDir],我现在使用了该任务。我现在得到了以下内容,但恕我直言,看起来不太好看的解决方案:

<basename property="lib.dir.rel" file="${lib.dir}"/>
<fileset dir="${basedir}" includes="${lib.dir.rel}/*filename*"/>

PS:再看一遍,我发现 Andreas_D 昨天也找到了正确的理由和一个很好的建议,我一定忽略了:-/

As indicated above, the problem was that ${lib.dir} too contained the whole path, thus searching for [pathToDir]/[pathToDir]/lib/filename.

To clip away the unwanted [pathToDir] in the ${lib.dir} property, I now used the task. I got now the following, but imho not so nice looking solution:

<basename property="lib.dir.rel" file="${lib.dir}"/>
<fileset dir="${basedir}" includes="${lib.dir.rel}/*filename*"/>

P.S.: On a second look, I found that Andreas_D also found the right reason and a good suggestion yesterday, which I must have overlooked :-/

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