I want to use ant to run sql scripts for a build. I grab all these sql scripts files from a specific folder, so I know how to grab them and run them using the sql ant task. But I want to perform this task little different.
1. I want to grab the user id, password and url from a file (data source file).
2. If the script is throwing some error, I want to log that error/display the error in the console and move on.
3. I want to perform the above steps only if I am on my local machine/or in a predefined environment.
4. Finally, I want to jar-up the new class and add to the class path.
I haven’t done much in terms of overriding the ant task or extending it to perform additional tasks. So please give me some ideas of how to do this. I really appreciate any help on this.
1. You can do this already. Place the user/password as properties in a properties file, then use <property> task to read the properties file, and then use property value to pas the userid to the <sql> task:
2. The output property of the <sql> task already defaults to System.out, so doesn't that cause the script errors to be shown in the console? Or are you saying that the first script that runs into an error stops everything which prevents the rest of the scripts from running? If that is what you mean, try setting the "onerror" property of the <sql> task to "continue".
3. You should be able to do the above conditionally via a property setting. Look at the "depends" and "unless" properties of Ant targets.
4. Yes you can do this is you write your own task class. Not sure what the issue is here - this is just standard Java compile and package, followed by a copy.
If you still want to customize the existing sql task, I recommend spending some time with the source for that task. Look it over, compile it, package it, etc. Rename the class and create an <sql2> tasks that does the exact same thing. Once you have that figured out, you should be ready to tackle customization.
Thank you. Even though my intention was to override some methods to find out if the table already exists before executing a create table command for example; this should suffice for now.
Joined: Nov 15, 2005
This solution works generally. One problem I see is that, I have all the “*.sql” files in the release folder, say release\2.1\sql\. So when there are no files in that folder, my build script throws an error and stops since my onerror continue is for each sql queries. Is there any simple way to find if there is any file exists in the release\2.1\sql\ folder before I proceed to execute these scripts at all?
Thank in advance.
Take a look at the <condition> task and the nested <resourceexists> test. That task conditionally sets a property. Then use that property in the "if" attribute of the target that does the SQL processing.