Add a custom target to .csproj file - visual-studio

I want to add a custom target in my .csproj file.
The custom target will be :
msbuild /t:assembleDebug
This target will just build with the Debug mode.
I have already try to modify the .csproj file but no success.
Thanks for your answers.

This target will just build with the Debug mode. I have already try to modify the .csproj file but no success.
To accomplish this, unload your project. Then at the very end of the project, just before the end-tag </Project>, place below scripts:
<Target Name="assembleDebug" Condition=" '$(Configuration)' == 'Debug' ">
<Message Text="This custom target will only be executed when configuration is debug" Importance="high"></Message>
</Target>
With the condition '$(Configuration)' == 'Debug', the target will only be executed when configuration is Debug:
When you build it with the Release mode, this custom target will not be executed:
msbuild "CustomTarget.csproj" /p:Configuration=Release /t:build;assembleDebug
Update for comment:
Can I just call : msbuild /t:assembleDebug and this target will do the
same thing as msbuild /p:Configuration=Debug
/p:Configuration=Release is used to overwrite the default configuration on Visual Studio, if you just want call : msbuild /t:assembleDebug and this target will do the same thing as msbuild /p:Configuration=Debug, you should set the default configuration to Debug, then build it with the command msbuild /t:assembleDebug:
Then build it with that command line:
Hope this helps.

Related

how to add stop msbuild from building the project visual studio build option

I want to stop msbuild default "build" target to stop building my code instead, i want to define my custom target which will build my application. how to achieve this?
Thanks,
Just open the csproj file of your project.
And add these at the bottom of the file:
<Target Name="Build">
//write your custom build function
</Target>
It will overwrite the default build and use yours.

MSBuild - how to include/exclude directories?

I am trying to build a visual studio solution from the windows command line as follows:
msbuild solution.sln /t:Build /p:Configuration=Release
But I need to exclude a certain directory from one of the projects, and include another one. How do I do that?
Use this in your project csproj file:
<Compile Include="Folder1/*" Condition=" '$(Configuration)' == 'Release' " />
<Compile Exclude="Folder2/*" Condition=" '$(Configuration)' == 'Release' " />
Of course, you already set up the Configuration property, but you can replace it with your own property and set him from command line same as Configuration.
In the project dir, add two folders "CppFilesForDebug" and "CppFilesForRelease".
The "CppFilesForDebug" folder contains "Debug1.cpp,Debug2.cpp,Debug3.cpp" while "CppFilesForRelease" folder contains "Release1.cpp,Release2.cpp,Release3.cpp ".
Then add statement in the bottom of the vcxproj file like red rectangle below:
The red rectangle means we build and compile cpp files in "CppFileForDebug" folder when using Debug mode. And in release mode, we only compile cpp files in "CppFilesForRelease" folder insteat of cpp files in "CppFilesForDebug" folder.
I test it with C++ projects in VS2015 and VS2017 and it works. I think it can go well with your command "msbuild solution.sln /t:Build /p:Configuration=Release". Please it a try and any feedback would be great.
Update:
IF your issue results from QT conflicts, In vs, go QT menu ->Qt Project Settings->MocDirectory-> change it to
.\GeneratedFiles
Note: Don forget the "." before \GenerateFiles. Hope it helps. Any update you can share here.

Use different pre-build events for different build configurations in Visual Studio

Is it possible to use different pre-build events for different build configurations in Visual Studio?
For example, I'd like both a release configuration for a beta & live system and have the relevant app.[type].config get copied to app.config before it is compiled.
At the moment the configuration settings are baked into the .settings file, using the settings from the default app.config file.
Or just put the Condition on your target ... eg.,
Condition="'$(Configuration)' == 'Debug'"
.. or on your task.
If you're using Visual Studio VB/C# simple post build events, you can hand-edit the project file to put such conditions on the PreBuildEvent/PostBuildEvent property tags; and repeat the tags for Release.
Dan (msbuild dev)
You can do this in a couple of ways, depending on your exact situation:
Option 1: Check the $(ConfigurationName) variable in your pre-build script, like so:
IF EXISTS $(ProjectDir)app.$(ConfigurationName).config
COPY $(ProjectDir)app.$(ConfigurationName).config $(ProjectDir)app.config
Option 2: Add a "BeforeCompile" MSBuild target to your project file:
<Target Name="BeforeBuild">
<!-- MSBuild Script here -->
</Target>
Option 3: Use configuration file transformations; this VSIX plug-in adds the web.config transform features to non-web projects. These are XSLT files that let you rewrite your config files on build (unlike web projects, where it happens on publish.)
To use different build events for different configuration in visual studio, open the cs proj file of the project. in the pre build section
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Condition="'$(Configuration)'=='Release'" Command="echo Release" />
<Exec Condition="'$(Configuration)'=='Debug'" Command="echo Debug" />
</Target>
The command in "Command" parameter will only execute if this condition is met.

Automate VS 2010 "Publish" Config File Substitutions

I'm using the config file replacement feature of Visual Studio 2010's "Publish" functionality, as described in this article. I want to automate this using MSBuild/Hudson. Does anybody know how to do this?
I like how it works but if I cannot automate it I'll have to switch to XmlMassUpdate or similar.
Explanation
To transform your config file you'll have to execute the TransformWebConfig target.
This target takes two files Web.config and Web.$(Configuration).config and generates a Web.config. The generated file is the transformed version of the original one for the current configuration.
This file is generated in folder : obj\$(Configuration)\TransformWebConfig
Usage
You don't really explain what you want to achieve, so here a basic usage, a job that generates a transformed config file in a given folder.
Add the following piece in the end of your project file *.csproj after the import of Microsoft.WebApplication.targets
<PropertyGroup>
<!-- Directory where your web.config will be copied -->
<TransformedWebConfigDestination>$(MSBuildProjectDirectory)</TransformedWebConfigDestination>
</PropertyGroup>
<!--
This target transforms the web.config based on current configuration and
put the transformed files in $(TransformedWebConfigDestination) folder
-->
<Target Name="ConfigSubstitution">
<CallTarget Targets="TransformWebConfig"/>
<ItemGroup>
<TransformedWebConfig Include="obj\$(Configuration)\TransformWebConfig\Web.config"/>
</ItemGroup>
<!-- Copy the transformed web.config to the configured destination -->
<Copy SourceFiles="#(TransformedWebConfig)"
DestinationFolder="$(TransformedWebConfigDestination)"/>
</Target>
In Hudson you could add a Build step in your build, or create a dedicated job configured as follow:
MsBuild Build File : Your csproj file.
Command Line Arguments : /t:ConfigSubstitution /p:Platform=AnyCpu;Configuration=Test;TransformedWebConfigDestination=DestinationFolder
Edit your web project.csproj
under
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Add -
<UseMsDeployExe>True</UseMsDeployExe>
Look at the Build output (make sure VS Tools - Options - Project & Solutions -Build & Run - MSBuild Output Verbosity - Detailed)
You should be able to see the msdeploy commands VS uses to produce the package. It's my understanding that VS actually uses Web Platform Pipeline API's and .target files to actually produce the deploy packages when building using MSBuild, and this command changes to use MsDeploy instead.
This stuff is so in need of documentation, its very frustrating.
I am using this in Hudson to target Release:
/Property:Configuration=Release
The exact settings are:
Build
MSBuild Version: msbuild-4 (configured to point to v4 msbuild)
MsBuild Build File: project_name.sln
Command Line Arguments: /Property:Configuration=Release
You can test this in your project directory by running something similar (as your .NET framework version may differ) to this:
%SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30319\msbuild project.sln /Property:Configuration=Release

Running FxCop directly through VS project file

I am interested in setting up FxCop to run on each of our VS .csproj files in our developing environment by calling it as a target in the .csproj file itself without creating a separate MSBuild file for the project. Is this
<Target Name="AfterBuild">
<Exec Command=""C:\Program Files\Microsoft FxCop\FxCopCmd.exe" /searchgac /p:FxCopProject.fxcop /out:FxCopOutput.xml" ContinueOnError="true">
</Exec>
</Target>
possible to get working in one way or another being included at the tail end of my .csproj file?
I think you're looking for the post-build event.
Right Click the Project
Click Properties
Go to the "Build Events" Tab
Add the command line to the "Post-build event command line" box.
The following should work - paste it anywhere at just under the node level:
<PropertyGroup>
<PostBuildEvent>"%25ProgramFiles%25\Microsoft FxCop 1.36\FxCopCmd.exe" /p:$(SolutionDir)Bank.FxCop /o:$(TargetDir)FxCopResults.xml /d:"%25ProgramFiles%25\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies"
</PropertyGroup>
Note the /d parameter I'm using to point to where my project is using additional assemblies (in this case, the MSTest unit testing stuff).

Resources