ANT opening and writing into a file

Hi
I have been trying to use ANT to open a file and write content into it. I used to exec to run notepad.exe or cmd.exe and output attribute to define a file. But after creating the file I want some custom content to be written into the file. How do I do that. I thought I could use inputstring attribute but I cannot use it, I have tried redirector element.

I just want my ANT script file to open a file and write something into it (say "hello!").
Thanks
NGujja
ngujjaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
yes filters take this example very known in J2ME builds, i have a file (anything.jad) that contains something like this:

MIDlet-Jar-Size: @jarsize@
....

this file is considered a template i want to replace @jarsize@ with a value at running ant with the Jar size (for example)
hence I have an ant task like this:

    <filter token="jarsize" value="120000" />
    <copy todir="c:\output" filtering="true" overwrite="true">
      <fileset dir="c:\input" includes="anything.jad" />
    </copy>

assuming the template is at c:\input
the output at c:\output will look like this:

MIDlet-Jar-Size: 120000
....

0
kiranhkCommented:
You need to create a task of your own and then write whatever contents you want into that file.


import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class MyTask extends Task {
    private String msg;

    // The method executing the task
    public void execute() throws BuildException {
        System.out.println(msg);
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
            out.write(msg);
            out.close();
    } catch (IOException e) {
    }

    }

    // The setter for the "message" attribute
    public void setMessage(String msg) {
        this.msg = msg;
    }
}


Now you can add this in your build.xml as an ant task

   1. Make sure the class that implements your task is in the classpath when starting Ant.
   2. Add a <taskdef> element to your project. This actually adds your task to the system.
   3. Use your task in the rest of the buildfile.

Example

<?xml version="1.0"?>

<project name="OwnTaskExample" default="main" basedir=".">
  <taskdef name="mytask" classname="MyTask"/>
  <target name="main">
    <mytask message="Hello World! MyTask works!"/>
  </target>
</project>

hope this helps
0
petmagdyCommented:
if it is just writing text to file it just can be




<project name="Write file" default="all" basedir=".">

      <property name="message" value="hello dear" />

<target name="all" >
<echo message="${message}"  file="out.txt" />

</target>

</project>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

ngujjaAuthor Commented:
hey petmagdy thanks for the comment. I dont want to use filter, Iam already using it with tokens and doing pretty much what you have posted for some other task.

hey Kiran thanks for the info. seems your comment could be the solution. iam in a meeting right now, will try your suggestion soon, and let you know and give you points.
0
petmagdyCommented:
please also see my last comment
0
ngujjaAuthor Commented:
God I can't believe it was so simple. Thanks petmagdy. I will probably use Kiran's approach. It's more generic.
Thanks both of you.
0
ngujjaAuthor Commented:
Sorry to bother again folks.
How can I echo "<" or ">" character. Ant says I can't use these characters. I tried to escape them using / and \, but no success.
0
kiranhkCommented:
use &gt; and &lt; for the same
0
ngujjaAuthor Commented:
Hey Kiran Thanks I had got the solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.