Gili
Gili

Reputation: 90150

Ant: Convert class name to file path

How do I convert Java class names into file paths using Ant tasks?

For example, given a property containing foo.bar.Duck I'd like to get out foo/bar/Duck.class.

I tried (and failed) to implement this in terms of <pathconvert> and <regexpmapper>.

Upvotes: 6

Views: 1232

Answers (3)

old-monk
old-monk

Reputation: 931

I feel using ant script-javascript for this is much simpler

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

note: that naming your variable same as argument e.g var wsPath may give error, it gave to me!

courtesy: https://stackoverflow.com/a/16099717/4979331

Upvotes: 0

martin clayton
martin clayton

Reputation: 78215

Here's another way, using Ant resources and an unpackagemapper, which is designed for this purpose. The opposite package mapper is also available.

<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 by means of the property helper syntax ${toString:...}, e.g.:

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

Yields

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

Upvotes: 1

matt
matt

Reputation: 79803

Here's 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>

This puts the desired foo/bar/Duck.class into the file.name property.

Upvotes: 4

Related Questions