MSBuild - how to include/exclude directories? - visual-studio

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.

Related

Visual Studio 2017 builds all project because of "missing" PDB file

I had a few minutes this morning to try to figure out why Visual Studio 2017 rebuild all of my projects in the solution every time I try to run the project. I think I know why this is happening but I'm not sure how to fix it.
I saw these articles:
http://michaelscodingspot.com/2017/04/28/visual-studio-keeps-rebuilding-projects-no-good-reason/
https://ofekshilon.com/2015/08/16/visual-studio-projects-that-just-keep-rebuilding-or-how-quantum-mechanics-mess-up-your-build/
Which got me to turn up build logging.
I now see that VS is looking for PDB files in the OBJ directory and since they are not there(1), VS thinks the project needs to be rebuilt.
3>Project '****.Common' is not up to date. Missing output file 'C:\*******\***\****.Common\obj\Debug\***.****.Common.pdb'.2>Build started.
1) The PDB files are not in the OBJ directory for the assembly projects. They are in the BIN directory. Note that in the main "executable" web project the PDB files are in both the OBJ and BIN directories.
So now I need to make visual studio do one of the following:
Put a copy of the PDB in the OBJ directory for all projects
Make VS check in the BIN directory for the PDB
I did some search but haven't been able to figure out a good set of search terms to find the answer to this one.
Note this is a project that has been developed using VS2012, VS2015, VS2017 and maybe some before 2012, so this might be part of the problem.
This was an issue with assembly project that had been created in older versions of Visual Studio (the projects created in VS2017 don't have the problem).
The workaround I found was to change this part of the .csproj file:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
...
<OutputPath>bin\Debug\</OutputPath>
...
</PropertyGroup>
to:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
...
<OutputPath>obj\Debug\</OutputPath>
...
</PropertyGroup>
Note that you can make this change in "Properties / Build" under "Output" by editing "Output path". Also note that this is apparently a workaround because I didn't need to do this to the VS2017 projects. If I go and exhaustively try to figure out the differences between the proj files, I'll update this answer.
There were a few other content items set to "Copy Always" which also forces the whole project to rebuild. I changed those to "Copy if Newer".

Add a custom target to .csproj file

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.

Correct way to use the OutDir - load and archive file test: with Teamcity and Visual Studio 2012

Background
I have an import process, which includes uploading a couple of files, which then go into an "Import" folder, the user then processes the files (get put into the database), which then get put into an "Archive" folder.
Problem
Running tests for this process, is where I'm encountering the problem.
The test import files for this are stored in a "Resources" (including Import and Archive) folder within the Tests project within the Visual Studio solution.
Current Solution: Attempt
A post-build event has been setup on the project to xcopy the "Resources" folder to the ${Outdir} - this works great in Visual Studio.
Problem
When I run Teamcity, the solution build - creates the folder (and subfolders) in /bin/Release/ rather than Teamcity's /out/ directory.
I'm sure I'm just not doing the copy in the correct way, there have been some suggestions of using MSBuild rather than xcopy, so could do with some help.
How do I setup Teamcity / my Solution to build to output these test files to the same place.
Using TeamCity
You can override the build output path by passing in the parameter to msbuild - this will override the project settings.
In the MsBuild / Visual Studio runner step, add this into the Command Line Parameters field
/p:OutputPath=out
The other alternative is to edit the project file in an editor and change the path there.
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>out\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
This will ensure that all files are output to a directory that is consistent whether you are building in Visual Studio or TeamCity
To get your test files into this directory, I would set the build action on them to Content and to copy if newer.
Hope this helps.

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