Sequencing project/solution build and cmd file executing in custom MSBuild file - visual-studio-2012

I need to tie together a bunch of steps which include building solutions, projects and running .cmd files using a custom MSBuild file.
My first pass at this is below:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>AnyCPU</Platform>
</PropertyGroup>
<ItemGroup>
<ProjectsToBuild Include="..\Hosts\solution1.sln"></ProjectsToBuild>
<ProjectsToBuild Include="..\..\solution2.sln"></ProjectsToBuild>
<ProjectsToBuild Include="helper1.csproj"></ProjectsToBuild>
<ProjectsToBuild Include="..\..\Sandboxes\helper2.sln"></ProjectsToBuild>
<Exec Include="" Command="CALL GetFiles.cmd"/>
<ProjectsToBuild Include="wix\proc\prod.wixproj"></ProjectsToBuild>
<Exec Command="CALL final.cmd"/>
</ItemGroup>
<Target Name="Build">
<MSBuild Projects="#(ProjectsToBuild)" Targets="Build">
<Output ItemName="ProjectOutputs" TaskParameter="TargetOutputs"/>
</MSBuild>
<Message Text="#ProjectOutputs"/>
</Target>
</Project>
This resulted in an error since the Exec element is in the wrong place.
Basically, I need to build solution1.sln, solution2.sln,helper1.csproj and helper2.sln (in sequence), then, run the file GetFiles.cmd, then build prod.wixproj followed by running the final.cmd file.
I have looked at MSDN (here, here, here), a blog, and browsed through various stackoverflow questions (including this, this, this, this), but none of them quite address what I am trying to do. This is the first time I have ever worked with MSBuild, so it is possible I may have missed something. Will appreciate any pointers...

Since an ItemGroup node can be a child of a Target node, break down those ItemGroup members into separate targets, then use the DefaultTargets attribute to control the sequence in which those are built.
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Target1;Target2;Target3" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5" >
<Target Name="Target1">
<Message Text="Target 1" />
</Target>
<Target Name="Target2">
<Message Text="Target 2" />
</Target>
<Target Name="Target3">
<Message Text="Target 3" />
</Target>
</Project>

The build projects are already in the correct order see:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>AnyCPU</Platform>
</PropertyGroup>
<ItemGroup>
<ProjectsToBuild Include="..\Hosts\solution1.sln"></ProjectsToBuild>
<ProjectsToBuild Include="..\..\solution2.sln"></ProjectsToBuild>
<ProjectsToBuild Include="helper1.csproj"></ProjectsToBuild>
<ProjectsToBuild Include="..\..\Sandboxes\helper2.sln"></ProjectsToBuild>
<ProjectsToBuild Include="wix\proc\prod.wixproj"></ProjectsToBuild>
</ItemGroup>
<Target Name="Build">
<Exec Command="CALL GetFiles.cmd"/>
<Message Text="Build order: %(ProjectsToBuild.Identity)"/>
<MSBuild Projects="#(ProjectsToBuild)" Targets="Build">
<Output ItemName="ProjectOutputs" TaskParameter="TargetOutputs"/>
</MSBuild>
<Message Text="#(ProjectOutputs)"/>
<<Exec Command="CALL final.cmd"/>
</Target>
</Project>
At the start the order of the itemgroup is displayed:
Project "C:\Test\Testcode\build\testcode.msbuild" on node 1 (default targets).
Build:
Build order: ..\Hosts\solution1.sln
Build order: ....\solution2.sln
Build order: helper1.csproj
Build order: ....\Sandboxes\helper2.sln
Build order: wix\proc\prod.wixproj
All done.

Related

How to package a Groovy class or Groovy project using Ant Build scripts?

Error: Could not find or load main class GroovyDemoClass
I am using a build.xml file similar to below
<project>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/GroovyDemoClass.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="GroovyDemoClass"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/GroovyDemoClass.jar" fork="true"/>
</target>
</project>
My doubt is that Can ANT identifies the Groovy class files like Java class files

How to resolve ArgumentNullException caused by adding <FrameworkReference> to .nuproj project file?

According to the NuProj documentation,
NuGet supports adding references to framework assemblies as well. You can specify those via the FrameworkReference item:
<ItemGroup>
<FrameworkReference Include="System.dll" />
<FrameworkReference Include="System.Core.dll" />
</ItemGroup>
But when I try this (see below), I am getting what looks like an ArgumentNullException — the generated .nuspec file does contain the correct <frameworkAssembly> elements, however:
1>C:\…\MSBuild\NuProj\NuProj.targets(527,5): error : Value cannot be null.
1>C:\…\MSBuild\NuProj\NuProj.targets(527,5): error : Parameter name: folderName
This is part of my .vbproj file:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
…
<PropertyGroup>
<NuProjPath Condition=" '$(NuProjPath)' == '' ">$(MSBuildExtensionsPath)\NuProj\</NuProjPath>
</PropertyGroup>
<Import Project="$(NuProjPath)\NuProj.props" Condition="Exists('$(NuProjPath)\NuProj.props')" />
<PropertyGroup Label="Configuration">
<Id>SomeProject</Id>
<Version>…</Version>
<Title>…</Title>
…
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SomeProject.vbproj" />
</ItemGroup>
<!-- the next ItemGroup is the one I added manually, as shown in the documentation: -->
<ItemGroup>
<FrameworkReference Include="System.ServiceModel.dll" />
</ItemGroup>
<Import Project="$(NuProjPath)\NuProj.targets" />
</Project>
Am I doing something wrong, or is this a bug with NuProj?
This is an issue with v3.4.3 of Nuget.exe - details here:
https://github.com/NuGet/Home/issues/2648
I was able to resolve this by updating to v3.5.0 - just run > nuget update -self on the command line.

Determining If Rebuild was requested in Visual studio 2012 AfterClean task

Anyone knows if i could determine if AfterClean task was executed in context of Rebuild.
I am only intrested to execute my task if Clean is executed explicitly from project context menu.
<Target Name="MyTask" AfterTargets="AfterClean">
<Message Text="Removing file: cache.data" Importance="high" />
<Exec Command="del /S cache.data" ></Exec>
</Target>
found answer myself, there may be a better way.
<Target Name="BuildExecutedFlag" BeforeTargets="BeforeRebuild" >
<CreateProperty Value="1">
<Output
TaskParameter="Value"
PropertyName="WasBuildExecuted" />
</CreateProperty>
</Target>
<Target Name="MyTask" AfterTargets="AfterClean" Condition="'$(WasBuildExecuted)' != '1'">
<Message Text="Removing file: cache.data" Importance="high" />
<Exec Command="del /S cache.data" ></Exec>
</Target>

MSBuild to build Release and QA at same time?

I have a working msbuild script in TeamCity which builds the Release configuration. I also need it to build the "QA" configuration and copy it to the QA folder. Can this be done in one script, or do I need multplie scripts?
<?xml version="1.0" encoding="utf-8" ?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
<SolutionName>AOP</SolutionName>
<SolutionFile>AOP.sln</SolutionFile>
<ProjectName>AOP.Web</ProjectName>
<ProjectFile>AOP.Web\AOP.Web.csproj</ProjectFile>
</PropertyGroup>
<Target Name="Build" DependsOnTargets="BuildPackage;CopyOutput" />
<Target Name="BuildPackage">
<MSBuild Projects="$(SolutionFile)" ContinueOnError="false" Targets="Rebuild" Properties="Configuration=$(Configuration)" />
<MSBuild Projects="$(ProjectFile)" ContinueOnError="false" Targets="Package" Properties="Configuration=$(Configuration)" />
</Target>
<Target Name="CopyOutput">
<ItemGroup>
<PackagedFiles Include="$(ProjectName)\obj\$(Configuration)\Package\PackageTmp\**\*.*"/>
</ItemGroup>
<Copy SourceFiles="#(PackagedFiles)" DestinationFiles="#(PackagedFiles->'c:\devDeploy\AOP\$(Configuration)\%(RecursiveDir)%(Filename)%(Extension)')"/>
</Target>
</Project>
If its just the value of configuration property which is different for QA, may be you can add another step in the teamcity build and call the same MSBUild script with the QA configuration value. Something like:
msbuild.exe YourScript.proj /p:Configuration=DEBUG
Alternatively you can try editing your script to something like:
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
<QAConfiguration Condition=" '$(Configuration)' == '' ">QAConfig</QAConfiguration>
<SolutionName>AOP</SolutionName>
<SolutionFile>AOP.sln</SolutionFile>
<ProjectName>AOP.Web</ProjectName>
<ProjectFile>AOP.Web\AOP.Web.csproj</ProjectFile>
</PropertyGroup>
<Target Name="Build" DependsOnTargets="BuildPackage;CopyOutput;BuildQAPackage;CopyQAOutput" />
<Target Name="BuildPackage">
<MSBuild Projects="$(SolutionFile)" ContinueOnError="false" Targets="Rebuild" Properties="Configuration=$(Configuration)" />
<MSBuild Projects="$(ProjectFile)" ContinueOnError="false" Targets="Package" Properties="Configuration=$(Configuration)" />
</Target>
<Target Name="CopyOutput">
<ItemGroup>
<PackagedFiles Include="$(ProjectName)\obj\$(Configuration)\Package\PackageTmp\**\*.*"/>
</ItemGroup>
<Copy SourceFiles="#(PackagedFiles)" DestinationFiles="#(PackagedFiles->'c:\devDeploy\AOP\$(Configuration)\%(RecursiveDir)%(Filename)%(Extension)')"/>
</Target>
<Target Name="BuildQAPackage">
<MSBuild Projects="$(SolutionFile)" ContinueOnError="false" Targets="Rebuild" Properties="Configuration=$(QAConfiguration)" />
<MSBuild Projects="$(ProjectFile)" ContinueOnError="false" Targets="Package" Properties="Configuration=$(QAConfiguration)" />
</Target>
<Target Name="CopyQAOutput">
<ItemGroup>
<PackagedFiles Include="$(ProjectName)\obj\$(QAConfiguration)\Package\PackageTmp\**\*.*"/>
</ItemGroup>
<Copy SourceFiles="#(PackagedFiles)" DestinationFiles="#(PackagedFiles->'c:\devDeploy\AOP\$(QAConfiguration)\%(RecursiveDir)%(Filename)%(Extension)')"/>
</Target>

VS2012 publish to multiple locations

I am currently using the inbuilt Publish function within VS2012 to publish an ASP.NET MVC site to a file system directory share on a Web Server. Is there anyway that I can have it publish to multiple locations rather than just the one when I click the Publish button?
I don’t want to have to create a second profile and have to do the same process twice and I have looked at modifying the pubxml file by adding in an additional tag to see if the publish routine picks it up. But unfortunately it just seems to pick up the last configuration in the list.
I know the ideal would be to implement a CI solution but for the time being my hands are tied with the Publish functionality and need to keep it relatively straight forward.
Many thanks
We had the same need of publishing our solution to multiple file share locations, and while the question was asked several months ago I thought that an answer could benefit to the community.
Since VS publish profiles are plain MSBuild files that can easily be extended, here is the solution I came with.
Note that I extracted some code fragments from our build process that is a bit more complex so I do not guarantee that it will all works without having to alter it a bit.
In the publish profile, I added a custom DeploymentPaths item as shown below.
Note that you could define one or more additional locations.
<ItemGroup Label="Defines additional publish locations">
<DeploymentPaths Include="\\SERVER1\ShareFolder\ProjectA\" />
<DeploymentPaths Include="\\SERVER2\ShareFolder\ProjectA\" />
</ItemGroup>
Then I added a custom target CustomWebFileSystemPublish to run after WebFileSystemPublish. This target calls another MSBuild file publish.xml that performs the delete of existing files and copy the new files.
<!-- Custom File System Publish to deploy to additional locations based on DeploymentPaths -->
<Target Name="CustomWebFileSystemPublish" AfterTargets="WebFileSystemPublish" Condition=" #(DeploymentPaths)!='' ">
<CreateItem Include="$(MSBuildProjectDirectory)\$(_PackageTempDir)">
<Output ItemName="AbsoluteSourcePathItem" TaskParameter="Include" />
</CreateItem>
<CreateProperty Value="%(AbsoluteSourcePathItem.Fullpath)">
<Output PropertyName="AbsoluteSourcePath" TaskParameter="Value" />
</CreateProperty>
<Message Text="### CustomWebFileSystemPublish" Importance="high" />
<Message Text="### DeploymentPaths: #(DeploymentPaths)" Importance="high" />
<MSBuild Projects="$(MSBuildProjectFile)" Properties="AbsoluteSourcePath=$(AbsoluteSourcePath)" Targets="DoPublish" />
</Target>
<Target Name="DoPublish">
<Message Text="### DoPublish $(AbsoluteOutputPath) | %(DeploymentPaths.Identity)" Importance="normal" />
<!-- Adjust path to the publish.xml file depending on where you put it in your solution -->
<MSBuild Projects="..\Deployment\publish.xml" Properties="OutputPath=$(AbsoluteSourcePath);DeployPath=%(DeploymentPaths.Identity)" />
</Target>
Finally, here is the publish.xml MSBuild file
<!-- Publish.xml -->
<Project ToolsVersion="4.0" DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Start">
<PropertyGroup>
<!-- Ensure DeployPath has the expected trailing slash -->
<DeployPath Condition=" '$(DeployPath)' != '' and !HasTrailingSlash('$(DeployPath)') ">$(DeployPath)\</DeployPath>
</PropertyGroup>
<Message Text=" # Deploying from $(OutputPath) to $(DeployPath) " Importance="normal" />
</Target>
<Target Name="CleanDeployFolder" DependsOnTargets="Start"
Condition=" $(DeployPath)!=''">
<Message Text=" # Cleaning files in $(DeployPath)" Importance="normal" />
<!-- Defines the files to clean -->
<ItemGroup>
<DeployCleanFiles Include="$(DeployPath)\**\*.*" />
</ItemGroup>
<!--Delete files in Deploy folder (folders not deleted by Delete Task)-->
<Delete Files="#(DeployCleanFiles)" />
<Message Text=" # Cleaning files in $(DeployPath) Completed" Importance="normal" />
</Target>
<Target Name="CopyToDeployFolder" DependsOnTargets="CleanDeployFolder"
Condition=" $(DeployPath)!=''">
<Message Text=" # Copying files to $(DeployPath)" Importance="normal" />
<ItemGroup>
<OutputFiles Include="$(OutputPath)\**\*.*" />
</ItemGroup>
<Copy SourceFiles="#(OutputFiles)" DestinationFolder="$(DeployPath)%(OutputFiles.RecursiveDir)" />
<Message Text=" # Copying files to $(DeployPath) Completed" Importance="normal" />
</Target>
<Target Name="Default" DependsOnTargets="CopyToDeployFolder"
Condition=" $(OutputPath)!='' And $(DeployPath)!='' ">
<Message Text=" # Deploying from $(OutputPath) to $(DeployPath) Completed" Importance="normal" />
</Target>
</Project>
You could create a small Windows Service that monitors a Directory and copies to multiple locations when new files are added
Try FileSystemWatcher on MSDN

Resources