Question

Is it possible to have a dynamically set attribute in Ant?

At the moment all of our wars have the same name: "mywar.war". I'd like to have wars built with JDK6 to be "mywar-1.6.war" and those built with JDK7 to still just be "mywar.war".

Currently:

<war warfile="${dist.dir}/mywar.war">
..
<war />

I can include the Java version like this:

<war warfile="${dist.dir}/mywar-${ant.java.version}.war">

But what I'd like to have is the logic to do something like this:

if (${ant.java.version} == "1.6") {
  war.name="mywar-${ant.java.version}";
} else {
  war.name="mywar";
}

<war warfile="${dist.dir}/${war.name}.war">

Is there a correct way to go about that?

Était-ce utile?

La solution

The way to set properties conditionally is to use the condition task:

<condition property="war.suffix" value="-1.6" else="">
  <equals arg1="${ant.java.version}" arg2="1.6"/>
</condition>

<war warfile="${dist.dir}/mywar${war.suffix}.war">

The trick here is that the else="" means that war.suffix will be an empty string on any version other than 1.6.

Autres conseils

Yes you can use some thing like that but it's not available in ant as such. You will have to download ant-contrib.jar

And then you have some thing like this

<if>
 <equals arg1="${ant.java.version}" arg2="1.6" />
 <then>
   war.name="mywar-${ant.java.version}";
 </then>
 <else>
   war.name="mywar";
 </else>
</if>

See this link for ant contrib

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top