MSBuild extensionpack Copy all the contents of a Directory to another - msbuild

I am using MSBuild extensionpack. I'd like to copy the entire contents of the build directory to another directory on the file system. I do not want to rename the destination directory, just replace the contents. It could be my unfamiliarity with msbuild extensionpack but it seems like this should be easy and I have been unable to find readily available documentation on the web.
I am trying to set up a service that is automatically deployed in the Continuous Integration environment after a successful build.

As far as I remember, you'll need to clear and copy in separate steps. So do the delete/purge first, then copy over. I wasn't able (at the time I last did) to find a way to "overwrite". This actually worked better for us b/c one build may remove files that a previous one contained, so we wouldn't want them to "linger".
To delete, try (assuming DeploymentDesintationPath is a property with the path):
<MSBuild.ExtensionPack.FileSystem.Folder
TaskAction="RemoveContent"
path="$(DeploymentDestinationPath)" />
And then copy (notice you need to populate an itemgroup for both the source and the destination)
<ItemGroup>
<DeploymentSourceFiles
Include="$(BuildFolder)\**\*"
/>
<DeploymentDestinationFiles
Include="#(DeploymentSourceFiles->
'$(DeploymentDestinationPath)\%(RecursiveDir)%(Filename)%(Extension)')"
/>
</ItemGroup>
<Copy SourceFiles="#(DeploymentSourceFiles)"
DestinationFiles="#(DeploymentDestinationFiles)" />
I haven't done this in a few months, so pardon if any of these examples require a bit of tweaking.

Related

Disable predefined targets (like CoreBuild and CorePublish) if property is set

I have a project structure with several dirs.proj and cs/ccproj files.
When developers and certain builds run, I don't want to generate cloud service .cspkg files (takes too long time when we don't need them). What our build infrastructure team have set up is an extra property "BuildCloudPackages"=='true', which I can use to only build the cloud packages when needed.
I want to stop CorePublish and Build unless BuildCloudPackages is set to true for all ccproj's in a dirs.proj.
Is there a way to retrofit conditions to existing targets? Or Conditionally including a ProjectFile in a dirs.proj. Or conditionally redefine targets inside a Choose/When? Or stop build/publish by disabling some of their preconditions? Or remove Build and CorePublish from default targets on the condition?
I worked around it using conditional import, where one disabled the tasks I didn't want to run, and one adds what I need only when I ask for it.
<Import Project="..\..\..\targets\DisableBuild.targets"
Condition="'$(BuildCloudPackages)'!='true'" />
<Import Project="..\..\..\targets\CustomTasks.targets"
Condition="'$(BuildCloudPackages)'=='true'" />
Where the disable file is something like this:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="CoreBuild" />
<Target Name="CorePublish" />
</Project>

Synchronize folders using MSBUILD

How can I use MSBuildExtensionPack 's Sync task to replace all files that exist in folder A with copies in folder B? (but skip any files that only exist in B?)
Do you have to use extension pack? If that is not a requirement, you can just do it using xcopy:
<Exec Command="xcopy /yu $(SourceFolder) $(DestinationFolder)" />
Make sure $(DestinationFolder) already exists before executing this, otherwise xcopy will be displaying a prompt to create one, which is not useful for the automated build system.
The solution is simple
Use "Exclude" and "!Exists" when defining the list of files to be copied

MSBuild: Task to embedding a file before compilation

I have the following need:
I'll have to create an MSBuild task that will produce an xml file, which I then need to embed as a resource to one of the projects being built. How do I change my MSBuild proj to accomplish that? Is there a built-in task I can use for embedding the file, or do I need to create one? If the latter, any direction on that would be great.
Thanks in advance!
Update: based on the suggestions given, I've ended up adding an empty xml file to the project as a resource, creating a simple MSBuild custom task (http://bartdesmet.net/blogs/bart/archive/2008/02/15/the-custom-msbuild-task-cookbook.aspx) that writes content to that file as I need it, and running that task as a "BeforeBuild" target. Works like a charm. Note that I've had to "exclude the file from source control", so it won't get checked out every time I build the project, and I've also added some code to the task to make sure the file isn't read-only (http://www.del337ed.com/blog/index.php/2007/09/05/clearing-the-read-only-flag-on-a-file-in-c/).
If you don't need to create the whole Xml file from scratch and could add a stubb file to your project you could use the XmlPoke Task to update this file in the BeforeBuild Target (see Sergios answer).
You can use builtin in your .csproj/.vbproj file target BeforeBuild (not forget to uncomment it) and call required MSBuild task in BeforeTarget. In that project add that resource as embedded. That's all.

MSBuild Configuration Files

I'm a complete newbie to MSBuild and I want to use it over NANT.
What I'm wanting is to run a build in say debug mode and to use app.configA, then in Stage use app.configB and in Production use app.configC.
I presume this is all doable but can anyone point me in the direction on how to set this up?
If you can endure the excruciating pains of MSBuild's copy statement, then you can do something like this as a post-build event:
<Copy Condition="'$(Env)' != ''" SourceFiles="$(WhereverTheDeployedAppIs)\web.$(Env).config" DestinationFiles="$(WhereverTheDeployedAppIs)\web.config" />
Now let's go through that.
$(Env) is the environment. You'll have to pass that in via your build script.
SourceFiles is set to the config file's original name (Web.MyFavoriteEnvironment.config, for example).
DestinationFiles is set to the same thing, only shortened to Web.config, overwriting whatever Web.config was there before. This is what your app will use.
Massage this to your app config file naming convention.
Now...
Although (something like) this works for my team, I really hope, for your sake, that someone posts something better.

Create MSBuild task that recursively copies a folder to several projects in my solution

I'm new to MSBuild and I tried reading up on several sources on the net but I'm missing somet things..
Here's what I want:
A build task that on execution recursively copies a directory structure from a (hardcoded/configured) path on my machine to a set of projects in the solution
Then compiles (release/debug, I guess I can make two sets of them)
Preferably this process would be called when I press f5 in VS and is selectable from the release/debug dropdown (with a different name off course).
What I don't get yet:
Where do I place my .proj file? In my root (where my sln file is)?
How can I make VS pick it up (bind it to f5).
Look at the Copy Task examples on how to copy a file structure recursively.