Run web.config transformation from command-line - visual-studio-2010

Good day!
I want to have ability to build ASP.NET MVC 2 project using VS2010 Publish dialog and from command-line.
For command-line I get the following to work:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe .\SolutionFolder\MyProject.csproj /p:Configuration=Release;DeployOnBuild=True;PackageAsSingleFile=False;outdir=c:\_OutputFolder\
The only problem I have that Web.config transformation are not applied (but added to WebDeploy package). I don't use WebDeploy. Is there any way to apply Web.config transformations?
Thanks!

You can also try using the XDT Transformation Tool:
http://ctt.codeplex.com was moved to Github
https://github.com/greenfinch/ctt
I'm using this instead of messing with obscure msbuild targets.

Here is another approach, which uses msbuild to transform Web.config file:
http://sedodream.com/2010/04/26/ConfigTransformationsOutsideOfWebAppBuilds.aspx
In my tests the results were better. Basically, you create a project file to perform only a TransformXML task:
<Project ToolsVersion="4.0" DefaultTargets="Demo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="TransformXml"
AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>
<Target Name="Demo">
<TransformXml Source="app.config"
Transform="Transform.xml"
Destination="app.prod.config"/>
</Target>
</Project>
Save the project file and then apply the transformation, running the following command:
msbuild trans.proj /t:Demo
Where trans.proj is the name of the project file and Demo is the name of task target.

I think it's worth to mention that you can also use with PowerShell the DLL that Visual Studio is using: Microsoft.Web.XmlTransform.dll
PowerShell script, see: Web.Config transforms outside of Microsoft MSBuild?
To load the DLL instead of copying around, I do like this (so you see where to find this DLL, at least in my scenario at work we had to look-up these locations):
if (Test-Path "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.XmlTransform.dll") {
Add-Type -LiteralPath "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.XmlTransform.dll"
} elseif (Test-Path "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.XmlTransform.dll") {
Add-Type -LiteralPath "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.XmlTransform.dll"
} else {
throw [System.IO.FileNotFoundException] "Microsoft.Web.XmlTransform.dll not found."
}

Related

Using a ** wildcard in an F# project causes Visual Studio to refuse to load the project

I've got an F# project with several additional files stored in many nested folders, creating a complex folder structure. The compiler doesn't need to know about them: all I need to do is to have them copied into the output directory at the end of the build process.
I tried adding my files using the ** wildcard:
<ItemGroup>
    <FilesToCopyToOutput Include="additionalData\**\*.*"/>
</ItemGroup>
It works fine when running MSBuild manually from PowerShell. However, when I attempt to open my project in Visual Studio (v. 2015), I get the following error message:
Cannot open F# project . This project uses
wildcards in the item specification. Wildcards in F# projects are not
currently supported.
Similarly, a C# project with the wildcards works just fine. I guess this has something to do with the fact that the order of files in an F# project matters and using the wildcards causes a problem if a user wants to reorder the files.
However, I'm wondering if there's anything that can be done in my particular case: I don't care if the files are available in VS or not: I just want them to be copied to the output folder.
Using a ** wildcard in an F# project causes Visual Studio to refuse to load the project
Indeed, Visual Studio 2017 is a good choose. I have test the ** wildcard in the Visual Studio 2015 and 2017, it works fine in the Visual Studio 2017 but not in the Visual Studio 2015, got the same error as you.
Since you could not change the tech stack to use Visual Studio 2017, I would like provide a workaround for this issue, you can check if it works for you.
Since you do not care if the files are available in VS or not, you just want them to be copied to the output folder, you can use a power shell script to copy the additionalData folder to the output folder, like:
Copy-Item -Path "ThePathForAdditionalData\additionalData" -Destination "ThePathForProject\bin\Debug" -recurse -Force
Then execute this power shell script with MSBuild task after build:
<Target Name="CopyMyFiles" AfterTargets="Build">
<Message Text="Copying files..."/>
<Exec Command="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -executionpolicy Unrestricted -command "& { .\YourCopyFiles.ps1 } "" ></Exec>
</Target>
Hope this helps.
I assume FilesToCopyToOutput is the name you gave to an item collection, and the question is how to stop the old F# compiler from thinking it should handle it?
I suspect what you want to do though is to treat an entire folder as content items and copy them to the output folder :
<Content Include="additionalData\**\*.*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Copy Task
If you want to copy items with an MSBuild task you can use Copy. You should be able to write :
<ItemGroup>
<MyAdditionalData Include="additionalData\**\*.*" />
</ItemGroup>
<Target Name="CopyFiles">
<Copy
SourceFiles="#(MyAdditionalData)"
DestinationFolder="$(OutputPath)"
/>
</Target>
The Include attribute allows selecting multiple files that can be used as arguments to a task. Exclude is used to exclude files. You can specify a Condition attribute too, eg to run the task only for Release or Debug configuration. $(OutputPath) is one of the MSBuild properties that can be used in paths.
How to: Exclude Files from the Build shows how you can combine all those attributes to process specific items in a directory, eg:
<JPGFile
Include="Images\**\*.jpg"
Exclude = "Images\**\Version2\*.jpg"/>
This will include all files in the Images directory except those in Version2
Using Post-build events
Most people though didn't use MSBuild until the latest simplified format.
The most common way to copy files after build is to use a post-build event from the project's property pages, eg :
xcopy $(ProjectDir)additionalData\*.* $(TargetDir) /s /e
$(ProjectDir) and $(TargetDir) are replacement macros that point to the projects' folder and output folder. The list of all macros is available in Pre-build Event/Post-build Event Command Line Dialog Box.

Adding project to solution through command line

I have a Visual studio solution that is made up of multiple test projects. Each project is a copy of a template project. When the template project is copied there is a batch script that is ran that does some file renamings and adds a couple other specified files. What i would like to do is then have the project added to the solution during that process. I was looking at devenv switches here on MSDN but i am not seeing anything about adding a project to a solution.
Is adding a project to a solution possible through the command line?
Thanks
If you are here after 2019 and using Asp.Net Core. Here is the way to do it:
dotnet sln yoursolution.sln add yourprojectpath/yourproject.csproj
If you don't mind writing a bit of PowerShell, you can do this pretty easily. First, read this question and its answers. You'll find a pointer to a nuget package (Microsoft.SQLServer.Compact) which includes PowerShell scripts to automate VS through its object model. This is a good place to start to have some examples of how to handle a solution file from PowerShell. Then you write a small PowerShell script using the SolutionFolder.AddFromFile to add your project to your solution. This might be even easier using StudioShell and its provider, but I haven't tried it yet.
For sln file try this question. For project file you can run some xslt or any other necessary transform to edit. For example you can create MSBuild proj file to run xslt transform over desired sln file and then run it in cmd like: MSBuild.exe MyProj.proj. The sample content of MyProj.proj is like:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Transform">
<TransformXml Source="Project.csproj" Transform="Transform.xml" Destination="New_Project.csproj" />
</Target>
</Project>

Using MSBuild.exe to "Publish" a ASP.NET MVC 4 project with the cmd line

I'm looking for a command to run against the MSBuild.exe that just takes a MVC 4 project and publishes it to a given directory.
For example,
MSBuild <solution>/<project>.csproj -publish -output=c:/folder
This is obviously incorrect syntax. I'm trying to simplify my question.
This question talks of a build XML, but I'm not trying to do anything with that much detail.
I'm simply trying to do a deploy.
Further down in that question, someone speaks of "MSDeploy". I can look into that, but is it the only option? I do not have the ability to install web deploy on the server. In which case, all I really need to do is "Publish" and send the contents of the published project to a given directory on the server/file-system.
Does anyone have a one liner I can use?
Do I have to use MSDeploy?
Does MSDeploy require web deploy to be installed on the server?
Doesn't setting up web deploy on the server require setting up some ports, permissions, and installing some IIS add-ons?
I'd love to just execute something simple.
In VS 2012 (as well as the publish updates available in the Azure SDK for VS 2010) we have simplified command line publishing for web projects. We have done that by using Publish Profiles.
In VS for a web project you can create a publish profile using the publish dialog. When you create that profile it is automatically stored in your project under Properties\PublishProfiles. You can use the created profile to publish from the command line with a command line the following.
msbuild mysln.sln /p:DeployOnBuild=true /p:PublishProfile=<profile-name>
If you want to store the publish profile (.pubxml file) in some other location you can pass in the path to the PublishProfile.
Publish profiles are MSBuild files. If you need to customize the publish process you can do so directly inside of the .pubxml file.
If your end goal is to pass in properties from the command line. I would recommend the following. Create a sample publish profile in VS. Inspect that publish profile to determine what MSBuild properties you need to pass in on the command line. FYI not all publish method support command line publishing (i.e. FTP/FPSE).
FYI if you are building the .csproj/.vbproj instead of the .sln and you are using VS 2012 you should also pass in /p:VisualStudioVersion=11.0. For more details as to why see http://sedodream.com/2012/08/19/VisualStudioProjectCompatabilityAndVisualStudioVersion.aspx.
Create a build.xml file thats look like below
Start Visual Studio command prompt
Run msbuild build.xml
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" DefaultTargets="Build">
<PropertyGroup>
<Build>$(MSBuildProjectDirectory)\Build</Build>
<ProjectFile>MyProject.csproj</ProjectFile>
<ProjectName>MyProjectNameInVisualStudio</ProjectName>
<CopyTo>$(MSBuildProjectDirectory)\CopyTo</CopyTo>
</PropertyGroup>
<Target Name="Build">
<RemoveDir Directories="$(Build)"/>
<MSBuild Projects="$(ProjectFile)" Properties="Configuration=Release;OutputPath=$(Build);OutDir=$(Build)/"></MSBuild>
<Exec Command="robocopy.exe $(Build)\_PublishedWebsites\$(ProjectName) $(CopyTo) /e /is
if %errorlevel% leq 4 exit 0 else exit %errorlevel%"/>
</Target>
</Project>
The command below works perfect:
msbuild Myproject.sln /t:Rebuild /p:outdir="c:\outproject\\" /p:Configuration=Release /p:Platform="Any CPU"
I found Sayed's answer was deploying the default configuration i.e. Debug. The configuration selected in the Publishing Profile seems to get ignored by MSBuild. Accordingly I changed the command to specify the correct configuration for the deployment...
msbuild mysln.sln /p:Configuration=[config-name] /p:DeployOnBuild=true /p:PublishProfile=[profile-name]
where config-name = Release or some other build configuration you've created
With web projects you need to build, as per above, but then you also need to package/copy. We use a file copy, rather than the "publish"...
Also; we use DEBUG/RELEASE to build the website; but then actual environments, ie "QA" or "PROD" to handle the web.config transforms.
So we build it initially with RELEASE, and then package it with QA - in the example below.
<PropertyGroup>
<SolutionName>XXX.Website</SolutionName>
<ProjectName>XXX.Website</ProjectName>
<IisFolderName>XXX</IisFolderName>
<SolutionConfiguration>QA</SolutionConfiguration> <!--Configuration will be set based on user selection-->
<SolutionDir>$(MSBuildThisFileDirectory)..</SolutionDir>
<OutputLocation>$(SolutionDir)\bin\</OutputLocation>
<WebServer>mywebserver.com</WebServer>
</PropertyGroup>
<Target Name="BuildPackage">
<MSBuild Projects="$(SolutionDir)\$(SolutionName).sln" ContinueOnError="false" Targets="Clean;Rebuild" Properties="Configuration=Release" />
<MSBuild Projects="$(SolutionDir)\$(ProjectName)\$(ProjectName).csproj" ContinueOnError="false" Targets="Package" Properties="Configuration=$(SolutionConfiguration);AutoParameterizationWebConfigConnectionStrings=False" />
</Target>
<Target Name="CopyOutput">
<ItemGroup>
<PackagedFiles Include="$(SolutionDir)\$(ProjectName)\obj\$(SolutionConfiguration)\Package\PackageTmp\**\*.*"/>
</ItemGroup>
<Copy SourceFiles="#(PackagedFiles)" DestinationFiles="#(PackagedFiles->'\\$(WebServer)\$(IisFolderName)\$(SolutionConfiguration)\%(RecursiveDir)%(Filename)%(Extension)')"/>
</Target>
So;
Setup your properties
Call the BuildPackage target
Call the CopyOutput target
And voila!

How can I build a SharePoint 2010 package using command line?

I have a Visual Studio 2010 SharePoint project. If I choose 'Package' from the project menu, a .wsp file is generated. How can I invoke the same build from command line (i.e. what /target is required for MSBuild)?
I got it to work, finally. The tricky part is the fact that the SharePoint targets do not exist when MSBuild loads the .sln file, you have to load the individual .csproj files.
set msbuild="C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe"
set config=Debug
set outdir="C:\out\"
%msbuild% /p:Configuration=%config% /m ../My.SharePoint.Project/My.SharePoint.Projectcsproj /t:Package /p:BasePackagePath=%outdir%
This is also a useful document here: http://msdn.microsoft.com/en-us/ff622991.aspx
"To generate packages when building in
TFS 2010, set the parameter
/p:IsPackaging=True on MSBuild"
Also to package project with msbuild you can use target Package:
Define new target "BuildAndPackage"
<Target Name="BuildAndPackage">
<CallTarget Targets="Build"/>
<CallTarget Targets="Package"/>
</Target>
Use new target in build process:
<Project ToolsVersion="4.0" DefaultTargets="BuildAndPackage">
But this approach not recommended because it may cause errors in TFS Build process..
Set the MSBuild's verbosity to 'maximum' and you should see what is called from the build console.
In VS2010 of course :)

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

Resources