I want to define a target using a property as part of its name attribute, but the property doesn't seem to resolve.
<property name="foo" value="FOO" />
<target name="${foo}.init.win32" />
<antcall target="${foo}.init.win32" />
The error I get is: Target "FOO.init.win32.x86" does not exist in the project.
I guess Ant doesn't allow this behavior?
Yes, ant doesn't allow variable name of the target. Otherwise dependency calculation can be very difficult task
Related
I have a requirement where in a build.xml file I have two variables.
While calling the build.xml file from Java program, I will pass the values as properties
And while calling the same build.xml file from command line, it should ask the user for input
Now I have to check whether a particular property is having any value or not, if not, then ask user to input the value
Can you please help me to write this build.xml file?
Thanks in advance.
You can do it like this:
<target name="printMyProperty" depends="askUser">
<echo message="${my.property}"/>
</target>
<target name="askUser" unless="my.property">
<input
message="Enter the value for my.property:"
addproperty="my.property"
/>
</target>
The unless attribute is the solution to your problem. It means "execute this target only if my.property is not set".
i have a requirement that as follows.
I have a .properties file (with name=value pair) from which i am reading couple of properties.
i want to check a particular property exist or not.
i am getting the error with if doesn't support the "name" attribute for the following code.
where JavaProjectName,projDir are the names getting from the .properties file.
<if name="${JavaProjectName}" exists="true">
<property name="importJavaProject" value="${projDir}/${JavaProjectName}"/>
</if>
can you please tell me where am i doing wrong.
Read the document of <if> task first. It doesn't support the way you wrote.
It should be:
<if>
<isset property="JavaProjectName" />
<then>
<property name="importJavaProject" value="${projDir}/${JavaProjectName}"/>
</then>
</if>
However, you want to set a property importJavaProject when another property JavaProjectName has been set before (in the build file or in a properties file imported). So, what if JavaProjectName has not been set?
You should either think of an <else> part, or fail the build.
If you just want to check for existence and fail the build when it does not exist, just use <fail>:
<fail unless="JavaProjectName"/>
Also check Condition task and "Supported conditions".
Addition:
Also read the question posted by ManMohan in the comment more carefully. For "check the property existence in .properties file", the accepted answer of that question checks both "whether the property has been set" and "whether its value is empty".
I would like to be able to pass an optional parameter to an ant task call, so that it appears as unset to the called target if it is omitted (important in conditions or when using the unless or if attributes). I don't know if optArg is set, so I cannot hard-code the parameter.
Example: The ant target someTarget requires a mandatory parameter mandatoryArg and supports an optional parameter optArg.
I can achieve this by cludgy duplication like so:
<if><equals arg1="${optArg}" arg2="true" />
<then>
<ant dir="someDir" target="someTarget" inheritAll="false">
<property name="mandatoryArg" value="42" />
<property name="optArg" value="true" />
</ant>
</then>
<else>
<ant dir="someDir" target="someTarget" inheritAll="false">
<property name="mandatoryArg" value="42" />
</ant>
</else>
</if>
I would like to leave out the if statement so that the ant call is not duplicated, without losing the optional nature of optArg. I already found this question, which appears to cover a different problem.
I use Ant 1.7 and ant-contrib.
Any ideas? Thanks!
EDIT: I also accept answers that explain why it really isn't possible.
You might be able to get the behavior you want by using the <if> block to build a property set, and then referencing it inside a single <ant> task call later with <propertyset refid="whatever"/>
I want to set a property to a filename, for which I know a pattern that will match a unique file. For example, I have the file:
plugins/doc.en_20110608.zip
I define in my ant file:
<property name="doc.zip" location="plugins/doc.en_*.zip" />
I know the pattern will match only one file. The problem is that ant doesn't try to match any pattern at this point, and fails because there is no file named plugins/doc.en_*.zip.
If I'm not using a fileset, it's because the property can be substituted where a fileset is not allowed, like destfile attribute of zip task.
<zip destfile="${doc.zip}" update="true"> ... </zip>
The answer is no I think, but you can use reference/path shortcuts to 'stringify' a fileset into a property. Something like:
<fileset id="doc.zip.fs" dir="plugins" includes="doc.en_*.zip"/>
<property name="doc.zip" value="${toString:doc.zip.fs}" />
I'm writing a velocity macro within which I have some ant tasks. Within a #foreach loop in the velocity macro, I have a pathconvert task:
#foreach(<iterate through something>)
<pathconvert property='filename' refid='swf.file'>
<mapper>
<chainedmapper>
<flattenmapper/>
<globmapper from='*-d.swf' to='*'/>
</chainedmapper>
</mapper>
</pathconvert>
#end
The problem I have is that the 'filename' property gets set only once, during the first iteration, since properties in ANT are immutable.
But I need the filename to be set during each iteration. Is there a way to get this done?
If there was a way to reset the property, I could do that at the end of each iteration. Or is there a better way to do this?
Any help would be highly appreciated!
Thanks in advance,
Anand
You could use ant-contrib's variables. They act like mutable properties.
http://ant-contrib.sourceforge.net/tasks/tasks/variable_task.html
Use the new lexically scoped properties in Ant 1.8:
"Lexically scoped local properties, i.e. properties that are only defined inside a target, sequential block or similar environment."
Annoucement.
Properties in Ant were designed to be immuatable, but they gave in to popular demand and gave us variables. Your alternative is to write a custom task ( in Java or a Dynamic Language) but this seems like a good compromise.
The following snippet illustrates an ant property which I guess is not documented. Properties are immutable, but references are mutable. So any data type which has no name, but a reference, is mutable. For example a fileset. But today I found a way to have a kind of mutable property. Connected with local task or some other tricks it may be a way of having variables in ant.
<property name="a" value="aaa" id="refa" />
<property name="b" refid="refa" />
<echo>${b}</echo>
<property name="c" value="ccc" id="refa" />
<property name="d" refid="refa" />
<echo>${d}</echo>
The output is:
aaa
ccc
Although in both cases a reference refa is printed.
Here is a post about it. And another one.
Use a combination of for + let task from Ant Plugin Flaka to overwrite existing properties.
See some snippets here.