ant compilation resets the value of property

There are already N many alternatives to ant, but it is not that ant is not available, and its cross-platform nature still has great advantages.

Android is often built using ant.

 

The property in the ant script cannot be reset by default. After the first definition, it will not change during the entire project execution process.

The simplest way to define it is as follows:

 

<property name="build.target" value="release" />

But in our compilation scripts, it is often necessary to use variable values. Another value setting method can satisfy us:

 

<var name="build.target" value="release" />

Defining variables can be reset at any time, but there is another point, if we need to use condition to conditionally set the value, sorry, var is not supported. 

<condition property="BATCH_INPUT" value="${env.BATCH_INPUT}" else="${local.BATCH_INPUT}">
          <isset property="env.BATCH_INPUT" />
</condition>

 

Directly, it is not impossible for property to support reset. After jdk 1.6, it can be supported by contrib . You need to define a reset script. You need to download ant-contrib-0.6.jar to the lib directory of ant :

<scriptdef name="propertyreset" language="javascript"
   		description="Allows to assign @{property} new value">
    	<attribute name="name"/>
    	<attribute name="value"/>
       	project.setProperty(attributes.get("name"), attributes.get("value"));
</scriptdef>

Also add the following line of declaration:

<taskdef resource="net/sf/antcontrib/antlib.xml" />

 

The usage is as follows:

<propertyreset name="chanel" value="${chanelname}"/>

 This way you can use the new value directly.

Complete example from this post:

 

<project name="test" default="testA" basedir=".">
    
     <!--This tag must be defined-->
    <taskdef resource="net/sf/antcontrib/antlib.xml" />
    
    <scriptdef name="propertyreset" language="javascript"
    		description="Allows to assign @{property} new value">
   		  <attribute name="name"/>
    		<attribute name="value"/>
        project.setProperty(attributes.get("name"), attributes.get("value"));
		</scriptdef>


 		<property name="nameA" value="A"/>
 		<!--Redefine property, does not work-->
 		<property name="nameA" value="B"/>
 		
 		<var name="nameB" value="A"/>
 		<!--var can set the value at will -->
 		<var name="nameB" value="B"/>
 		
 		
 		<property name="nameC" value="A"/>
 		<!--Use a custom script to reset the value-->
 		<propertyreset name="nameC" value="B"/>
 		
    
    
   <target name='testA'>
     <echo>${nameA}</echo>
     <echo>${nameB}</echo>
     <echo>${nameC}</echo>  
   </target>
</project>

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326489968&siteId=291194637