Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

How to: Clean a Build

When you clean a build, all intermediate and output files are deleted, leaving only the project and component files. From the project and component files, new instances of the intermediate and output files can then be built. The library of common tasks that is provided with MSBuild includes an Exec task that you can use to run system commands. For more information on the library of tasks, see MSBuild Task Reference.

By default, the .exe file that is created when you compile a project is placed in the same directory as the project and source files. Typically, however, output items are created in a separate directory.

To create a directory for output items

  1. Use the Property element to define the location and name of the directory. For example, create a directory named BuiltApp in the directory that contains the project and source files:

    <builtdir>BuiltApp</builtdir>

  2. Use the MakeDir task to create the directory if the directory does not exist. For example:

    <MakeDir Directories = "$(builtdir)"

    Condition = "!Exists('$(builtdir)')" />

Prior to creating new instances of intermediate and output files, you may want to clear all previous instances of intermediate and output files. Use the RemoveDir task to delete a directory and all files and directories that it contains from a disk.

To remove a directory and all files contained in the directory

  • Use the RemoveDir task to remove the directory. For example:

    <RemoveDir Directories="$(builtdir)" />

The following code example project contains a new target, Clean, that uses the RemoveDir task to delete a directory and all files and directories that it contains. Also in this example, the Compile target creates a separate directory for the output items that are deleted when the build is cleaned.

Compile is defined as the default target and is therefore used automatically unless you specify a different target or targets. You use the command line switch /target to specify a different target. For example:

msbuild <file name>.proj /target:Clean

The /target switch can be shortened to /t and can specify more than one target. For example, to use the target Clean then the target Compile, type:

msbuild <file name>.proj /t:Clean;Compile

<Project DefaultTargets = "Compile"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >

    <PropertyGroup>
        <!-- Set the application name as a property -->
        <name>HelloWorldCS</name>

        <!-- Set the output folder as a property -->
        <builtdir>BuiltApp</builtdir>
    </PropertyGroup>

    <ItemGroup>
        <!-- Specify the inputs by type and file name -->
        <CSFile Include = "consolehwcs1.cs"/>
    </ItemGroup>

    <Target Name = "Compile">
        <!-- Check whether an output folder exists and create
        one if necessary -->
        <MakeDir Directories = "$(builtdir)" 
            Condition = "!Exists('$(builtdir)')" />

        <!-- Run the Visual C# compiler -->
        <CSC Sources = "@(CSFile)" 
            OutputAssembly = "$(BuiltDir)\$(appname).exe">
            <Output TaskParameter = "OutputAssembly"
                ItemName = "EXEFile" />
        </CSC>

        <!-- Log the file name of the output file -->
        <Message Text="The output file is @(EXEFile)"/>
    </Target>

    <Target Name = "Clean">
        <RemoveDir Directories="$(builtdir)" />
    </Target>
</Project>
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.