Adding files to the bin directory at Build and Publish - asp.net-mvc

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:
Build Action = Content
Copy to Output Directory = Copy Always
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.

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

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

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.

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.

Related

Where does Ionide + Fake put the output executable?

I am trying to use .NET Core + Ionide + VS Code + Fake + Paket on macOS High Sierra.
Using the project generator, I have created a Suave application called Test. Ionide seems to have generated the appropriate files. After tweaking the TargetFramework to .NET Core, I can build successfully:
$ ./build.sh
...
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:03.72
Finished Target: Build
---------------------------------------------------------------------
Build Time Report
---------------------------------------------------------------------
Target Duration
------ --------
Clean 00:00:00.0026904
InstallDotNetCLI 00:00:01.2292511
Restore 00:00:04.2731055
Build 00:00:07.1234434
Total: 00:00:12.7035334
---------------------------------------------------------------------
Status: Ok
---------------------------------------------------------------------
There are now some files in Test/bin, but none of them are .exe, which is what I would expect as output from fsharpc.
Where does Ionide + Fake put the output executable?
My project has OutputType executable:
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Test.fs" />
</ItemGroup>
<Import Project="..\.paket\Paket.Restore.targets" />
</Project>
.Net Core compiles all projects (even executable applications) to .dll not .exe that can be run with dotnet PATH_TO_DLL. In the bin folder, in the subfolder for given framework target there should be file YOUR_PROJECT_NAME.dll that can be run with dotnet CLI.
To generate an exe you need to supply a run-time identifier. You can include this in the fsproj with
<PropertyGroup>
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
</PropertyGroup>
but you don't need to do that. You can use VSCode's tasks.json file to set up a build task.
My suggestion is just to run the dotnet CLI:
dotnet build -c Release -r win10-x64
This will create an exe in the bin\release\netcoreapp2.0\win10-x64 folder. You can also dotnet publish if you want a self-contained directory to deploy (this can get large). The id for OSX will be something like osx-x64.
By default ionide generates an fsproj file that is targetting net461, and you might also need Fake 5 for dotnetcore. I also suggest you use paket in magic-mode, and commit the .exe to github (if you use git).

Get full path of .sln file

How does one get the full physical path of the .sln file when scripting in MSBuild?
I'm trying to force nuget to download packages using:
<Target Name="BeforeCompileConfiguration">
<Exec Command=""$(ToolsHome)NuGet\NuGet.exe" restore "$(SolutionRoot)\KK\MyProject.sln"" />
</Target>
KK is the name of the folder that the .sln file is under. I'd like to replace
$(SolutionRoot)\KK\MyProject.sln
with a single $() build property.
$(SolutionPath)
should do your job!
By the way it's equivalent to:
$(SolutionDir)$(SolutionFileName)

TFS MSBuild copy command

The is my copy command:
<ItemGroup>
<SwfFiles Include="$(MSBuildProjectDirectory)\bin-release\**\*.*"/>
</ItemGroup>
<CallTarget Targets="CopyFilesToDropLocation"/>
<Target Name="CopyFilesToDropLocation">
<Copy
SourceFiles="#(SwfFiles)"
DestinationFiles="#(SwfFiles->'$(OutDir)_PublishedWebsites\PrismWeb\% (RecursiveDir)%(Filename)%(Extension)')"/>
</Target>
I dont anything being copied to the destinaton. Any idea what am I doing wrong?
IS this space for hiding values ?
DestinationFiles="#(SwfFiles->'$(OutDir)_PublishedWebsites\PrismWeb\% [here] (RecursiveDir)%(Filename)%(Extension)')"/>
I tested this with some of my code
<Copy SourceFiles="#(SourceAllFiles)" DestinationFiles="% (RecursiveDir)%(Filename)%(Extension)'" />
This space will produce a copy that works, but it tries to copy the source to this destination:
% [nbSpaces](RecursiveDir)[this part of the metadata woks]
You should use the latest build template in TFS 2013.4. It has defined locations for executing PowerShell, one of which is post-build. Using Powershell is both easyer to debug and more future proof.

MSBuild Fails to remove Binaries directory

I am building two solutions with MSBuild:
<ItemGroup>
<SolutionToBuild Include="$(BuildProjectFolderPath)/HostASPX/SolutionA.sln"/>
<SolutionToBuild Include="$(BuildProjectFolderPath)/../Installer/SolutionB.sln"/>
</ItemGroup>
It seems that this build fails with the error message:
Unable to remove directory "c:\TeamBuild\Team Solutions\Solution\Binaries". The directory is not empty.
It appears that MSBuild creates this 'Binaries' directory by default and the build passes. When I build again the build fails with the above message. If I try a 3rd build.. it works again.
Can someone tell me how to ensure that his folder is deleted/overwritten each time?
You sure there isn't an observer effect at play? i.e., something isn't locking the directory like explorer.exe :P
You can rule it out by using \\live.sysinternals.com\procmon.exe (to find out who is doing what to the dir) and procexp (to find out who is locking it).
I experienced the same problem of "Unable to remove directory ... the directory is not empty" while running a target that looks something like this:
<Target Name="CleanFiles"
DependsOnTargets="Prepare_Files"
Inputs="#(Files->'%(OutputPath)'->Distinct())"
Outputs="_Non_Existent_Item_To_Batch_">
<ItemGroup>
<DirsToDelete Include="#(Files->'%(OutputPath)'->Distinct())"/>
</ItemGroup>
<RemoveDir Directories="#(DirsToDelete)"/>
</Target>
Occasionally it worked fine and deleted the directories, but often I got the error above.
Eventually I found out that MSBuild itself is locking the files, beacuase they appear on the target's "Inputs", and not unlocking them in time for RemoveDir to delete them.
Changing the above to:
<Target Name="CleanFiles"
DependsOnTargets="Prepare_Files">
<ItemGroup>
<DirsToDelete Include="#(Files->'%(OutputPath)'->Distinct())"/>
</ItemGroup>
<RemoveDir Directories="#(DirsToDelete)"/>
</Target>
seems to solve the problem.
It's still OK to delete all directories always since RemoveDir skips non-existent directories and does not report an error.

How to tell MSBuild where to put my compiled files?

I'm trying to use Nant to compile an ASP.NET MVC app, so far my build script just runs ms build and runs some other tasks, however I want my compiled files to be put in a "build" directory, how can I tell msbuild where to put the compiled files?
Looking here:
http://msdn.microsoft.com/en-us/library/ms164311.aspx
it specifies that you can set msbuild to override the output dir setting in your project file, like so:
/properties:OutputDir=bin\Debug
Is this what you want?
you can put this in your project file (or in an imported project file if you want reuse), it will override both the path for the executable/dll and the path where the .obj files etc go.
<PropertyGroup>
<OutputPath>c:\bin</OutputPath>
<BaseIntermediateOutputPath>c:\temp\$(AssemblyName)</BaseIntermediateOutputPath>
</PropertyGroup>
If you are using the <msbuild> task from NAntContrib, then you can set the OutputDir property like this:
<msbuild project="path-to-sln-or-csproj-or-msbuild" target="Build">
<properties>
<property name="OutputDir" value="build-outdir-dir" />
</properties>
</msbuild>

Resources