Determining If Rebuild was requested in Visual studio 2012 AfterClean task - visual-studio-2012

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>

Related

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>

Nant using devenv to build and publish Visual Studio 2012 Solution?

I am attempting to merge a Visual Studio 2010 project to 2012. The 2010 solution had a deployment project which output can be replicated by publishing in 2012. Our build script looking something like this:
<target name="buildSolution">
<delete file="${BuildRootFolder}\build.log" />
<exec program="devenv.exe" verbose="true" timeout="2400000"
commandline='/out build.log ${BuildRootFolder}\Solution.sln /Build "Release" /deploy Solution'
basedir="C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE">
<environment>
<variable name="PATH"
value="blah blah....">
</environment>
</exec>
</target>
How do I update this to Publish one of the projects in that solution using the publishing profile I defined?
Forget devenv and use msbuild.
<property name="MSBuildPath" value="C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe"/>
<target name="buildSolution" depends="clean">
<exec program="${MSBuildPath}">
<arg line='"${BuildRootFolder}\Solution.sln"' />
<arg line='/property:Configuration="Release";DeployOnBuild=true;PublishProfile=Deployment' />
<arg value="/target:Build" />
<arg value="/verbosity:normal" />
<arg value="/nologo" />
</exec>
</target>

Sequencing project/solution build and cmd file executing in custom MSBuild file

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.

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

NAnt Web Application Deployment

Good day.
I'm trying to deploy a web application using NAnt. It is current zipped using the NAnt ZIP task.
I can try calling MSDeploy from NAnt but I don't think MSDeploy was written for such deployments.
I can also try using NAnt task.
Does anybody have suggestions as to what approach can save me the most time?
Using the aspnet compiler is the simplest way and gets you access to all cl arguments which is not available on nant tasks. Not sure why it's so.
Here's what I do
<property name="aspnetcomplier" value="C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler.exe" />
<target name="deploy">
<mkdir dir="${output.dir}" />
<exec program="${aspnetcomplier}">
<arg value="-v" />
<arg value="/trunk" />
<arg value="-p" />
<arg value="${source.dir}\Root" />
<arg value="-f" />
<arg value="${output.dir}" />
</exec>
</target
Nothing complicated.Works like a charm.
P.S. Dont forget to do a iisreset /stop and /start
<target name="stop.iis" >
<servicecontroller action="Stop" service="w3svc" timeout="10000" verbose="true" />
</target>
<target name="start.iis" >
<servicecontroller action="Start" service="w3svc" timeout="10000" verbose="true" />
</target>

Resources