[英]How to execute msbuild (csproj) targets based on input files being out of date
I'm required to add a target to my csproj file which specifies input files as well as output files.我需要向我的 csproj 文件添加一个目标,该文件指定输入文件和输出文件。 Going by this link, I tried to define my target like so:
通过此链接,我尝试像这样定义目标:
<ItemGroup>
<ins Include="A.txt" />
<outs Include="B.txt" />
</ItemGroup>
<Target Name="ExecuteTarget" Inputs="@(ins)" Outputs="@(outs)">
<Exec Command="echo hello world" />
</Target>
The intention here is to echo hello world in the Visual Studio output window every time the csproj is built in VS IDE if and only if the file A.txt is newer than B.txt OR B.txt does not exist.这里的目的是在每次在 VS IDE 中构建 csproj 时在 Visual Studio 输出窗口中回显 hello world 当且仅当文件 A.txt 比 B.txt 新或 B.txt 不存在时。 Both A.txt and B.txt exist in the same directory as the csproj file.
A.txt 和 B.txt 都与 csproj 文件位于同一目录中。
However, this does not seem to work as I'm not able to get the string "hello world" to print in the output window when I build this csproj.但是,这似乎不起作用,因为当我构建此 csproj 时,我无法在输出窗口中打印字符串“hello world”。 I'm using VS 16.11.5 along with .NET 5.0 enabled csproj (WinUI).
我正在使用 VS 16.11.5 以及 .NET 5.0 启用的 csproj (WinUI)。
Any help/ideas/suggestions will be greatly appreciated.任何帮助/想法/建议将不胜感激。
A target has to be called somewhere, just having inputs and outputs isn't sufficient.必须在某处调用目标,只有输入和输出是不够的。 Couple of ways to do that: call it explicitly with
CallTarget
, or make it a dependency of another target (so another target which does get called has Dependencies="ExecuteTarget"
), or use AfterTargets
, etc.有几种方法可以做到这一点:使用
CallTarget
显式调用它,或者使其成为另一个目标的依赖项(因此另一个被调用的目标具有Dependencies="ExecuteTarget"
),或者使用AfterTargets
等。
Eg <Target Name="ExecuteTarget" Inputs="@(ins)" Outputs="@(outs)" AfterTargets="CoreCompile">
should do it, and the target will be skipped if B.txt exists and is newer than A.txt.例如
<Target Name="ExecuteTarget" Inputs="@(ins)" Outputs="@(outs)" AfterTargets="CoreCompile">
应该这样做,如果 B.txt 存在并且比 A 新,则目标将被跳过。文本。 I'd suggest using a more meaningful name for the target though.不过,我建议为目标使用更有意义的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.