Tutorial :Save information between tasks in Ant build scripts



Question:

In am writing a Java API for an SMS gateway, and I have some trouble with my ANT build script.

Basically what I would like to do is to be able to only specify the username and password for the service in one place in the build file but be able to call the task that sends an SMS from anywhere in the build file. What I have now is this:

<smsConfig password="${sms.password}" user="${sms.user}">    <sms msg="England prevails" to="XXXXXXXX"/>  </smsConfig>  

The execute method in the sms task class gets the values from the smsConfig class when the task is executed, and the SMS is sucessfully sent.

What I would like to have is this:

<smsConfig  password="${sms.password}" user="${sms.user}"/>  

Later, perhaps in a different task:

<sms msg="England prevails" to="XXXXXXXX"/>  

Unfortunately if I do that the static values stored in the SmsConfig class can no longer be accessed (I get a null pointer exception, even though they had a value when the execute method in the SmsConfig class ran).

So how (and where) do I store the value between the smsConfig task and any sms tasks?

Added to make the problem clear based on the feedback from the first two questions:

I would like to keep the configuration to talk to the gateway separated from the action of sending the SMS for two reasons:

  1. The information is necessary to actually send an SMS, but it is mental clutter - the user does not care how the SMS is sent, only that it is.
  2. The information would have to be repeated anywhere the SMS is sent, if I put the value into a property that would avoid having to repeat the actual values, but the property would still have to be repeated each place I send an SMS.


Solution:1

Unless I'm reading you wrong, what you want is a property:

<property name="smsuser"   value="${sms.user}"/>  

Then you can reference the property later, a la:

<sms msg="England prevails" to="${smsuser}"/>  


Solution:2

It sounds to me like you should create a target to send the SMS, then you can use the AntCall task to call it whenever you want, with appropriate properties.

Unfortunately it's not entirely clear what the smsConfig and sms tasks do here. I'd really expect an sms task contain its own configuration (possibly as a nested element) rather than being a nested element inside an smsConfig.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »