使用MSBuild,如何将属性设置为文件的内容?

时间:2022-11-22 07:33:12

I have a file that I set using PowerShell that contains the version number of my build. I need to get this within MSBuild so I can act on it within my build script. It seems simple enough; I just want to take the contents of the file and set a property to that value.

我有一个使用PowerShell设置的文件,其中包含我的构建版本号。我需要在MSBuild中获取它,以便我可以在构建脚本中对其进行操作。看起来很简单;我只想获取文件的内容并将属性设置为该值。

I thought maybe doing an Exec task, doing a "more" on my file, and capturing standard out would do the trick, but I can't seem to get this to work. It appears that others have had problems with stdout and MSBuild as well. Here is what I have tried:

我想可能正在做一个Exec任务,在我的文件上做一个“更多”,并且捕获标准输出就可以了,但我似乎无法让它工作。似乎其他人也遇到了stdout和MSBuild的问题。这是我尝试过的:

<Exec Command="more $(BuildDirectory)\version.txt" Outputs="stdout">
    <Output TaskParameter="Outputs" ItemName="BuildNumber" />
</Exec>

1 个解决方案

#1


The ReadLinesFromFile task is what you want

ReadLinesFromFile任务就是您想要的

<ReadLinesFromFile File="Version.Txt">
    <Output TaskParameter="Lines" Item="BuildNumber"/>
</ReadLinesFromFile>

that said, another way to do what your question implies is to store you build num info in an xml file, with a MSBuild schema

这就是说,你的问题所暗示的另一种方法是使用MSBuild模式将你的数据信息存储在xml文件中

something like

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
 <PropertyGroup>
   <BuildNumber>10</BuildNumber>
   <RevNumber>5</RevNumber>
 </PropertyGroup>
</Project>

and then import the version.properties file into your main msbuild file

然后将version.properties文件导入主msbuild文件

#1


The ReadLinesFromFile task is what you want

ReadLinesFromFile任务就是您想要的

<ReadLinesFromFile File="Version.Txt">
    <Output TaskParameter="Lines" Item="BuildNumber"/>
</ReadLinesFromFile>

that said, another way to do what your question implies is to store you build num info in an xml file, with a MSBuild schema

这就是说,你的问题所暗示的另一种方法是使用MSBuild模式将你的数据信息存储在xml文件中

something like

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
 <PropertyGroup>
   <BuildNumber>10</BuildNumber>
   <RevNumber>5</RevNumber>
 </PropertyGroup>
</Project>

and then import the version.properties file into your main msbuild file

然后将version.properties文件导入主msbuild文件

相关文章