开发者

MSBuild: Permanently Change PropertyGroup Property of a Project

I was hoping to find a way to set a value in my csproj file during my build to a value. Is there a task in MSBuild that I can use to set a property permanently to a value? In the example below, can I set CustomValue = Yes permanently?

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
   开发者_运维百科 .....
    <CustomValue>XXXX</CustomValue
    <FileAlignment>512</FileAlignment>
    <ProjectTypeGuids></ProjectTypeGuids>
    <SccProjectName>SAK</SccProjectName>
    <SccLocalPath>SAK</SccLocalPath>
    <SccAuxPath>SAK</SccAuxPath>
    <SccProvider>SAK</SccProvider>
  </PropertyGroup>


You can use the XmlPoke task to do that. It seems a little odd to be altering projects this way though. Alternatively, you can set up a tiny import file,

<!-- in your main project file, right below the PropertyGroup -->
<Import
  Condition="Exists('Custom.props')"
  Project="Custom.props"
  />

Then dynamically create this property file, as,

<?xml version="1.0" encoding="utf-8"?>
<Project
  xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
  ToolsVersion="4.0">
  <PropertyGroup>
    <CustomValue>True</CustomValue>
  </PropertyGroup>
</Project>

You can either use XmlPoke on just this .props file, or use WriteLinesToFile to create the entire file. This secondary file wouldn't need to be checked into source control, the condition on the import makes the project functional when the file doesn't exist.

The XmlPoke task would look like this,

  <XmlPoke
     XmlInputPath="./Custom.props"
     Namespaces="&lt;Namespace Prefix='x'
        Uri='http://schemas.microsoft.com/developer/msbuild/2003'/&gt;"
     Query="//x:PropertyGroup/x:CustomValue/@Value"
     Value="True"
     />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜