Using SmartAssembly with MSBuild
Published 02 January 2013
You can integrate SmartAssembly in your build process.
You set up your project in SmartAssembly once, and you can then build your assembly in your normal MSBuild process. The settings you chose in the SmartAssembly project are applied without needing to run SmartAssembly separately.
Note that the SmartAssembly task should be the last stage of your build process. Because of the way SmartAssembly changes your assembly, running other tasks after SmartAssembly is not supported. (An exception to this is assembly-signing tools, if you choose not to sign your code with SmartAssembly).
Automatic integration (.NET Framework, .NET Core, .NET Standard)
To integrate SmartAssembly directly into the build process using our NuGet package, follow these steps:
- Open your C# or VB.NET project in Visual Studio and add a reference to
- Build your assembly in Release mode.
- Open SmartAssembly and create a new project for your assembly (see Working with projects and Working with project settings).
- Save a SmartAssembly project next to your C# or VB.NET project under the same name. For example, if your C# project is named ProjectA.csproj, then save SmartAssembly project as ProjectA.saproj.
RedGate.SmartAssembly.MSBuild package is referenced, no changes are required to your .csproj/.vbproj project file. SmartAssembly will be executed automatically for all Release builds.
You can change this behaviour by setting optional custom properties in your C# or VB.NET project file (see below).
Using custom properties
You can add any of the following properties anywhere in your .csproj or .vbproj file:
Set to True if you want to process your assembly during build.
Set to False if you don't want to process your assembly during build.
Default: If this option is not set, or set to an empty string, SmartAssembly will process your assembly only for Release configuration.
Set to a relative or absolute path of SmartAssembly project file.
Default: If this option is not set, or set to an empty string, SmartAssembly uses a .saproj project file located next to your .csproj or .vbproj file.
Set to True if you want to overwrite the original assembly with the obfuscated one.
If you set this option to True, SmartAssembly ignores the SmartAssemblyOutput property, if set.
Default: If this option is not set, or set to False, SmartAssembly uses the destination file name from the .saproj project.
If this option is set, SmartAssembly will process the assembly specified as this property's value and will ignore input assembly path from .saproj project.
This is useful if you want to build an assembly using exactly the same settings as those you chose for a different assembly.
Input assembly path can be absolute or relative to .saproj file.
Default: If this option is not set, SmartAssembly will process the assembly specified in the .saproj project.
If this option is set, SmartAssembly will use this property's value as the file name when saving the built assembly.
This option is useful if you want to save a test build to a different path from other builds.
This attribute is ignored if the SmartAssemblyOverwriteAssembly property is set to True.
Output assembly path can be absolute or relative to .saproj file.
Default: If this option is not set, SmartAssembly will use the file name in the .saproj file when saving the built assembly.
An example with optional properties set in a .csproj file is shown below.
<Project> ... <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- Optional SmartAssembly properties --> <PropertyGroup> <SmartAssemblyIsEnabled>False</SmartAssemblyIsEnabled> <SmartAssemblyIsEnabled Condition=" '$(Configuration)' == 'Debug' ">True</SmartAssemblyIsEnabled> <SmartAssemblyProjectFile>$(MSBuildProjectDirectory)\$(AssemblyName)_$(Configuration).saproj</SmartAssemblyProjectFile> <SmartAssemblyInput>$(TargetPath)</SmartAssemblyInput> <SmartAssemblyOutput>$(OutputPath)\Protected\$(TargetFileName)</SmartAssemblyOutput> <SmartAssemblyOverwriteAssembly>True</SmartAssemblyOverwriteAssembly> </PropertyGroup> <!-- /Optional SmartAssembly properties --> </Project>
Explanation of the optional properties set on the screenshot above and their sample values:
- SmartAssembly will only process the assembly when building project in Debug configuration.
- SmartAssembly will use a project file located in $(MSBuildProjectDirectory)\$(AssemblyName)_$(Configuration).saproj, for example C:\MyProject\MyProject_Debug.saproj.
- SmartAssembly will process the assembly located in $(TargetPath), for example C:\MyProject\bin\Debug\MyProject.exe.
- SmartAssembly will overwrite the input assembly.
- SmartAssembly will ignore
OverwriteAssemblywas set to True.
If your project uses SDK-style project file or targets multiple frameworks, refer to Process a project targeting multiple frameworks.
Using the MSBuild task directly
By default, SmartAssembly MSBuild task is executed automatically for Release builds after CoreBuild MSBuild target.
If you want to execute SmartAssembly task before or after different MSBuild targets, or you want to execute SmartAssembly multiple times, you can use the SmartAssembly task directly.
First, you need to disable the default task by setting SmartAssemblyIsEnabled property to False:
<PropertyGroup> <SmartAssemblyIsEnabled>False</SmartAssemblyIsEnabled> </PropertyGroup>
Then, you can execute SmartAssembly task directly with your own MSBuild target:
<Target Name="MyCustomSmartAssemblyTarget" AfterTargets="CoreBuild"> <SmartAssembly ProjectFile="path\to\project.saproj" /> </Target> <!-- Attribute "ProjectFile" is required for "SmartAssembly" task. You can use optional attributes: OverwriteAssembly="true or false" Input="path\to\input\assembly.exe" Output="path\to\output\assembly.exe" -->