MSBuild:永久更改项目的 PropertyGroup 属性

发布于 2024-11-15 13:24:23 字数 703 浏览 5 评论 0原文

我希望找到一种方法在构建过程中将 csproj 文件中的值设置为某个值。 MSBuild 中是否有一个任务可以用来将属性永久设置为某个值?在下面的示例中,我可以永久设置 CustomValue = Yes 吗?

<?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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ偶尔ゞ 2024-11-22 13:24:23

您可以使用 XmlPoke 任务来执行此操作。不过,以这种方式改变项目似乎有点奇怪。或者,您可以设置一个小型导入文件,

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

然后动态创建此属性文件,因为,

<?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>

您可以仅在此 .props 文件上使用 XmlPoke,或使用 WriteLinesToFile 创建整个文件。该辅助文件不需要签入源代码管理,导入条件使项目在该文件不存在时正常运行。

XmlPoke 任务看起来像这样,

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

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="<Namespace Prefix='x'
        Uri='http://schemas.microsoft.com/developer/msbuild/2003'/>"
     Query="//x:PropertyGroup/x:CustomValue/@Value"
     Value="True"
     />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文