Suppose I have two targets that are time-consuming and I want to execute them in parallel. Let's say one target runs unit tests and the other generates some documentation. I tried this approach:
root.targets:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Default">
<Target Name="Default">
<MSBuild Projects="$(MSBuildProjectFile)" Targets="RunTests;BuildDocumentation" BuildInParallel="True"/>
</Target>
<Target Name="RunTests">
<Message Text="Running tests"/>
</Target>
<Target Name="BuildDocumentation">
<Message Text="Building documentation"/>
</Target>
</Project>
And then invoking like this (on a dual-core machine):
msbuild root.targets /m
But I get this output:
1>Project "C:\Repository\depot\EDG\DEW\branches\dna.dev.br\DnA\client\src\root.targets" on node 1 (default targets).
1>Project "C:\Repository\depot\EDG\DEW\branches\dna.dev.br\DnA\client\src\root.targets" (1) is building "C:\Repository\depot\EDG\DEW\branches\dna.dev.br\DnA\client\src\root.targets" (1:2) on node 1 (RunTests;BuildDocumentation target(s)).
1>RunTests:
Running tests
BuildDocumentation:
Building documentation
From this and some googling I gleaned that parallelization occurs only at the project level. Thus, I tried this:
root.targets:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Default">
<Target Name="Default">
<MSBuild Projects="test.targets;documentation.targets" BuildInParallel="True"/>
</Target>
</Project>
test.targets:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Default" DependsOnTargets="RunTests"/>
<Target Name="RunTests">
<Message Text="Running tests"/>
</Target>
</Project>
documentation.targets:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Default" DependsOnTargets="BuildDocumentation"/>
<Target Name="BuildDocumentation">
<Message Text="Building documentation"/>
</Target>
</Project>
Running it in the same fashion, I get:
1>Project "C:\Repository\depot\EDG\DEW\branches\dna.dev.br\DnA\client\src\root.targets" (1) is building "C:\Repository\depot\EDG\DEW\branches\dna.dev.br\DnA\client\src\test.t
argets" (2) on node 1 (default targets).
2>RunTests:
Running tests
2>Done Building Project "C:\Repository\depot\EDG\DEW\branches\dna.dev.br\DnA\client\src\test.targets" (default targets).
1>Project "C:\Repository\depot\EDG\DEW\branches\dna.dev.br\DnA\client\src\root.targets" (1) is building "C:\Repository\depot\EDG\DEW\branches\dna.dev.br\DnA\client\src\docume
ntation.targets" (3) on node 2 (default targets).
3>BuildDocumentation:
Building documentation
Thus, the targets are building in parallel.
But separating out targets into separate files just for the purposes of parallelization seems clunky. Am I missing something here? Is there a way I can avoid creating the extra targets files and still achieve parallelism?
I did the same for my time consuming builds and separating targets in different files does not look clumsy to me.
Since you want to build them in parallel, they don't interact. They are part of the build but this aside, they have no reason to be in the the same file.
I also separate property and items in a properties.xml.Targets to maintain them more easily. Doing this I can reference them in my multiple targets files without copying code too.
Related
How to perform own program before/after FolderPublish event in VS2019?
Not interesting at all how to add custom build script, it working and can be adding manually or by VS2019 project editor.
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
...
<PropertyGroup>
<PreBuildEvent>echo $(PublishDir)</PreBuildEvent>
</PropertyGroup>
</Project>
My question exactly about processing project publishing event, I try to use
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
...
<Target Name="ActionsBeforePublish" BeforeTargets="BeforePublish">
<Exec Command="echo YES" />
</Target>
<Target Name="ActionsAfterPublish" AfterTargets="AfterPublish">
<Exec Command="echo $(PublishDir)" />
</Target>
</Project>
but its not working. I don't see "YES" in publishing output window.
I have the following dummy build script files:
Common.targets
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<Target Name="TargetA">
<Message Text="This is TargetA"/>
</Target>
<Target Name="TargetB">
<Message Text="This is TargetB"/>
</Target>
</Project>
EntryPoint.proj
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<Import Project="Common.targets"/>
<Target Name="EntryPointTarget" DependsOnTargets="TargetA">
<!--But why this message is not shown during build?-->
<Message Text="This is Entry Point Target"/>
</Target>
</Project>
Why is EntryPointTarget message not shown during build?
If you do not specify a target on the commandline and no DefaultTarget is specified then msbuild executes the first target it sees, TargetA in this case. If you switch TargetA and TargetB you'll see TargetB being executed first. If you remove the import and make EntryPointTarget not depend on any other targets EntryPointTarget will be executed. Those aren't proper fixes obviously, so either:
specify the target explicitly call msbuild EntryPoint.proj /t:EntryPointTarget
make it determinstic what gets executed by adding the DefaultTargets=EntryPointTarget attribute to the Project tag, then you can just call msbuild EntryPoint.proj and it will execute EntryPoint.
I have a .vcxproj file that compiles a C++ program. I would like to create a second MSBuild project file that tests the program by running it, but only if the program has been rebuilt since the last successful test. How can I access the "TargetPath" of the program from the second project file?
If I could access TargetPath as an "item" from the .vcxproj file, then the the tester project file will look like this:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Build" Inputs="#(TargetPath)" Outputs="#(TargetPath->'%(filename).test-passed)'">
<Exec Command="#(TargetPath)" />
<Touch Files="#(TargetPath->'%(filename).test-passed)'" />
</Target>
</Project>
I would like to execute the test using a separate project file from the compilation of the program, to make it easier to choose between build-and-test or build-and-debug within Visual Studio, without multiplying the build configurations.
It is possible to run a native program compiled by a separate .vcxproj using the MSBuild task. Use the <Output> element to create an Item with the "TargetOutputs" from the C++ application build. However, if you are building a "native" program, "TargetOutputs" is normally blank. In this case, use the "GetNativeTargetPath" target to get the output path. The following project .vcxproj file works with Visual Studio. It builds test_build.vcxproj. The test_build.exe file is run, if it has changed since the last successful run.
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{80DB0D71-72E0-4FB1-B53F-EFB858A1D5A8}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>nordic_test_run</RootNamespace>
</PropertyGroup>
<PropertyGroup>
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ItemGroup>
<ProjectReference Include="test_build.vcxproj" />
</ItemGroup>
<Target Name="BuildExecutable">
<MSBuild Projects="#(ProjectReference)" Targets="Build" BuildInParallel="True" />
<MSBuild Projects="#(ProjectReference)" Targets="GetNativeTargetPath" BuildInParallel="True">
<Output TaskParameter="TargetOutputs" ItemName="NativeTests" />
</MSBuild>
</Target>
<Target Name="Build" DependsOnTargets="BuildExecutable" Inputs="#(NativeTests)" Outputs="#(NativeTests->'%(filename).test-passed')">
<Exec Command="#(NativeTests)" />
<Touch Files="#(TestTargets->'%(filename).test-passed')" />
</Target>
</Project>
I'm trying to set up a CI environment at a new client site using Team City and MSbuild and the MS build community extensions. Compiling the code seems to work fine. However, when I run my unit tests I get the following error coming from the NUnit task:
log4net : error XmlConfigurator: Failed to find configuration section 'log4net' in the application's .config file.
I've identified the two test projects that are causing this issue. However, I've ran the tests directly from nunit-console, and the resharper nunit test runner and though I see the warning the tests don't fail. I don't want to do anything with the Log4net configuration file or the assembly.cs in any project. All I want to do is make the MSBuild script behave like Visual Studio which doesn't consider the log4net error as a failure.
Here's the build file
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" DefaultTargets="Compile">
<Import Project=".\MSBuild.Community.Tasks.Targets"/>
<PropertyGroup>
<Configuration Condition="'$(Configuration)' == ''"> Debug</Configuration>
</PropertyGroup>
<ItemGroup>
<BuildArtifacts Include=".\build_artifacts\"/>
<SolutionFile Include ="..\Core.Services.sln"/>
<NUnitPath Include="..\Packages\NUnit.2.5.10.11092\tools"/>
</ItemGroup>
<Target Name="Clean">
<RemoveDir Directories="#(BuildArtifacts)"/>
</Target>
<Target Name="Init" DependsOnTargets="Clean">
<MakeDir Directories="#(BuildArtifacts)"/>
</Target>
<Target Name="Compile" DependsOnTargets="Init">
<MSBuild
Projects="#(SolutionFile)"
Targets="Rebuild"
Properties="OutDir=%(BuildArtifacts.FullPath)">
</MSBuild>
</Target>
<Target Name="DevelopmentBuild" DependsOnTargets="Compile">
<Message Text="Running Unit Tests from %(BuildArtifacts.FullPath)...." ContinueOnError="true"></Message>
<CreateItem Include="%(BuildArtifacts.FullPath)*.Tests.dll">
<Output TaskParameter="Include" ItemName="TestAssembly" />
</CreateItem>
<NUnit Assemblies="#(TestAssembly)"
ToolPath="#(NUnitPath)\"
ContinueOnError="false"
OutputXmlFile="%(BuildArtifacts.FullPath)test-results.xml"
DisableShadowCopy="true"/>
</Target>
</Project>
Execute this with msbuild:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Main" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Colors Include="Blue">
<Shade>Dark</Shade>
</Colors>
</ItemGroup>
<Target Name="Main">
<Message Text="Color: %(Colors.Shade) %(Colors.Identity)"/>
</Target>
</Project>
And it outputs:
Color: Dark Blue
All well, and good, but delete the color and use this:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Main" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
</ItemGroup>
<Target Name="Main">
<Message Text="Color: %(Colors.Shade) %(Colors.Identity)"/>
</Target>
</Project>
And it outputs:
Color:
Why is one batch of the Message task being executed when there are no items in the group? I would have expected for zero items, the batch would execute zero times and I would not see "Color:" followed by nothing in the output.
Am I doing something wrong or is there a workaround for this?
Thanks.
Update:
I've found you can do:
<Message Condition="'#(Colors)'!=''" Text="Color: %(Colors.Shade) %(Colors.Identity)"/>
But, if feels unsatisfactory to have to explicitly write code for the case when there are no items every time batching is used.
My 2 cents :
In your Message Task, there is information from Batching and static information ("Colors :"). I think MsBuild prints the static information and then batch over the values of your Colors Item. The probleme is that you don't have any data in your collection (it is even undeclared), I suppose MsBuild interpretates this as an empty list, which, when you try to print it, print the empty string ''.
If you remove the static content ("Colors :" and the whitespace before identity), you won't have anything displayed.
A solution for printing with batching only if the items collection is not empty would be either :
Check if the collection is empty
<Message Condition="'#(Colors)'!=''" Text="Color: %(Colors.Shade) %(Colors.Identity)"/>
Use MSBuild transforms
<Message Text="#(Colors->'Color : %(Shade) %(Identity)')"/>
Just wanted to add an alternative solution for this as well. If you can change your batching to Target batching, instead of Task batching, you can add your Condition statement to the Target.
I added the Target batching here:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Main" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
</ItemGroup>
<Target Name="Main" Outputs="%(Colors.Identity)">
<Message Text="Color: %(Colors.Shade) %(Colors.Identity)"/>
</Target>
</Project>
...and that can be conditionally made to only execute when something exists in the Colors item group:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Main" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
</ItemGroup>
<Target Condition="'#(Colors)'!=''" Name="Main" Outputs="%(Colors.Identity)">
<Message Text="Color: %(Colors.Shade) %(Colors.Identity)"/>
</Target>
</Project>