wilsjd
wilsjd

Reputation: 2258

Adding files to the bin directory at Build and Publish

I have two license files that I would like to include in my \bin directory both when I build and publish.

Both files are in the App_Data directory (their initial location doesn't matter, they just need to end up in the \bin) and have the following properties set:

They are in not the \bin when I build or publish.

What is wrong with my setup: the settings, the folders, the files, something else...?

UPDATE

I moved the files out of the App_Data directory and placed them in the project root and now they are copied to the \bin on build.

Upvotes: 10

Views: 16363

Answers (4)

Kreshnik
Kreshnik

Reputation: 2831

If you're using Visual Studio:

  • Show your file properties (Click on your file or Right-click on it then choose Properties)
  • At the Copy to Output Directory property choose Copy always or Copy if newer.

At build time, the file is going to be copied at the bin directory: Debug or Release...

Upvotes: 11

Daniel Lobo
Daniel Lobo

Reputation: 2211

In this post on Microsoft Connect the answer is much simpler: Referenced assemblies in Unit Test are not copied in TestResults/Out

So what I did was the following:

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

It works fine for me. Hope it help.

Upvotes: 0

Arve Systad
Arve Systad

Reputation: 5479

I've done this in a few projects by expanding my .csproject file slightly. The following code should be put directly beneath the Project node in your WebProject.csproj. The AfterBuild target simply copies a set of files ("unreferenced DLLs" in this case) to the bin-folder when building normally from Visual Studio. The CustomCollectFiles basically do the same thing when deploying.

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

The part you need to modify is basically the UnreferencedDlls node to match your folder structure. The **\*.dll part simply means "every DLL file at any level beneath here".

Upvotes: 11

Chase Florell
Chase Florell

Reputation: 47417

not necessarily a direct answer, but I highly suggest not using the baked in "publish" mechanism, but rather wire up a build script (probably in powershell) that will do everything you need. It's really easy to hook into MSBuild as well as nUnit and also copy files and move them around.

POWERSHELL (rough) example.

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

Upvotes: 0

Related Questions