MsBuild failing to build package, but okay with "just" building or building package after a "normal" build - visual-studio

I have a problem with building a Web Deployment Package from a Web Application Project (within a solution containing multiple projects, as well as multiple web applications).
This actually works
I can build the project just fine from the command line if I use this command for example:
msbuild D:\PathTo\Solution\Project\Project.csproj
/fl /flp:logfile="D:\buildadventures\Build.log";errorsonly;verbosity=diagnostic
/p:SolutionDir="D:\PathTo\Project\\";Configuration=Release;Platform=AnyCpu
But this does not work
But when I try the same command just a bit differently to build a deployment package for me like that:
msbuild D:\PathTo\Solution\Project\Project.csproj
/fl /flp:logfile="D:\buildadventures\Build.log";errorsonly;verbosity=diagnostic
/T:Package
/p:SolutionDir="D:\PathTo\Solution\\";Configuration=Release;Platform=AnyCpu;
PackageLocation="D:\buildadventures\Project.zip";
AutoParameterizationWebConfigConnectionStrings=false
...it fails miserably, spitting out hundreds of errors of the "The type or namespace name '' does not exist"-kind
And why does it work this way?
The strange thing however is, the second command I posted works fine if the first one was executed prior to that.
I suppose the tasks executed when doing a "normal" build are different to those that are executed when building a package, now I wonder in what way they are different.
What symptomatically seems to cause it
I noticed that in the project file of the project I want to build a package of contains a custom section towards the end:
<PropertyGroup>
<PreBuildEvent>
cscript $(ProjectDir)SvnRevision\svnrevision.vbs $(ProjectDir) $(ProjectDir)Version.cs
nuget install "$(ProjectDir)packages.config" -o "$(SolutionDir)Packages"
</PreBuildEvent>
</PropertyGroup>
This seems to be responsible for that difference; if I do a "normal" build I can see that after that a new "Packages" directory was created in my solution folder.
However, as you might have guessed, that directory is missing when I try to do the package creation command. I also do not run into this problem if I let Visual Studio create the package for me.
Maybe I do have to change the project file or passed properties to carry over this behavior for my package creation, too?
Or Is there maybe a way to force a "normal" build and then just append package creation to that somehow?

Silly me.
I was able to circumvent this issue by just calling multiple targets in my msbuild command such as that:
msbuild D:\PathTo\Solution\Project\Project.csproj
/fl /flp:logfile="D:\buildadventures\Build.log";errorsonly;verbosity=diagnostic
/t:Build;Package
/p:SolutionDir="D:\PathTo\Solution\\";Configuration=Release;Platform=AnyCpu;
PackageLocation="D:\buildadventures\Project.zip";
AutoParameterizationWebConfigConnectionStrings=false

Related

Build specific Visual studio project under project solution using Msbuild and devenv

I have Visual studio project solution which has multiple .csproj. Each .csproj has some reference libraries.The project settings are made in such a way the reference libraries are built first and then .csproj is built. This works as expected when i run it in visual studio IDE. But when i try to execute using msbuild i'm getting an error saying target doesn't exist. Gone through many posts related to this issue ,tried possible things.But didn't built.Looks like i might be doing something silly or missing something in the settings.
Also tried using devenv from commandline. With this option i dont see any error but at same time the project doesnt build.I dont see any message after execution of command.Im using visual studio 2015
Here is my project structure
Poject.sln
ProjectA
porjectB
projectC
Libraries
libA
libB
msbuild "project.sln" target:"D:\Projects\Source\Demo\ProjectA\ProjectA.csproj" /t:build
"/p:Configuration=Debug" "/p:platform=x86"
I see the below error
"D:\project.sln" (D:\Projects\Source\Demo\ProjectA\;build target) (1) ->
D:\project.sln.metaproj : error MSB4057: The target "D:\Projects\Source\Demo\ProjectA" does not exist in the project. [D:\project.sln]
Here is the command used using devenv
devenv.exe "project.sln" /build Debug /project `"D:\Projects\source\Demo\Applications\ProjectA\ProjectA.csproj" /projectconfig Debug
After executing the above its doesnt build and i dont see any error too.
error MSB4057: The target "D:\Projects\Source\Demo\ProjectA" does not
exist in the project.
The error indicates your path in command is not valid and project.sln can't recognize the path. So you actually meet one path-related issue. And you should pass the ProjectA to the targets argument instead of ProjectA.csproj! More details see tip3 in For MSBuild.
For MSBuild:
1.If you're only trying to build ProjectA and its reference libraries.
Navigate(cd) to path where ProjectA.csproj exists, and then use command msbuild ProjectA.csproj /t:build /p:Configuration=Debug /p:platform=x86
Also you can directly use command msbuild AbsolutePath\ProjectA.csproj /t:build /p:Configuration=Debug /p:platform=x86. It's not necessary to use " to cover the path and arguments.
(ProjectA.csproj file should have definitions about the reference to those two library projects, so msbuild ProjectA.csproj will build those two projects first. You don't need to specify the xx.sln in your command.)
2.If you're trying to build whole solution(all the projects):
msbuild project.sln /t:build /p:Configuration=xxx /p:platform=xxx
Navigate to solution folder when you run above command, or use absolutepath\project.sln with that command.
3.When you want to build specific projects(more than one) in solution:
Check How to: Build Specific Targets in Solutions By Using MSBuild.exe. Since you're only build ProjectA, you don't need to use this format. For example: Only when you need to build both ProjectA and ProjectB, but not build ProjectC... You can use command like:
msbuild xxx.sln /t:NotInSlnfolder:Build;NewFolder\InSolutionFolder:Build
Pay attention to the path when you use this format. Whether your project is in solution folder can affect the build result a lot ! And, the direct cause of your issue, this command's targets argument needs one ProjectName as input instead of ProjectName.csproj.
For Devenv command:
1.I always use VS2017 and VS2019,so I'm not certainly sure if VS2015's devenv related command has big difference from VS2017's or VS2019's. But according to details from this VS2017 document:
Commands that begin with devenv are handled by the devenv.com utility, which delivers output through standard system streams, such as stdout and stderr.
Alternatively, commands that begin with devenv.exe can use the same switches, but the devenv.com utility is bypassed. Using devenv.exe directly prevents output from appearing on the console.
I think that's why you don't see any message after execution of command. You should use devenv.com command to see the output in console. And it's by design that devenv.exe will prevents output from appearing on the console.
2.The following command builds the project CSharpWinApp, using the Debug project build configuration within MySolution.
devenv "%USERPROFILE%\source\repos\MySolution.sln" /build Debug /project "CSharpWinApp\CSharpWinApp.csproj" /projectconfig Debug
More details about devenv reference please check this document.
In addition:
1.Looks like you have one strange project structure. I checked your error message above and it seems your several projects(xx.csproj) are not under Solution(xx.sln) folder. Just a suggestion, the normal folder structure for VS2015 is always:
Solution folder
xx.sln
ProjectA folder
ProjectA.csproj
ProjectB folder
ProjectB.csproj
2.For most of the projects, build using msbuild is more powerful and convenient than build using devenv. So if you don't have special reason, I recommend using Msbuild, the build engine of VS.
Hope all above helps to resolve your issue and puzzle. Let me know if your issue persists:)
File "/Users/morel893/Desktop/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "projects_project" does not exist
LINE 1: ...ct"."technology", "projects_project"."image" FROM "projects_...

Build using specified startup project

I am running the build command like this:
set MSBuildParams=/m:16 /target:Rebuild
/property:Configuration=""Release"";Platform=""x64"" msbuild
%MSBuildParams% C:\path\to\the\sln\Solution.sln
I'm using the following msbuild version:
Microsoft (R) Build Engine version 14.0.25420.1
Solution.sln contains 2 projects,
let's say Project1 and Project2.
Project1 is set as the startup project.
After I build the Solution.sln using this setup, I would like to
rebuild it, but this time using Project2 as the startup project.
Is there a way to do that, without changing the Solution.sln?
What would be the best practice to accomplish that?
You could use the specific command line to build/rebuild the specific project using MSbuild like the following case.
specify project file of a solution using msbuild
But we have to change certain files if you want to change the start up project without using the VS IDE, since the setting was stored into the ".SUO" file.
Actually there is no setting in ".SLN" file for startup project even if you don't want to change it,
In addition, the start up project was the running project of the solution, maybe you don't have to change it:
https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/b6347dce-8449-4cbb-a606-7b19407a1026/how-do-i-set-the-startup-project-in-the-sln-file?forum=vcgeneral

.Net Core 2.0 "Generate Nuget package on build" issue with Post-build events

The new .NET Core 2.0 projects provide and easy and convenient way to create Nuget Package from the project output. Just click on the "Generate Nuget package on build" check box and it is done.
It works fine but I have an issue with Post-build events.
I want to copy all of the packages from a solution after each build to a specific folder. So I use the "Post-build event command line" with a script:
xcopy "$(ProjectDir)$(OutDir)..*.nupkg" "$(SolutionDir)..\WebServicePracticesNuget\" /Y /I
And sometimes it works fine sometimes not at all. So far my investigation concluded that the Nuget package creation is not part of the build process itself. So the script (sometimes) will triggered before the package was generated and it is unpredictable. My solution is to add some delay. Unfortunately "timeout x" is not working with Post-build events. So I used the fallback option:
ping 127.0.0.1 -n 4 > NUL
Which makes it almost reliable (~95%) but I think it is "poor man's" solution. And looks ridiculous in a Post-build event script. I have already reported this issue to the VS team. But not much comments or solution so far.
So my question is: does anybody have the same issue? Or any idea for a better solution then I have now?
Thanks.
The GeneratePackageOnBuild feature executes the Pack target after Build so the post build event will potentially run before the NuGet packages have been created. In VS 15.3+, when you create a post-build event, it will create a Target element in the project file. You can change the AfterTargets attribute on it to AfterTargets="Pack" to run after packing and not after the core .net build. But it is a bit of a fragile approach.
The pack target will respect the PackageOutputPath msbuild property, which is what dotnet pack's --output parameter would set.
Since xcopy only works on windows, the most versatile solution would be to use msbuild to set up the property during the build.
For example you could put a Directory.Build.props file next to the solution file (directory above all projects) with the following contents:
<Project>
<PropertyGroup>
<PackageOutputPath>$(MSBuildThisFileDirectory)nupkgs</PackageOutputPath>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
</Project>
This file will be automatically imported into all project files in the directory hierarchy blow this file and set the target directory for the generated .nupkg files to a folder named nupkgs next to the Directory.Build.props file. It also enables the "generate package build" feature for all projects that support it ("sdk-based" projects like .net standard / .net core libraries) so you don't have to set it up in VS or edit all project files.
You can configure msbuild task order inside the project file.

Running post deployment script after Web Deploy

I have the following problem and I'm surprised that I can't find any straightforward solution on SO or MSDN.
I have existing *.pubxml profiles in several of my web applications and I would like to execute post deployment script - powershell script - which reorganizes WebSite and its child applications slightly.
I'm not usign Web Deployment Package - just Web Deploy.
The script is deployed successfully but the problem is - how should I execute it automatically after deployment?
I have two scenarios:
Execute by simply "Publish..." from Visual Studio.
Execute as part of TFS Build definition (TFS 2013).
You can try to define a “Target” by MSBuild to achieve your requirement.
For the first scenario:
The Visual Studio build process is defined by a series of MSBuild .targets files that are imported into your project file. One of these imported files, Microsoft.Common.targets. This file contains a set of predefined empty targets that are called before and after some of the major targets in the build process.
So you can define a "Target" element whose "AfterTarget" attribute's value is set to "MSDeployPublish":
<Target Name="CustomPostPublishActions" AfterTargets="MSDeployPublish" >
<Exec Command="..\PostDeploymentScript.sp1 " />
</Target>
For the second scenario:
You can add a PowerShell build task as MrHinsh`s suggestion.
You should switch to deploying from Build & Release only in VSTS/TFS.
You can then add a PowerShell build task and either point at a script or use Inline if it's short. If it is a script that you use in many builds you might want to write your own build task.

TFS build - deployment/package target does not run

We have a TFS build definition set up where we pass the following extra MSBuild arguments in:
/p:DeployOnBuild=true;DeployTarget=PipelinePreDeployCopyAllFilesToOneFolder;_PackageTempDir="\\server\build";AutoParameterizationWebConfigConnectionStrings=false
This has been detailed elsewhere as a way to have the published files copied to a specific location instead of generating a deploy package.
This unfortunately does not work on our build server, however if I run the exact same msbuild command line as called by TFS on my dev machine then it works perfectly and copies the output files to the location.
I have checked the log file and there is no errors, it just seems to completely skip the publish/deploy step.
Done building target "_BuiltWebOutputGroupOutput" in project "xyz.csproj".
Target "PrepareForRun" in file "c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets" from project "C:\Builds\2\xyz\xyz build\Sources\xyz.Web\xyz.Web.csproj" (target "CoreBuild" depends on it):
whereas on my local machine, after _BuiltWebOutputGroupOutput target is run the package target runs and deploys the files correctly.
I have tried using different paths and even setting the properties in the project file but it seems to make no difference. My local solution and project files are the same as in the repository that the TFS build is using. Is there something config related on our build server or with the build agent that would cause the packaging target not to run?
I was having a similar problem today and found a fix so it maybe worth a look for you. Here

Resources