Java 是否可以替换Ant';s build.xml?
我有一个属性app.version,它设置为1.2.0(当然,总是在更改),需要创建名为“something-ver-1_2_0”的zip文件。这可能吗?可以使用zip任务Java 是否可以替换Ant';s build.xml?,java,ant,Java,Ant,我有一个属性app.version,它设置为1.2.0(当然,总是在更改),需要创建名为“something-ver-1_2_0”的zip文件。这可能吗?可以使用zip任务 <zip zipfile="something-ver-${app.version}.zip"> <fileset basedir="${bin.dir}" prefix="bin"> <include name="**/*" /> </fileset> <fil
<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>
有关zip任务的更多信息:由于属性
app.version
总是在更改,我假设您不想将其硬编码到属性文件中,而是在生成时传递它。此外,您可以在命令行上尝试以下操作:
ant -f build.xml -Dapp.version=1.2.0
将应用程序版本更改为所需版本
编辑:
从反馈中更好地理解您的问题。ant没有字符串操作任务,您需要为此编写自己的。这里是一个结尾。另一种方法是使用正则表达式从文件到属性的版本号,如本例所示:
<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>
您可以使用该任务将“.”替换为“\u”,并分配给新属性:
<?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>
${app.version}已转换为${app.version.下划线}
属性不能更改,但antContrib变量()可以更改
下面是一个宏,用于对变量执行查找/全部替换:
<macrodef name="replaceVarText">
<attribute name="varName" />
<attribute name="from" />
<attribute name="to" />
<sequential>
<local name="replacedText"/>
<local name="textToReplace"/>
<local name="fromProp"/>
<local name="toProp"/>
<property name="textToReplace" value = "${@{varName}}"/>
<property name="fromProp" value = "@{from}"/>
<property name="toProp" value = "@{to}"/>
<script language="javascript">
project.setProperty("replacedText",project.getProperty("textToReplace").split(project.getProperty("fromProp")).join(project.getProperty("toProp")));
</script>
<ac:var name="@{varName}" value = "${replacedText}"/>
</sequential>
</macrodef>
project.setProperty(“replacedText”、project.getProperty(“textToReplace”).split(project.getProperty(“fromProp”)).join(project.getProperty(“toProp”));
然后调用宏,如下所示:
<ac:var name="newFileName" value="${app.version}"/>
<current:replaceVarText varName="newFileName" from="." to="_" />
<echo>Filename will be ${newFileName}
文件名将为${newFileName}
这将得到“something-ver-1.2.0.zip”,我需要收到“something-ver-1_2_0.zip”。也许,这是不可能的?:)pathconvert可用于在ANT中执行字符串操作。'mapper还包括对正则表达式的支持。