Reading a single value from a file in MSBuild

♀尐吖头ヾ 提交于 2019-12-23 18:04:07

问题


I'm trying to read a version number from a file in MSBuild:

<ItemGroup>
    <VersionFile Include="Properties\VERSION" />
</ItemGroup>
<Target Name="BeforeBuild">
    <ReadLinesFromFile File="@(VersionFile)">
        <Output TaskParameter="Lines" ItemName="VersionNumber" />
    </ReadLinesFromFile>
</Target>

I only need the first line of this file. How can I concatenate that value with another string in WriteLinesToFile? This does not work:

<WriteLinesToFile
    File="$(AssemblyVersionFile)"
    Lines="[assembly: AssemblyVersion(&quot;@(VersionNumber)&quot;)]" />

I get an error:

error MSB4012: The expression "[assembly: AssemblyVersion("@(VersionNumber)")]" cannot be used in this context. Item lists cannot be concatenated with other strings where an item list is expected. Use a semicolon to separate multiple item lists.`


回答1:


I'm not too familiar with MSBuild but changing the Output of ReadLinesFromFile to be a Property and using $ to access it in the WriteLinesToFile seems to work:

<Target Name="BeforeBuild">
    <ReadLinesFromFile File="@(VersionFile)">
        <Output TaskParameter="Lines" PropertyName="VersionNumber" />
    </ReadLinesFromFile>
    <WriteLinesToFile
        File="output.txt"
        Lines="[assembly: AssemblyVersion(&quot;$(VersionNumber)&quot;)]" />
</Target>


来源:https://stackoverflow.com/questions/12785535/reading-a-single-value-from-a-file-in-msbuild

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