簡體   English   中英

如何根據過時的輸入文件執行 msbuild (csproj) 目標

[英]How to execute msbuild (csproj) targets based on input files being out of date

我需要向我的 csproj 文件添加一個目標,該文件指定輸入文件和輸出文件。 通過鏈接,我嘗試像這樣定義目標:

<ItemGroup>
    <ins Include="A.txt" />
    <outs Include="B.txt" />
</ItemGroup>

<Target Name="ExecuteTarget" Inputs="@(ins)" Outputs="@(outs)">
    <Exec Command="echo hello world" />
</Target>

這里的目的是在每次在 VS IDE 中構建 csproj 時在 Visual Studio 輸出窗口中回顯 hello world 當且僅當文件 A.txt 比 B.txt 新或 B.txt 不存在時。 A.txt 和 B.txt 都與 csproj 文件位於同一目錄中。

但是,這似乎不起作用,因為當我構建此 csproj 時,我無法在輸出窗口中打印字符串“hello world”。 我正在使用 VS 16.11.5 以及 .NET 5.0 啟用的 csproj (WinUI)。

任何幫助/想法/建議將不勝感激。

必須在某處調用目標,只有輸入和輸出是不夠的。 有幾種方法可以做到這一點:使用CallTarget顯式調用它,或者使其成為另一個目標的依賴項(因此另一個被調用的目標具有Dependencies="ExecuteTarget" ),或者使用AfterTargets等。

例如<Target Name="ExecuteTarget" Inputs="@(ins)" Outputs="@(outs)" AfterTargets="CoreCompile">應該這樣做,如果 B.txt 存在並且比 A 新,則目標將被跳過。文本。 不過,我建議為目標使用更有意義的名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM