I need to build at the solution level using MSBuild since I have multiple configurations. I have a custom target in my web project that publishes the output. I can specify the web project as the target and it builds fine. But I need to publish the web application. Is there a way to build a project's custom target when building at the solution level?
I found this link, but I don't understand the solution. If someone could break that down a little, it would help a lot.
This is what my current PowerShell command-line looks like:
& "$env:windir\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe" `
"..\..\Solution.sln" `
"/t:WebApplication:PublishToFileSystem" `
"/p:Configuration=$configuration;PublishDestination=$publishPath"
The :PublishToFileSystem causes it to fail, with this message:
error MSB4057: The target "PublishToFileSystem" does not exist in the project. [C:\Source\Solution.sln]
This is what my custom target in my .csproj file looks like:
<Target Name="PublishToFileSystem" DependsOnTargets="PipelinePreDeployCopyAllFilesToOneFolder">
<Error Condition="'$(PublishDestination)'==''" Text="The PublishDestination property must be set to the intended publishing destination." />
<MakeDir Condition="!Exists($(PublishDestination))" Directories="$(PublishDestination)" />
<ItemGroup>
<PublishFiles Include="$(_PackageTempDir)\**\*.*" />
</ItemGroup>
<Copy SourceFiles="#(PublishFiles)" DestinationFiles="#(PublishFiles->'$(PublishDestination)\%(RecursiveDir)%(Filename)%(Extension)')" SkipUnchangedFiles="True" />
</Target>
So, after some research, I found that creating a custom target is not the best way to publish a web application. Assuming you have just one web project in your solution, MS extended MSBuild to use publish profiles (the same ones used when you publish in Visual Studio). I was able to create a separate publish profile for each configuration (environment) and call the following command line:
& "$env:windir\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe" `
"..\..\Solution.sln" `
"/p:DeployOnBuild=true" `
"/p:PublishProfile=$configuration"
I just had to make sure my publish profiles had the same name as my $configuration.
Related
I'm trying to get the Output folder of referenced project in my Wix Visual Studio project, using MsBuild task. But it is failing with following error:
C:\Program Files
(x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(724,5):
error : The OutputPath property is not set for project
'ConsoleApplicatio n1.csproj'. Please check to make sure that you
have specified a valid combination of Configuration and Platform for
this project. Configuration='Release' Platfo rm='x86'. You may be
seeing this message because you are trying to build a project without
a solution file, and have specified a non-default Configuration or
Plat form that doesn't exist for this project.
[C:\Users\fwaheed\Documents\Visual Studio
2015\Projects\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cspro
j]
Following is the Target calling MsBuild task.
<Target Name="AfterBuild">
<MSBuild
Projects="#(ProjectReference)"
Targets="GetTargetPath"
BuildInParallel="false"
Condition="'%(Name)'=='ConsoleApplication1'" >
<Output TaskParameter="TargetOutputs" ItemName="DependentAssemblies" />
</MSBuild>
</Target>
Please note that same target worked perfectly if it is CSharp project, but failing in Wix project.
Can someone guide how to get ReferencedProjects output dirs in Wix Projects?
Thanks
You can try to see how Wix does it for passing the reference values to candle on build. They're in the wix2010.targets or wix200x.targets file. Unfortunately I don't have the time to really dig into this stuff but the properties these tasks set should still exist to be used in your AfterBuild target.
Just search for "ResolveReferences" in one of those targets files.
You can also just try setting
<OutputPath>somepathhere</OutputPath>
in your csproj file since msbuild is complaining that the property isn't set.
I have a solution with two default projects inside (VisualBasic console applications):
Solution1\
Solution1.sln
ConsoleApplication1\ConsoleApplication1.vbproj
ConsoleApplication2\ConsoleApplication2.vbproj
I have added a custom target to ConsoleApplication1:
...
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
<Target Name="HelloWorld">
<Message Text="Hello Build!"></Message>
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
...
</Project>
I would like to set up a script to execute a normal build on both projects, then call my custom target.
According to: http://msdn.microsoft.com/en-us/library/ms171486.aspx, I should be able to call the specific target with the following command (ignoring the initial build of the projects for now):
msbuild C:\Path\To\Solution1\Solution1.sln /t:ConsoleApplication1:HelloWorld
Instead, I get the following error message:
error MSB4057: The target "ConsoleApplication1:HelloWorld" does not exist in the project. [E:\Path\To\Solution1\Solution1.sln]
I presume I have the syntax incorrect, what would the correct syntax be to accomplish this?
The following commands work as expected:
msbuild C:\Path\To\Solution1\Solution1.sln
msbuild C:\Path\To\Solution1\ConsoleApplication1\ConsoleApplication1.vbproj /t:HelloWorld
Have you considered just adding your custom target action to the AfterBuild target in the desired msbuild proj file? It's a different approach, but would eliminate the need for the explicit invocation of the custom target and still be project file specific.
I want to Publish my WCF Service Library using MSBuild Command Line with VS2012, i don't want to do right click->Publish Website , instead i want to publish it using Command Prompt(MSBuild).
What are the Pre-requisites required for MSBuild?
I don't have windows azure,it is necessary to install windows azure?
I am new to MSBuild, and I would like step by step instructions on how to accomplish this?
I want the .svc file, all the dll's inside the bin folder and web config file to be present inside the published folder.
I found the answer for my question with the reference as below link,
"http://www.asp.net/mvc/tutorials/deployment/visual-studio-web-deployment/command-line-deployment"
using the command line "msbuild C:\ContosoUniversity\ContosoUniversity.sln /p:DeployOnBuild=true /p:PublishProfile=Test"
Your solution is fine and will work to deploy directly out to your host (don't forget to include your build Configuration so it publishes the correct config transform).
Another option is to "publish" your site to a local folder and then upload it to your host separately. This also gives you a chance to archive the site in a zip, do post checks and troubleshoot.
You can do this like so:
<MSBuild Projects="WebProject.csproj"
Targets="Rebuild;_WPPCopyWebApplication"
Properties="WebProjectOutputDir=WebProject\;UseWPP_CopyWebApplication=True;PipelineDependsOnBuild=False;" />
If you get stuck there are more details on publishing WCF, ASP.NET and MVC projects.
You can try with this.
The svc file was manually generated for first time use and then added to the project.
Content Include="*.svc" ---- Change this with the name of the file generated by VS Publish Option.
<ItemGroup>
<Content Include="*.svc">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<WebConfig Include="App.config" />
</ItemGroup>
<Target Name="AfterBuild">
<Copy SourceFiles="#(WebConfig)" DestinationFiles="$(OutDir)web.config" />
</Target>
I am facing an issue with MSBuild I can't overcome it by myself. As a result I rely on community's wisdom.
The real situation I'm having troubles with
I have a soluiton file containing several projects with dependencies to other projects in same solution. I'd like to append a custom target to one of the project's csproj file and build it from the command line. It will allow me to make all the necessary output binaries for this project for further processing during the building of the custom target. But the main thing is that I can't figure out how to do it, googling doesn't help either.
Simplification
To make thing simplier I decided to make a new C# console project, add a simple custom target to the project's file and try to make it build. Still no success! Here what I've done so far:
Created a solution app with a default console project coreapp. This gaves me at least two files:
app.sln
coreapp\coreapp.csproj
Modified coreapp.csproj with addition of my custom target inside of the Project tag
<Target Name="SampleTarget">
<Message Text="This is a SampleTarget" />
</Target>
Run on the command line the following command
%windir%\Microsoft.NET\framework\v3.5\msbuild.exe app.sln /t:coreapp:SampleTarget
or even
%windir%\Microsoft.NET\framework\v3.5\msbuild.exe app.sln /t:coreapp.csproj:SampleTarget
Results
No luck, facing the error
MSB4057: The target "coreapp.csproj:SampleTarget" does not exist in the project.
I suspect that MSBuild thinks somehting fundamentally different from what I want it to think...
BEsides that, I also tried to set on the same command line the environment variable MSBuildEmitSolution=1 to force msbuild dump a temporary solution file it creates while processing the solution. In this file, indeed, no such target. However I guess it isn't the reason because I asked msbuild to build coreapp.proj where target SampleTarget really resides.
The question is how to build SampleTarget in this simplified scenario using solution file since potencially it can contain dependencies for the project containing this SampleTarget target?
I'd be greatful for any sort of help or firection for further investigation!
Instead of inserting a custom target in your project file, you could try creating a new standalone msbuild file, which would:
build the solution file (which builds projects)
defines your extra target
Call it app-custom-Debug.msbuild , for example.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WorkingFolder>$(MSBuildProjectDirectory)</WorkingFolder>
<Configuration>Debug</Configuration>
<SolutionFile>app.sln</SolutionFile>
</PropertyGroup>
<Target Name="Build" DependsOnTargets="Compile" />
<Target Name="Compile">
<Message Text="=== COMPILING $(Configuration) configuration ===" />
<MSBuild Projects="$(SolutionFile)"
Properties="Configuration=$(Configuration)" />
</Target>
<Target Name="SampleTarget">
<Message Text="This is a SampleTarget" />
</Target>
</Project>
Then you execute:
msbuild.exe app-custom-Debug.msbuild /t:SampleTarget
One option is to tie your SampleTarget to the standard Build targets via overriding the appropriate DependsOn property. In this case you could tell BeforeBuild that it DependsOn SampleTarget or you do the same thing with AfterBuild. This will ensure that MSBuild processes your target prior to the standard target indicated.
World's simplest task (see below) is not being executed after I publish my web application project. Any idea why?
<Target Name="AfterPublish">
<Copy SourceFiles="C:\A.txt" DestinationFiles="C:\B.txt" />
</Target>
Note: The following applies to VS2010 and publishing web-application projects with the "Web Deploy" publish method selected in the 'Build/Publish {projectname}' dialog.
Julien Hoarau's correct in that "Publish" is NOT the name of the msbuild target invoked in the above case; the actual target name is "MSDeployPublish".
Therefore, you have to define a "Target" element whose "AfterTarget" attribute's value is set to "MSDeployPublish" - the "Name" attribute's value does not matter (as long as it's unique among target names).
Here's how to do it:
Open the project file (e.g. *.csproj) in a text/XML editor and, just before the closing </Project> tag, add a <Target Name="CustomPostPublishAction" AfterTargets="MSDeployPublish"> element; pick a name of your choice for "CustomPostPublishAction".
Add a so-called Task child element that performs the desired action; for instance, to add a command to be passed to cmd.exe, use an <Exec Command="..." /> element.
Example:
<Target Name="CustomPostPublishActions" AfterTargets="MSDeployPublish" >
<Exec Command="echo Post-PUBLISH event: Active configuration is: $(ConfigurationName)" />
</Target>
Note:
In command strings, use XML entity(?) references in place of characters that would break XML parsing, e.g. ">" in place of "<".
For documentation of the <Target> element in general, see http://msdn.microsoft.com/en-us/library/t50z2hka.aspx
Task-elements reference here: http://msdn.microsoft.com/en-us/library/7z253716.aspx
In general, if you need to determine the name of the msbuild.exe target that is actually invoked by Visual Studio 2010, do the following:
Go to Tools/Options..., Project and Solutions/Build and Run, select 'Detailed' (or, for even more information, 'Diagnostic') from the dropdown list labeled 'MSBuild project build output verbosity.
After running the build/publish action, e.g. Build/Publish, examine the Output window for the last occurrence of the string "Done building target" to determine the top-level target that was invoked.
Visual Studio 2013. Publish Web application to file system.
<Target Name="Moose" AfterTargets="GatherAllFilesToPublish" >
<Message Importance="high" Text="***Moooooooooooooooose***$(WPPAllFilesInSingleFolder)***$(TargetDir)" />
</Target>
Note: Make sure that build logging is set to at least to Detailed. Look for it under Tools -> Options -> Projects and Solutinos -> Build and Run -> MSBuild output verbosity. Diagnostic is also fine if you want to investigate which build target was last run before actual publish.
This seems to work in Visual Studio 2019
<Target Name="MyCustomTarget" AfterTargets="Publish">
<Copy SourceFiles="C:\A.txt" DestinationFiles="C:\B.txt" />
</Target>
You must define override the target at the end of your file, after <Import ... />
Launch MSBuild with detailed verbosity to see why your target is ignored :
msbuild project.csproj /t:Target_to_Launch /v:d
AfterPublish is called after Publish target, but Publish is not the target called when you publish a web application. Publish is the target for publishing ClickOnce application.
You'll have to find the target used when you call Publish in Visual Studio, it could be Package, WebPublish...
I'm a little lazy right now to figure out the mess of targets to find the right one for a file-based publish (which you might be interested in). What you can do in the meantime is defining an AfterBuild target in the *.pubxml file.
<Target Name="AfterBuild">
...
I recommend also turning off the property "DeleteExistingFiles" because if you copy files into the directories being published, it does a clean somewhere during the publishing process.
<DeleteExistingFiles>False</DeleteExistingFiles>