MSBuild:部署项目中未包含的文件 [英] MSBuild: Deploying files that are not included in the project

查看:168
本文介绍了MSBuild:部署项目中未包含的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Web项目上有一个预构建事件,可以使用节点来缩小并连接javascript文件。这将在脚本文件夹中创建一个名为BuiltScripts的文件夹,该文件夹与脚本文件夹重复,但文件被缩小。当我正在进行部署时,我想发布包含其中的BuiltScripts文件夹的scripts文件夹。为了实现这一点,我已经将BuiltScripts文件夹添加到项目中。这不是一个理想的解决方案:

I have a Pre-build event on a web project that minifies and concatenates javascript files using node. This creates a folder called BuiltScripts in the scripts folder that is a duplicate of the scripts folder except the files are minified. When I am doing a deploy I want to publish the scripts folder including the BuiltScripts folder within it. To achieve this I have added the BuiltScripts folder to the project. This is not an ideal solution as:


  1. 我必须将BuiltScripts文件夹签出,以便构建,因为其中的文件已被读取只有解决方案在源控制下。当我有这么多文件签出时,这会造成麻烦。

  2. 当我向项目添加一个新文件时,我必须确保我记得将它添加到BuiltScripts文件夹或该文件的内置版本将不被部署。

  3. 我的构建将在构建服务器上失败,因为BuiltScripts文件夹中的文件也只读在那里。

  4. 拥有相同名称的两个文件副本是搜索文件和进行基于文本搜索的问题。

  1. I have to have the BuiltScripts folder checked out in order to build as the files in it are read only as the solution is under source control. This creates hassles when checking in as I have so many files checked out.
  2. When I add a new file to the project I have to make sure I remember to add it to the BuiltScripts folder or the built version of the file will not be deployed.
  3. My build will fail on the build server as the files in the BuiltScripts folder are read only there as well.
  4. Having two copies of a file with the same name is an issue when searching for files and doing text based searches.

我想让构建服务器构建并将javascript文件缩小为预构建步骤,但是我不希望将BuiltScripts文件夹添加到项目中。然而,当构建服务器最终打包项目时,我希望它将BuildScripts文件夹与构建过程的输出进行复制。如何实现这个?

I would like to have the build server build and minifiy the javascript files as a pre build step but I do not want the BuiltScripts folder added to the project. However when the build server packages the project at the end I want it to copy the BuiltScripts folder with the output of the build process. How can I achieve this?

推荐答案

而不是使用项目属性的预构建事件(我认为是你意味着),覆盖.csproj / .vbproj文件中的BeforeBuild目标。

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>

您可能必须使用作为CreateItem任务输出的内容项目组的形状如果文件没有被部署到正确的目录。请注意,我使用一个项目变换来创建目标路径 scripts \YourScript.js

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.

另请注意第一个CreateItem任务将输出填充到名为FileWrites的项目组中。我发现Clean目标检查该项目组以了解要删除哪些文件。

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.

将该XML在 < Import> 元素之后的项目文件中,你应该很好。不需要检查源代码管理,甚至您的构建服务器也将很开心。

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.

这篇关于MSBuild:部署项目中未包含的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆