不要使用项目属性的预构建事件(我认为这就是您的意思),而是覆盖 .csproj/.vbproj 文件中的 BeforeBuild 目标。
<Project ...>
...
<Target Name="BeforeBuild">
<!-- Create the 'BuildScripts' directory. -->
<!-- The $(IntermediateOutputPath) reference the 'obj' folder, which I like to -->
<!-- use for these kinds of things. -->
<MakeDir Directories="$(IntermediateOutputPath)BuiltScripts">
<Output PropertyName="BuildScriptsPath" TaskParameter="DirectoriesCreated" />
</MakeDir>
<!-- Execute the javascript minifier. -->
<Exec Command="..." />
<!-- Create an item group for the minified scripts so we manipulate the target path. -->
<CreateItem Include="$(BuildScriptsPath)\*.js">
<Output ItemName="BuiltScripts" TaskParameter="Include" />
<Output ItemName="FileWrites" TaskParameter="Include" />
</CreateItem>
<!-- Add the minified scripts to the Content item group, -->
<!-- which the deployment MSBuild inspects for files to deploy. -->
<CreateItem Include="@(BuiltScripts)"
AdditionalMetadata="TargetPath=scripts\%(Filename)%(Extension)">
<Output ItemName="ContentWithTargetPath" TaskParameter="Include" />
</CreateItem>
</Target>
...
</Project>
如果文件未部署到正确的目录,您可能必须使用作为 CreateItem 任务输出的内容项组的形状。请注意,我使用了项目转换来制作目标路径scripts\YourScript.js
。
另请注意,第一个 CreateItem 任务将输出填充到名为“FileWrites”的项目组中。我发现 Clean 目标检查该项目组以了解要删除的文件。
将该 XML 放在元素之后的项目文件中<Import>
,您应该一切顺利。无需签入源代码控制,甚至您的构建服务器也会很高兴。