2012-02-03 80 views
5

如何转换的Java类名到使用Ant任务文件路径?蚂蚁:将类名的文件路径

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

我试过(也失败了),以<pathconvert><regexpmapper>的条款实施。

回答

0

这里的另一种方式,使用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 
+0

该解决方案将工作**仅如果只有**的资源可以被其他地方找到。别的,用[@马特](http://stackoverflow.com/a/9135198/1099452)的溶液中。 – lucasvc 2014-12-09 14:23:13

3

这里是一个可能的方式做到这一点:

<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.classfile.name财产。

0

我觉得使用ant脚本的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}" /> 

注:在命名变量相同的参数e.g VAR wsPath可能会错误,它给了我!

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