简体   繁体   English

如何根据过时的输入文件执行 msbuild (csproj) 目标

[英]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.

相关问题 在.csproj中包含.targets文件以全局定义项目和MSBuild变量 - Including a .targets file in .csproj to globally define project & MSBuild variables 在没有.sln或.csproj文件的CMD中使用MsBuild - Using MsBuild in CMD without .sln or .csproj files 如何为具有多个目标的CSPROJ生成XML文档 - How to generate XML documentation for CSPROJ with multiple targets MSBuild:如何根据构建源项目的名称有条件地在.csproj文件中导入另一个项目? - MSBuild: How to conditionally import another project in a .csproj file based on the name of build-originated project? .NET Core + MSBuild:如何将csproj与多个project.json文件关联起来? - .NET Core + MSBuild: How to associate a csproj with multiple project.json files? 我想用 MSBuild 为 .csproj 创建 ChooseElement 如何? - I want to create ChooseElement for .csproj with MSBuild How? MSBuild和CSPROJ:如何调整C#文件的包含/编译 - MSBuild and csproj: how to conditioning c# file inclusion/compiling 如何在.csproj文件中使用MSBuild条件测试编译器指令? - How do I test for compiler directives with an MSBuild Condition in a .csproj file? 如何使用命令行中的C#7代码构建.csproj(msbuild) - How to build .csproj with C# 7 code from command line (msbuild) 如何使用MSBuild将链接文件添加到csproj文件。 (3.5框架) - How to add a linked file to a csproj file with MSBuild. (3.5 Framework)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM