Ant: convert class name to file path

How to convert Java class names to file paths using Ant tasks?

For example, if a property containing is set foo.bar.Duck, I would like to exit foo/bar/Duck.class.

I tried (and could not) implement this from the point of view of <pathconvert>and <regexpmapper>.

+5
source share
3 answers

Here's another way using Ant resources and unpackagemapperthat is designed for this purpose. The opposite is also available 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>

You use the resource value using the property helper syntax ${toString:...}, for example:

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

Productivity

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

Here is a possible way to do this:

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

+3

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

Please note: this name of your variable matches the argument, for example, var wsPath may give an error, it gave me!

courtesy: fooobar.com/questions/686434 / ...

0
source

All Articles