在Build and Publish中将文件添加到bin目录

时间:2013-08-07 18:47:18

标签: asp.net-mvc asp.net-mvc-4

我有两个许可文件,我希望在构建发布时包含在我的\bin目录中。

这两个文件都在App_Data目录中(它们的初始位置无关紧要,它们只需要在\bin中结束)并设置以下属性:

  • Build Action = Content
  • Copy to Output Directory = Copy Always

我在构建或发布时不在\bin

我的设置有什么问题:设置,文件夹,文件等等??

更新

我将文件移出App_Data目录并将它们放在项目根目录中,然后将它们复制到构建中的\bin

4 个答案:

答案 0 :(得分:9)

我通过稍微扩展我的.csproject文件在一些项目中完成了这项工作。以下代码应直接放在WebProject.csproj中的Project节点下面。 当从Visual Studio正常构建时,AfterBuild目标只是将一组文件(在这种情况下为“未引用的DLL”)复制到bin文件夹。 CustomCollectFiles在部署时基本上做同样的事情。

  <PropertyGroup>
    <UnreferencedDlls>..\lib\Unreferenced\**\*.dll</UnreferencedDlls>
  </PropertyGroup>
  <PropertyGroup>
    <CopyAllFilesToSingleFolderForPackageDependsOn>
      CustomCollectFiles;
      $(CopyAllFilesToSingleFolderForPackageDependsOn);
    </CopyAllFilesToSingleFolderForPackageDependsOn>
  </PropertyGroup>
  <Target Name="AfterBuild">
    <Message Text="Copying unreferenced DLLs to bin" Importance="High" />
    <CreateItem Include="$(UnreferencedDlls)">
      <Output TaskParameter="Include" ItemName="_UnReferencedDLLs" />
    </CreateItem>
    <Copy SourceFiles="@(_UnReferencedDLLs)" DestinationFolder="bin\%(RecursiveDir)" SkipUnchangedFiles="true" />
  </Target>
  <Target Name="CustomCollectFiles">
    <Message Text="Publishing unreferenced DLLs" Importance="High" />
    <ItemGroup>
      <_CustomFiles Include="$(UnreferencedDlls)" />
      <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
        <DestinationRelativePath>bin\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
      </FilesForPackagingFromProject>
    </ItemGroup>
  </Target>

您需要修改的部分基本上是UnreferencedDlls节点,以匹配您的文件夹结构。 **\*.dll部分仅表示“此处下方任何级别的每个DLL文件”。

答案 1 :(得分:1)

如果您使用的是 Visual Studio

  • 显示文件属性(点击文件上的右键单击然后选择属性
  • 复制到输出目录属性中,选择始终复制复制更新

在构建时,文件将被复制到bin目录:Debug或Release ...

答案 2 :(得分:0)

不一定是一个直接的答案,但我强烈建议不要在“发布”机制中使用烘焙,而是连接一个构建脚本(可能在powershell中),它将完成您所需的一切。它很容易挂钩到MSBuild以及nUnit,也可以复制文件并移动它们。

POWERSHELL(粗略)的例子。

# Get Directory Location
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path

# Build the application using MSBuild
cmd /c C:\Windows\Microsoft.NET\Framework\$v4_net_version\msbuild.exe "$directorypath\MyProject.sln" /p:Configuration=Release 

# Run the tests using nUnit
cmd /c $directorypath\build\nunit\nunit-console.exe $solutionPath\MyProject.Tests\bin\debug\MyProject.Tests.dll

# Copy the license to the appropriate directory
Copy-Item -LiteralPath "$directorypath\mylicensefile.txt" "$directorypath\bin\release" -Force 

# NOTE: You are going to have to adjust this to match your solution and projects.

答案 3 :(得分:0)

在Microsoft Connect的这篇文章中,答案更简单: Referenced assemblies in Unit Test are not copied in TestResults/Out

所以我做的是以下内容:

[TestClass]
[DeploymentItem("Some.dll")]
public class SomeTests
{
...
}

对我来说很好。 希望它有所帮助。