I'm trying to automate the process of some projects output folder, at the moment I'm writing those lines for each project that my website uses:
<Target Name="BuildPlugin" BeforeTargets="Build">
<MSBuild Projects="..\Module.Products\Module.Products.csproj" Targets="Publish" Properties="Configuration=$(Configuration);PublishDir=$(OutputPath)/plugins/Module.Products" />
<MSBuild Projects="..\Module.Sales\Module.Sales.csproj" Targets="Publish" Properties="Configuration=$(Configuration);PublishDir=$(OutputPath)/plugins/Module.Sales" />
</Target>
Is there any way I can automate that to search for Module.* and use in all those places where the name repeats? This is a standalone project.
Yes! Just make Items and then iterate over them while leveraging their metadata (https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-well-known-item-metadata). Something like this (apologies for any mistakes – typing this on a phone).
<Target Name="BuildPlugin" BeforeTargets="Build">
<ItemGroup>
<ProjectFiles Include="..\*\*.csproj" />
</ItemGroup>
<MSBuild Projects="%(ProjectFiles.Identity)" Targets="Publish" Properties="Configuration=$(Configuration);PublishDir=$(OutputPath)/plugins/%(ProjectFiles.Filename)" />
</Target>
Related
Is it possible to call MSbuild publish during build or in pre-buildevent or in post-buildevent? I'm trying to publish two of the web projects from a solution. I'm using file system publishing.Requirement here is , building solution should take care of publish those two web projects. Can any one please suggest ?
I wouldn't put too much deploy logic in a post-build event. It becomes "fragile".
Create a separate .msbuild file, and do the "extra" logic in it, instead of messing too much with the .csproj file.
Below is a basic example.
Place the xml below in an file call "MyBuildAndDeploy.msbuild", put it in the same folder as your .sln (or .csproj) file, and then use
msbuild.exe "MyBuildAndDeploy.msbuild" from the command line.
So below is a basic example of building the primary solution and then copying the files somewhere.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapper">
<PropertyGroup>
<!-- Always declare some kind of "base directory" and then work off of that in the majority of cases -->
<WorkingCheckout>.</WorkingCheckout>
<BuildResultsRootFolder>$(WorkingCheckout)\..\BuildResults</BuildResultsRootFolder>
</PropertyGroup>
<Target Name="AllTargetsWrapper">
<CallTarget Targets="BuildSolution" />
<CallTarget Targets="CopyBuildOutputFiles" />
</Target>
<Target Name="BuildSolution">
<MSBuild Projects="$(WorkingCheckout)\MySuperCoolSolution.sln" Targets="Build" Properties="Configuration=$(Configuration)">
<Output TaskParameter="TargetOutputs" ItemName="TargetOutputsItemName"/>
</MSBuild>
<Message Text="BuildSolution completed" />
</Target>
<Target Name="CopyBuildOutputFiles">
<MakeDir Directories="$(BuildResultsRootFolder)\$(Configuration)" Condition="!Exists('$(BuildResultsRootFolder)\$(Configuration)\')"/>
<ItemGroup>
<BuildOutputFilesExcludeFiles Include="$(WorkingCheckout)\**\*.doesnotexist" />
<BuildOutputFilesIncludeFiles Include="$(WorkingCheckout)\**\*.dll" Exclude="#(BuildOutputFilesExcludeFiles)" />
<BuildOutputFilesIncludeFiles Include="$(WorkingCheckout)\**\*.exe" Exclude="#(BuildOutputFilesExcludeFiles)" />
<BuildOutputFilesIncludeFiles Include="$(WorkingCheckout)\**\*.config" Exclude="#(BuildOutputFilesExcludeFiles)" />
<BuildOutputFilesIncludeFiles Include="$(WorkingCheckout)\**\*.pdb" Exclude="#(BuildOutputFilesExcludeFiles)" />
</ItemGroup>
<Copy SourceFiles="#(BuildOutputFilesIncludeFiles)"
DestinationFolder="$(BuildResultsRootFolder)\$(Configuration)\"/>
</Target>
</Project>
I have some msbuild code that looks something like this:
<Target Name="Build">
<MSBuild
Projects="#(UnitTestProject)"
Properties="$(BuildProperties)">
<Output TaskParameter="TargetOutputs" ItemName="TestAssembly" />
</MSBuild>
</Target>
<Target Name="Test" DependsOnTargets="Build">
<ItemGroup>
<TestAssembly Remove="*.Example.dll" />
</ItemGroup>
<xunit Assemblies="#(TestAssembly)" />
</Target>
So I am building all of my unit test projects and caputuring the built dll's using the Output task on the TargetOutputs parameter. The problem is that one of the projects is calling a task that outputs some dll's that I don't want to actually run xunit against.
What's weird though is that the Remove="*.Example.dll" appears to not have any affect at all and xunit is trying to test the assembly anyway.
Why is Remove not working?
Actually I think I figured it out. It appears that the problem resides in the way the relative path is resolved in ItemGroups in the Target vs. outside of a Target. I need to be a little more explicit with my path and then it works. Basically I did this to get it to work:
<Target Name="Build">
<MSBuild
Projects="#(UnitTestProject)"
Properties="$(BuildProperties)">
<Output TaskParameter="TargetOutputs" ItemName="UnitTestOutput" />
</MSBuild>
<ItemGroup>
<TestAssembly Include="#(UnitTestOutput)" Exclude="$(RootTestPath)\**\*.Example.dll" />
</Target>
<Target Name="Test" DependsOnTargets="Build">
<xunit Assemblies="#(TestAssembly)" />
</Target>
Im try to build my plugins that sit in a seperate directory on the root.
<ItemGroup>
<PluginProjectFiles Include="$(MSBuildStartupDirectory)..\..\Plugins\**\*.csproj"/>
</ItemGroup>
<Target Name="BuildPlugins">
<MSBuild Projects="#(PluginProjectFiles)" Targets="Clean;Build" Properties="Configuration=Release" />
<Message Text="Dir: $(MSBuildStartupDirectory)" />
</Target>
Although im having problems. My build runs the 'BuildPlugin' Target but it doesn't build my project files. I really don't want to have to build each project separately if I can avoid it.
Any ideas would be great. Thanks,
Please refer to my resolution below.
<PropertyGroup>
<SrcFolder>$(MSBuildProjectDirectory)\..\..</SrcFolder>
</PropertyGroup>
<ItemGroup>
<PluginProjectsFiles Include="$(SrcFolder)\Plugins\Plugin.*\*.csproj" />
</ItemGroup>
<Target Name="BuildPlugins">
<Message Text="Building Plugins" />
<MSBuild Projects="#(PluginProjectsFiles)" Targets="Clean;Build" Properties="Configuration=Release" />
<Message Text="Plugins Built" />
</Target>
I then changed my DependsOnTargets attribute on my primary build target to my 'BuildPlugins' target. Hope this helps someone as this cause me considerable pain.
I am creating a buildscript, where I'm outputting the TargetOutputs of an MSBuild, then wanting to call FXCop in a separate target, and using those outputs in the TargetAssemblies.
<Target Name="Build">
<MSBuild Projects="#(Projects)"
Properties="Platform=$(Platform);Configuration=$(Configuration);"
Targets="Build"
ContinueOnError="false">
<Output TaskParameter="TargetOutputs" ItemName="TargetDLLs"/>
</MSBuild>
<CallTarget Targets="FxCopReport" />
</Target>
<Target Name="FxCopyReport">
<Message Text="FXCop assemblies to test: #(TargetDLLs)" />
<FxCop
ToolPath="$(FXCopToolPath)"
RuleLibraries="#(FxCopRuleAssemblies)"
AnalysisReportFileName="FXCopReport.html"
TargetAssemblies="#(TargetDLLs)"
OutputXslFileName="$(FXCopToolPath)\Xml\FxCopReport.xsl"
ApplyOutXsl="True"
FailOnError="False" />
</Target>
When I run this, in the FxCopyReport target, the Message of TargetDLLs in empty, whereas if I put this in the Build target, it populates.
How can I pass/reference this value?
There is a blog post by Sayed Ibrahim Hashimi (co-author of Inside MSBuild book), describing the issue you ran into, dating back in 2005. Essentially CallTarget task is behaving weird. I'm not sure if it is a bug or designed behavior, but the behavior is still the same in MSBuild 4.0.
As a workaround, use normal MSBuild mechanism of setting order of execution of targets in MSBuild, using attributes DependsOnTargets, BeforeTargets or AfterTargets.
I was able to figure this one out.
Essentially, after the MSBuild step, I created an ItemGroup, which I then referenced in the calling Target.
<Target Name="Build">
<Message Text="Building Solution Projects: %(Projects.FullPath)" />
<MSBuild Projects="#(Projects)"
Properties="Platform=$(Platform);Configuration=$(Configuration);"
Targets="Build"
ContinueOnError="false">
<Output TaskParameter="TargetOutputs" ItemName="TargetDllOutputs"/>
</MSBuild>
<ItemGroup>
<TestAssemblies Include="#(TargetDllOutputs)" />
</ItemGroup>
</Target>
<Target Name="FXCopReport">
<Message Text="FXCop assemblies to test: #(TestAssemblies)" />
<FxCop
ToolPath="$(FXCopToolPath)"
RuleLibraries="#(FxCopRuleAssemblies)"
AnalysisReportFileName="$(BuildPath)\$(FxCopReportFile)"
TargetAssemblies="#(TestAssemblies)"
OutputXslFileName="$(FXCopToolPath)\Xml\FxCopReport.xsl"
Rules="$(FxCopExcludeRules)"
ApplyOutXsl="True"
FailOnError="True" />
<Message Text="##teamcity[importData id='FxCop' file='$(BuildPath)\$(FxCopReportFile)']" Condition="'$(TEAMCITY_BUILD_PROPERTIES_FILE)' != ''" />
</Target>
I am trying to create a msbuild script that will compile and place a test app into a folder on my desktop. I do not want this app published to IIS. I have followed several blgos and looked through hashimi's book but I still cannot figure this out. Below is my script. Thank you very much!
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Clean">
<ItemGroup>
<BinFiles Include="bin\*.*" />
</ItemGroup>
<Delete Files="#(BinFiles)" />
</Target>
<Target Name="Compile" DependsOnTargets="Clean">
<MSBuild Projects="test.vbproj"/>
</Target>
<Target Name="Publish" DependsOnTargets="Compile">
<RemoveDir Directories="$(OutputFolder)"
ContinueOnError="true"/>
<MSBuild Projects="test.vbproj"
targets="ResolveReferences;_CopyWebApplication"
Properties="WebProjectOutputdir=$(OutputFolder; OutDir=$WebProjectOutputDir)\"/>
</Target>
</Target>
</Project>
Your script is a bit awkward (you redefined the clean target to do the same as the the basic clean target).
I'm pretty sure your problem comes from the CopyWebApplication which does lots of stuff according to the properties set in your project file and pass by command line.
Can you try the following script :
<Project DefaultTargets="Compile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Compile">
<MSBuild
Projects="test.vbproj"
Targets="Clean;Build"
Properties="OutputPath=C:\tmp"/>
</Target>
</Project>
if your test project is a website then the build target should create it on the folder specified in the OutputPath/OutDir property