Is it possible to replace text in properties in Ant build.xml?

I have a property, app.version, which is set to 1.2.0 (and, of course, always changes) and you need to create a zip file called "something-ver-1_2_0". Is it possible?

+3
source share
4 answers

You can use the pathconvert task to replace "." with "_" and assign a new property:

<?xml version="1.0" encoding="UTF-8"?> <project> <property name="app.version" value="1.2.0"/> <pathconvert property="app.version.underscore" dirsep="" pathsep="" description="Replace '.' with '_' and assign value to new property"> <path path="${app.version}" description="Original app version with dot notation" /> <!--Pathconvert will try to add the root directory to the "path", so replace with empty string --> <map from="${basedir}" to="" /> <filtermapper> <replacestring from="." to="_"/> </filtermapper> </pathconvert> <echo>${app.version} converted to ${app.version.underscore}</echo> </project> 
+7
source

Another approach is to filter the version number from a file into a property using a regular expression, as suggested in this example:

 <loadfile srcfile="${main.path}/Main.java" property="version"> <filterchain> <linecontainsregexp> <regexp pattern='^.*String VERSION = ".*";.*$'/> </linecontainsregexp> <tokenfilter> <replaceregex pattern='^.*String VERSION = "(.*)";.*$' replace='\1'/> </tokenfilter> <striplinebreaks/> </filterchain> </loadfile> 
+2
source

It is possible to use a zip task

 <zip zipfile="something-ver-${app.version}.zip"> <fileset basedir="${bin.dir}" prefix="bin"> <include name="**/*" /> </fileset> <fileset basedir="${doc.dir}" prefix="doc"> <include name="**/*" /> </fileset></zip> 

Additional information about the zip task: http://ant.apache.org/manual/Tasks/zip.html

0
source

Since the app.version property app.version always changing, I assume that you do not want to hard-code it into property files, but rather pass it when you build. In addition to this answer , you can try the following on the command line:

 ant -f build.xml -Dapp.version=1.2.0 

change app.version to required.

Edit:

I better understood your question from the feedback. Unfortunately ant has no string manipulation tasks, you need to write your own task . The following is an example.

0
source

Source: https://habr.com/ru/post/1314464/


All Articles