5 ответов:
Я использую scriptdef для создания тега javascript для подстроки, например:
<project> <scriptdef name="substring" language="javascript"> <attribute name="text" /> <attribute name="start" /> <attribute name="end" /> <attribute name="property" /> <![CDATA[ var text = attributes.get("text"); var start = attributes.get("start"); var end = attributes.get("end") || text.length(); project.setProperty(attributes.get("property"), text.substring(start, end)); ]]> </scriptdef> ........ <target ...> <substring text="asdfasdfasdf" start="2" end="10" property="subtext" /> <echo message="subtext = ${subtext}" /> </target> </project>
Можно попробовать использовать PropertyRegex из Ant-Contrib.
<propertyregex property="destinationProperty" input="${sourceProperty}" regexp="regexToMatchSubstring" select="\1" casesensitive="false" />
Поскольку я предпочитаю использовать vanilla Ant, я использую временный файл. Работает везде, и вы можете использовать replaceregex, чтобы избавиться от части строки, которую вы не хотите. Пример для munging git сообщений:
<exec executable="git" output="${git.describe.file}" errorproperty="git.error" failonerror="true"> <arg value="describe"/> <arg value="--tags" /> <arg value="--abbrev=0" /> </exec> <loadfile srcfile="${git.describe.file}" property="git.workspace.specification.version"> <filterchain> <headfilter lines="1" skip="0"/> <tokenfilter> <replaceregex pattern="\.[0-9]+$" replace="" flags="gi"/> </tokenfilter> <striplinebreaks/> </filterchain> </loadfile>
Я бы пошел с грубой силой и написал пользовательскую муравьиную задачу:
public class SubstringTask extends Task { public void execute() throws BuildException { String input = getProject().getProperty("oldproperty"); String output = process(input); getProject().setProperty("newproperty", output); } }Что осталось сделать, чтобы реализовать
String process(String)и добавить пару сеттеров (например, для значенийoldpropertyиnewproperty)
Я бы использовал script task для этой цели, я предпочитаю ruby, пример отрезать первые 3 символа =
<project> <property name="mystring" value="foobarfoobaz"/> <target name="main"> <script language="ruby"> $project.setProperty 'mystring', $mystring[3..-1] </script> <echo>$${mystring} == ${mystring}</echo> </target> </project>Выход =
main: [echo] ${mystring} == barfoobazИспользование Ant api с проектом метода.setProperty() на существующем объекте свойство перезапишет его, таким образом, вы можете обойти стандартный ant поведение, означает, что свойства, однажды заданные, неизменны
Comments