msbuild Archives - Byron Salau

How to recursively copy files using MSBuild Copy Task

Posted by | c# | No Comments

Today I was faced with the problem of copying a set of files into a new location using a MSBuild task. Previously we got the job done using an xcopy command post build, while this was okay it’s not ideal for front end developers.

The problem is front end developers are constantly tweaking files usually followed by refreshing the website over and over. In my case today we where building from a kernal, so the task of building the dll’s and waiting for iis to recompile is all very time consuming.

This was my solution to copy css and javascript files recursively into a new location using the MSBuild Copy Task.


<ItemGroup>
	<CompiledCss Include=\"..\\css\\**\\*.css\"/>
	<CompiledJavascript Include=\"..\\javascript\\**\\*.js\"/>
</ItemGroup>

<Target Name=\"CopyFiles\">
	<Copy
		SourceFiles=\"@(CompiledCss)\"
		DestinationFolder=\"..\\..\\website\\css\\%(RecursiveDir)\"
	/>
	
	<Copy
		SourceFiles=\"@(CompiledJavascript)\"
		DestinationFolder=\"..\\..\\website\\javascript\\%(RecursiveDir)\"
	/>
</Target>

The double stars (**) is used to set the task as a recursive copy, it will also maintain any folder structure you have.

Using this method enables you to quickly build the CSS and Javascript files without changing any dll’s. This means the website refresh is instant and you will be much more productive.