使用ANT替换xml中的值

时间:2012-06-11 09:10:15

标签: ant xml-parsing

我想在< DefineConstants>处替换ProductVersion值以下xml的节点。必须保留该行的剩余部分。 例如:

<DefineConstants>XXXXX;ProductVersion=20.323.23;YYYYY</DefineConstants>

<DefineConstants>XXXXX;ProductVersion=21.58.44;YYYYY</DefineConstants>

我尝试了replaceregexp,但它更改了剩余的内容。

<replaceregexp file="${basedir}\Installer.wixproj"
     match="ProductVersion=([0-9].*);(.*)"
     replace="ProductVersion=${SpaceVersion};\1"
     byline="true"
/>

你可以指导我在这方面做错了吗。

XML:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
      <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
      <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
      <ProductVersion>3.5</ProductVersion>
      <ProjectGuid>{6bc0a85b-9c15-41e6-874b-5fe07e5338e6}</ProjectGuid>
      <SchemaVersion>2.0</SchemaVersion>
      <OutputName>Installer</OutputName>
      <OutputType>Package</OutputType>
      <WixTargetsPath Condition=" '$(WixTargetsPath)' == '' AND '$(MSBuildExtensionsPath32)' != '' ">$(MSBuildExtensionsPath32)\Microsoft\WiX\v3.x\Wix.targets</WixTargetsPath>
      <WixTargetsPath Condition=" '$(WixTargetsPath)' == '' ">$(MSBuildExtensionsPath)\Microsoft\WiX\v3.x\Wix.targets</WixTargetsPath>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
      <OutputPath>bin\$(Configuration)\</OutputPath>
      <IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
      <DefineConstants>Debug;</DefineConstants>
      <WixVariables>
      </WixVariables>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
      <OutputPath>bin\$(Configuration)\</OutputPath>
      <IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
      <DefineConstants>SourceDir=$(SolutionDir)XSSE\;ProductVersion=3.3.1.75;ProductName=XSSE;ToolchinShortcut=XSSEToolchinShortcut;ExtensionDir=XSSE;ManifestFileName=extension.vsixmanifest;PkageDefFileName=XSSE.ToolchainProvider.pkgdef;REGKEY=Software\Microsoft\XSSE;</DefineConstants>
      <WixVariables>XYZ=123;</WixVariables>
   </PropertyGroup>
   <ItemGroup>
      <Compile Include="filefragment.wxs" />
      <Compile Include="Product.wxs" />
   </ItemGroup>
   <ItemGroup>
      <WixExtension Include="WixUIExtension">
         <HintPath>$(WixExtDir)\WixUIExtension.dll</HintPath>
         <Name>WixUIExtension</Name>
      </WixExtension>
   </ItemGroup>
   <Import Project="$(WixTargetsPath)" /> 

2 个答案:

答案 0 :(得分:1)

我认为在产品版本之后不必尝试捕获剩余的行。

您无需担心这一点。如果正则表达式单独留下其余文本,则只能替换ProductVersion。

我成功了:

<replaceregexp file="${basedir}\Installer.wixproj"
    match="ProductVersion=[0-9]+\.?[0-9]+\.?[0-9]+\.?[0-9]+\.?;"
    replace="ProductVersion=${SpaceVersion};"
    byline="true"
    />

答案 1 :(得分:0)

您需要使用\2代替\1

<replaceregexp file="${basedir}\Installer.wixproj"
       match="ProductVersion=([0-9].*);(.*)"
       replace="ProductVersion=${SpaceVersion};\2"
       byline="true"
        />

独立测试:

<project default="test">

    <property name="SpaceVersion" value="a.b.c"/>

    <target name="test">
      <replaceregexp file="test.xml"
         match="ProductVersion=([0-9].*);(.*)"
         replace="ProductVersion=${SpaceVersion};\2"
         byline="true"
       />
    </target>
</project>

将更改的文件与原始文件进行比较:

$diff test.xml test.xml.0
2c2
<     <DefineConstants>XXXXX;ProductVersion=a.b.c;YYYYY</DefineConstants>
---
>     <DefineConstants>XXXXX;ProductVersion=20.323.23;YYYYY</DefineConstants>