Copying entire directory using MSBuild - asp.net-core

I have the following on an ASP.NET Core 3.1 application:
<Target Name="OnBuild" BeforeTargets="Build">
<Exec WorkingDirectory="approot" Command="npm run build --prod" />
<Copy SourceFiles="approot\dist" DestinationFolder="wwwroot" />
</Target>
When I build I get an error:
The source file "approot/dist" is actually a directory.
The "Copy" task does not support copying directories.
How can I copy the directory approot\dist to wwwroot using MSBuild?

Try this:
<Target Name="OnBuild" BeforeTargets="Build">
<ItemGroup>
<Folder Include="**\approot\**\*.*" />
</ItemGroup>
<Copy SourceFiles="#(Folder)" DestinationFolder="wwwroot\%(RecursiveDir)"></Copy>
</Target>

Related

Copy client files when publishing

On a Net Core 7 web application I need to build a few client scripts (JS and LESS).
The scripts are in "./approot" and when I run the command npm run build this is done:
Build the files in "./approot" and save the output to "./approot/dist"
Delete the folder "./webroot" with all its contents.
Create a new "./webroot" and copy all files from "./approot/dist" to "./webroot"
So I have the following on project's .csproj file:
<Target Name="Build Client" AfterTargets="Build">
<Exec Command="npm install" />
<Exec Command="npm run build" />
<ItemGroup>
<Content Include="webroot\**">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Target>
When I publish the application steps (1) and (2) are done.
However the "./webroot" folder is not copied to publish folder.
So when I upload the web application to the server the client files are not found.
What am I doing wrong?
UPDATE The only way I am able to make this work is using:
<Target Name="Copy Webroot" BeforeTargets="AssignTargetPaths" DependsOnTargets="PrepareForPublish">
<Exec Command="npm install" />
<Exec Command="npm run build" />
<ItemGroup>
<Content Include="webroot/**" CopyToPublishDirectory="Always" />
</ItemGroup>
</Target>
I also kept the following so that Client is build when debugging:
<Target Name="Build Client" AfterTargets="Build">
<Exec Command="npm install" />
<Exec Command="npm run build" />
<ItemGroup>
<Content Include="webroot\**">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Target>
The problem is when I publish the application I see the client being build twice.
How to solve this?

How to publish additional files using msbuild file and TeamCity?

I'm using a msbuild file, TeamCity and Web Deploy to deploy my siteand everything works just fine, for the files included in the Visual Studio csproj file. In addition to these files I want to publish a couple of more files such as license files etc depending on environment.
This is my build file DeployToTest.proj:
<Project DefaultTargets="Deploy" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<ItemGroup>
<LicenseSourceFiles Include="License.config"/>
<RobotSourceFile Include="robots.txt" />
</ItemGroup>
<Target Name="Build">
<Message Text="Starting build" />
<MSBuild Projects="..\..\WebApp.sln" Properties="Configuration=Test" ContinueOnError="false" />
<Message Text="##teamcity[buildNumber '$(FullVersion)']"/>
<Message Text="Build successful" />
</Target>
<Target Name="Deploy" DependsOnTargets="Build">
<Copy SourceFiles="#(LicenseSourceFiles)" DestinationFolder="..\..\wwroot"></Copy>
<Copy SourceFiles="#(RobotSourceFile)" DestinationFolder="..\..\wwwroot"></Copy>
<Message Text="Started deploying to test" />
<Exec Command="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe ..\..\wwwroot\WebApp.csproj /property:Configuration=Test /t:MsDeployPublish /p:MsDeployServiceUrl=99.99.99.99;DeployIisAppPath=MySite;username=user;password=pass;allowuntrustedcertificate=true" />
<Message Text="Finished deploying to test" />
</Target>
</Project>
As you can see I tried to copy the license.config and robots.txt without any luck.
This .proj file is selected as the 'Build file path' in TeamCity.
Any suggestions on how I can accomplish this?
To solve this problem it may be worth executing the build script with the verbosity set to the 'detailed' or 'diagnostic' level. That should tell you exactly why the copy step fails.
However one of the most likely problems could be the fact that the script is using relative file paths, which depend on the working directory being set to the correct value. For build scripts I prefer use absolute paths to prevent any file path problems.
To get the absolute path you can use the MSBuildProjectDirectory property. The value of this property points to the path of the directory containing the currently executing MsBuild script. With that you can change your MsBuild script like this:
<Project DefaultTargets="Deploy" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<BaseDir>$(MSBuildProjectDirectory)</BaseDir>
</PropertyGroup>
<ItemGroup>
<LicenseSourceFiles Include="$(BaseDir)\License.config"/>
<RobotSourceFile Include="$(BaseDir)\robots.txt" />
</ItemGroup>
<Target Name="Build">
<Message Text="Starting build" />
<MSBuild Projects="$(BaseDir)\..\..\WebApp.sln" Properties="Configuration=Test" ContinueOnError="false" />
<Message Text="##teamcity[buildNumber '$(FullVersion)']"/>
<Message Text="Build successful" />
</Target>
<Target Name="Deploy" DependsOnTargets="Build">
<Copy SourceFiles="#(LicenseSourceFiles)" DestinationFolder="$(BaseDir)\..\..\wwroot"></Copy>
<Copy SourceFiles="#(RobotSourceFile)" DestinationFolder="$(BaseDir)\..\..\wwwroot"></Copy>
<Message Text="Started deploying to test" />
<Exec Command="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe ..\..\wwwroot\WebApp.csproj /property:Configuration=Test /t:MsDeployPublish /p:MsDeployServiceUrl=99.99.99.99;DeployIisAppPath=MySite;username=user;password=pass;allowuntrustedcertificate=true" />
<Message Text="Finished deploying to test" />
</Target>
</Project>
Now this should fix the problem if there is indeed a problem with the relative file paths.
Solution was to change settings for the web project in Visual Studio. Under Package/Publish Web i set 'Items to deploy' to 'All files in this project folder'. I then added a filter to remove all .cs files and other unwanted files.

MSBuild and creating ZIP files

Here is my build script:
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
<PropertyGroup>
<!-- Path where the solution file is located (.sln) -->
<ProjectPath>W:\Demo</ProjectPath>
<!-- Location of compiled files -->
<DebugPath>W:\Demo\bin\Debug</DebugPath>
<ReleasePath>W:\Demo\bin\Release</ReleasePath>
<!-- Name of the solution to be compiled without the .sln extension --> <ProjectSolutionName>DemoTool</ProjectSolutionName>
<!-- Path where the nightly zip file will be copyd -->
<NightlyBuildPath>W:\Nightly_Builds\Demo</NightlyBuildPath>
<!-- Name of the nighly zip file (YYYYMMDD_NightlyZipName.zip, date added automatically) -->
<NightlyZipName>Demo</NightlyZipName>
</PropertyGroup>
<ItemGroup>
<!-- All files and folders from ./bin/Debug or ./bin/Release what will be added to the nightly zip -->
<DebugApplicationFiles Include="$(DebugPath)\**\*.*" Exclude="$(DebugPath)\*vshost.exe*" />
<ReleaseApplicationFiles Include="$(ReleasePath)\**\*.*" Exclude="$(ReleasePath)\*vshost.exe*" />
</ItemGroup>
<Target Name="DebugBuild">
<Message Text="Building $(ProjectSolutionName) Debug Build" />
<MSBuild Projects="$(ProjectPath)\$(ProjectSolutionName).sln" Targets="Clean" Properties="Configuration=Debug"/>
<MSBuild Projects="$(ProjectPath)\$(ProjectSolutionName).sln" Targets="Build" Properties="Configuration=Debug"/>
<Message Text="$(ProjectSolutionName) Debug Build Complete!" />
<CallTarget Targets="CreateNightlyZip" />
</Target>
<Target Name="CreateNightlyZip">
<PropertyGroup>
<StringDate>$([System.DateTime]::Now.ToString('yyyyMMdd'))</StringDate>
</PropertyGroup>
<MakeDir Directories="$(NightlyBuildPath)"/>
<Zip Files="#(DebugApplicationFiles)"
WorkingDirectory="$(DebugPath)"
ZipFileName="$(NightlyBuildPath)\$(StringDate)_$(NightlyZipName).zip"
ZipLevel="9" />
</Target>
</Project>
My script works perfectly, only there is one strange problem. When i build a project first time and there is no \bin\Debug folder and its created during the build, but the ZIP file still comes empty. Running the build script second time when the \bin\Debug folder is now in place with builded files then the file are added to the ZIP.
What could be the problem that running script first time the ZIP file is empty?
The problem is in the DebugApplicationFiles item collection. It is created before the build is invoked. Move the DebugApplicationFiles into CreateNightlyZip target. Update your script this way:
<Target Name="CreateNightlyZip">
<PropertyGroup>
<StringDate>$([System.DateTime]::Now.ToString('yyyyMMdd'))</StringDate>
</PropertyGroup>
<ItemGroup>
<DebugApplicationFiles Include="$(DebugPath)\**\*.*" Exclude="$(DebugPath)\*vshost.exe*" />
</ItemGroup>
<MakeDir Directories="$(NightlyBuildPath)"/>
<Zip Files="#(DebugApplicationFiles)"
WorkingDirectory="$(DebugPath)"
ZipFileName="$(NightlyBuildPath)\$(StringDate)_$(NightlyZipName).zip"
ZipLevel="9" />
</Target>
If powershell 5.0 or greater is available, you could use powershell command directly.
<Target Name="Zip" BeforeTargets="AfterBuild">
<ItemGroup>
<ZipFiles Include="$(OutDir)release\file1.exe" />
<ZipFiles Include="$(OutDir)release\file2.exe" />
</ItemGroup>
<Exec Command="PowerShell -command Compress-Archive #(ZipFiles, ',') $(OutDir)release\zippedfiles.zip" />
</Target>
Should you wish to zip a whole folder for 'xcopy deploy', since MSBuild 15.8 there is a simple way - the ZipDirectory build task.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="ZipOutputPath" AfterTargets="Build">
<ZipDirectory
SourceDirectory="$(OutputPath)"
DestinationFile="$(OutputPath)\..\$(AssemblyName).zip"
Overwrite=="true" />
</Target>
</Project>
[1] https://learn.microsoft.com/en-us/visualstudio/msbuild/zipdirectory-task?view=vs-2019

Msbuild compile website without placing the site in IIS

I am trying to create a msbuild script that will compile and place a test app into a folder on my desktop. I do not want this app published to IIS. I have followed several blgos and looked through hashimi's book but I still cannot figure this out. Below is my script. Thank you very much!
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Clean">
<ItemGroup>
<BinFiles Include="bin\*.*" />
</ItemGroup>
<Delete Files="#(BinFiles)" />
</Target>
<Target Name="Compile" DependsOnTargets="Clean">
<MSBuild Projects="test.vbproj"/>
</Target>
<Target Name="Publish" DependsOnTargets="Compile">
<RemoveDir Directories="$(OutputFolder)"
ContinueOnError="true"/>
<MSBuild Projects="test.vbproj"
targets="ResolveReferences;_CopyWebApplication"
Properties="WebProjectOutputdir=$(OutputFolder; OutDir=$WebProjectOutputDir)\"/>
</Target>
</Target>
</Project>
Your script is a bit awkward (you redefined the clean target to do the same as the the basic clean target).
I'm pretty sure your problem comes from the CopyWebApplication which does lots of stuff according to the properties set in your project file and pass by command line.
Can you try the following script :
<Project DefaultTargets="Compile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Compile">
<MSBuild
Projects="test.vbproj"
Targets="Clean;Build"
Properties="OutputPath=C:\tmp"/>
</Target>
</Project>
if your test project is a website then the build target should create it on the folder specified in the OutputPath/OutDir property

MSBuild: Add additional files to compile without altering the project file

After looking around I can't find a simple answer to this problem.
I am trying to create an MSBuild file to allow me to easily use SpecFlow and NUnit within Visual Studio 2010 express.
The file below is not complete this is just a proof of concept and it needs to be made more generic.
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildDependsOn>
BuildSolution;
SpecFlow;
BuildProject;
NUnit;
</BuildDependsOn>
</PropertyGroup>
<PropertyGroup>
<Solution>C:\Users\Craig\Documents\My Dropbox\Cells\Cells.sln</Solution>
<CSProject>C:\Users\Craig\Documents\My Dropbox\Cells\Configuration\Configuration.csproj</CSProject>
<DLL>C:\Users\Craig\Documents\My Dropbox\Cells\Configuration\bin\Debug\Configuration.dll</DLL>
<CSFile>C:\Users\Craig\Documents\My Dropbox\Cells\Configuration\SpecFlowFeature1.feature.cs</CSFile>
</PropertyGroup>
<Target Name="Build" DependsOnTargets="$(BuildDependsOn)">
<Message Text="Build Started" Importance="high" />
<Message Text="Build Ended" Importance="high" />
</Target>
<Target Name="BuildSolution">
<Message Text="BuildSolution Started" Importance="high" />
<MSBuild Projects="$(Solution)" Properties="Configuration=Debug" />
<Message Text="BuildSolution Ended" Importance="high" />
</Target>
<Target Name="SpecFlow">
<Message Text="SpecFlow Started" Importance="high" />
<Exec Command='SpecFlow generateall "$(CSProject)"' />
<Message Text="SpecFlow Ended" Importance="high" />
</Target>
<Target Name="BuildProject">
<Message Text="BuildProject Started" Importance="high" />
<MSBuild Projects="$(CSProject)" Properties="Configuration=Debug" />
<Message Text="BuildProject Ended" Importance="high" />
</Target>
<Target Name="NUnit">
<Message Text="NUnit Started" Importance="high" />
<Exec Command='NUnit /run "$(DLL)"' />
<Message Text="NUnit Ended" Importance="high" />
</Target>
</Project>
The SpecFlow Task looks in the .csproj file and creates a SpecFlowFeature1.feature.cs.
I need to include this file when building the .csproj so that NUnit can use it.
I know I could modify (either directly or on a copy) the .csproj file to include the generated file but I'd prefer to avoid this.
My question is: Is there a way to use the MSBuild Task to build the project file and tell it to include an additional file to include in the build?
Thank you.
I found no way of doing it without editing the project file.
So I made an MSBuild file to:
Copy the project files
Run the copies through SpecFlow
Add the new .cs files to the copied projects
Compile the projects
Debug Run each of the compiled DLLs through NUnit
Clean up - Delete the copied projects
I've blogged about how to use it here:
http://learntdd.wordpress.com/2010/06/10/using-specflow-and-nunit-on-visual-studio-2010-express/
(It's version 1, I'd like to improve the script)
I couldn't think of any way to achieve without any modification to the .csproj file.
The approach I'd suggest would look like this.
In your .csproj you Import a container target file
...
<Import Project="SpecFlow.target" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
...
just above the CSharp.targets.
Specflow.targets would look like this
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="#(Compile)" />
</ItemGroup>
</Project>
so it doesn't harm while building the project from VS.
You could then use the Output of your SpecFlow Exec and add it to the SpecFlow.targets file
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="#(Compile)" />
<Compile Include="SpecFlowFeature1.feature.cs" />
</ItemGroup>
</Project>
...
and clean SpecFlow.targets after building your .csproj.