Ant:将类名转换为文件路径

时间:2012-02-03 17:18:28

标签: ant

如何使用Ant任务将Java类名转换为文件路径?

例如,给定包含foo.bar.Duck的属性,我想要foo/bar/Duck.class

我尝试(并且失败)以<pathconvert><regexpmapper>来实现这一点。

3 个答案:

答案 0 :(得分:4)

这是一种可行的方法:

<property name="class.name" value="foo.bar.Duck"/>

<loadresource property="file.name">
  <string value="${class.name}" />
  <filterchain>
    <replaceregex pattern="\." replace="/" flags="g" />
    <replaceregex pattern="$" replace=".class" />
  </filterchain>
</loadresource>

这会将所需的foo/bar/Duck.class放入file.name属性。

答案 1 :(得分:1)

这是另一种方法,使用Ant resourcesunpackagemapper,这是为此目的而设计的。相反的package mapper也可用。

<property name="class.name" value="foo.bar.Duck"/>

<resources id="file.name">
  <mappedresources>
    <string value="${class.name}" />
    <unpackagemapper from="*" to="*.class" />
  </mappedresources>
</resources>

您可以通过属性助手语法${toString:...}使用资源值,例如:

<echo message="File: ${toString:file.name}" />

产量

[echo] File: foo/bar/Duck.class

答案 2 :(得分:0)

我觉得使用ant script-javascript更加简单

        <property name="class.name" value="foo.bar.duck" />
        <script language="javascript">
            var className = project.getProperty("class.name");
            println("before: " + className);
            var filePath= className.replace("\\", "/");
            println("File Path: "+filePath);
            project.setProperty("filePath", filePath);              
        </script>
        <echo message="${filePath}" />

注意:将变量命名为与参数相同,例如var wsPath可能会给出错误,它给了我!

礼貌:https://stackoverflow.com/a/16099717/4979331