MSBuild: Deploying files that are not included in the project

走远了吗. 提交于 2019-12-04 06:22:04

Instead of using the pre-build event of the project properties (which I think is what you mean), override the BeforeBuild target in the .csproj/.vbproj file.

<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>

You may have to play with the shape of the Content item group that is the output of the CreateItem task if the files don't get deployed to the right directory. Note that I used an item transform to make the target path scripts\YourScript.js.

Also note the first CreateItem task stuffs the output into an item group called 'FileWrites.' I discovered that the Clean target inspects that item group to know what files to delete.

Place that XML into your project file after the <Import> elements and you should be good to go. No checking-into source control required and even your build server will be happy.

Can't you use a post-build event to run a script to do what you want? I use one to move my output files to a location completely unrelated to the project. Whether its plain batch, VBScript/JScript, or PowerShell you can do pretty much anything you want.

I had a similar requirement, though it only involved adding existing files to a deployment package rather than generating them as part of the build. I found the technique on this page useful.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!