Java – Save information between tasks in Ant build scripts


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

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.

Best Solution

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

<property name="smsuser"

Then you can reference the property later, a la:

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