MSBuild: Permanently Change PropertyGroup Property of a Project

穿精又带淫゛_ 提交于 2019-12-11 06:15:36

问题


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>

回答1:


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"
     />


来源:https://stackoverflow.com/questions/6347989/msbuild-permanently-change-propertygroup-property-of-a-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!