gpt4 book ai didi

c# - 构建任务的外部文件

转载 作者:太空宇宙 更新时间:2023-11-03 11:41:29 24 4
gpt4 key购买 nike

我正在创建一个 .net 网络应用程序并有一些构建任务,例如 msbuild 复制任务。我通过编辑应用程序的项目文件并添加任务来添加它。

虽然这工作正常,但有什么方法可以将外部 xml 文件用于项目文件,并在其中使用主项目文件引用我的构建任务?我更喜欢这样,因为这样我就不必编辑主项目文件,并且项目文件和构建任务之间存在分离。

最佳答案

您可以通过向主项目文件添加import 来简单地引用任何外部项目或目标文件:

...
<!-- this is the default import for (c#) web project files -->
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets" />

<!-- import your custom project/target file here -->
<Import Project="MyCustom.targets" Condition="Exists('MyCustom.targets')" />
...

添加 Condition 将允许构建您的主项目,即使您的自定义项目/目标文件因为构建在不同的环境中运行而丢失也是如此。

我正在使用这种方法在我的本地计算机上运行 FxCop 和 StyleCop 目标,但可以构建相同的主项目文件而无需在我的暂存环境中进行任何更改。


更新

您的评论表明您实际上正在寻找一种应该反过来工作的解决方案:您想在构建项目之前和之后执行一些步骤而不修改项目配置本身。

在这种情况下,最好的方法是创建自定义项目来调用 Web 项目的构建。这可能看起来像这样:

<Project DefaultTargets="MyTargetAfterBuild">
<!-- some Project attributes omitted for readability -->

<Target Name="MyTargetBeforeBuild" ContinueOnError="false">
<Exec Command="svn export" />
</Target>

<Target Name="Build" DependsOnTargets="MyTargetBeforeBuild">
<MSBuild Projects="MyWebProject.csproj" Targets="Build" Properties="Configuration=Release" >
</MSBuild>
</Target>

<Target Name="MyTargetAfterBuild" DependsOnTargets="Build">
<Exec Command="powershell.exe .\MyCustomScript.ps1" />
</Target>

</Project>

您可能对 this answer on a similar scenario 感兴趣有一个更详细的例子。

关于c# - 构建任务的外部文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4650068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com