39

I am running an ASP.NET website from a Windows Server 2008 installation, and I like to edit the pages through the command line since I ssh into the server.

I installed Vim on the server so that I can edit the files easily. If I edit HTML and CSS and .aspx pages, the updates are successful. But if I want to edit source code I would have to rebuild the project. Rebuilding the project recompiles everything nicely and updates the copy on the web. This is a development server so updates to everything is fine since no one sees this server.

How can I build the project through the command line to update the source code and build on the server?

The project is written in C# and the files are all in the wwwroot folder so no file moving needs to occur after a build.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user708774
  • 523
  • 1
  • 5
  • 7

5 Answers5

51

Create a .bat file called: Manual_MSBuild_ReleaseVersion.bat

Put this in the .bat file.

REM you'll have to find the "latest" version of where msbuild.exe resides on your machine.. here are some popular versions/locations
REM set msBuildDir=%WINDIR%\Microsoft.NET\Framework\v2.0.50727
REM set msBuildDir=%WINDIR%\Microsoft.NET\Framework\v3.5
REM set msBuildDir=%WINDIR%\Microsoft.NET\Framework\v4.0.30319
REM set msBuildDir=C:\Program Files (x86)\MSBuild\12.0\Bin
set msBuildDir=C:\Program Files (x86)\MSBuild\14.0\Bin

call "%msBuildDir%\msbuild.exe"  MySolution.sln /p:Configuration=Release /l:FileLogger,Microsoft.Build.Engine;logfile=Manual_MSBuild_ReleaseVersion_LOG.log
set msBuildDir=

You can build a .sln file or a .csproj file. MySolution.sln or MyProject.csproj

See How to: Use MSBuild to Create a Web Package for more information.

You can take it one step further:

rd .\BuildResults /S /Q
md .\BuildResults
rd .\MyProject\Bin\Release  /S /Q

REM you'll have to find the "latest" version of where msbuild.exe resides on your machine.. here are some popular versions/locations
REM set msBuildDir=%WINDIR%\Microsoft.NET\Framework\v2.0.50727
REM set msBuildDir=%WINDIR%\Microsoft.NET\Framework\v3.5
REM set msBuildDir=%WINDIR%\Microsoft.NET\Framework\v4.0.30319
REM set msBuildDir=C:\Program Files (x86)\MSBuild\12.0\Bin
set msBuildDir=C:\Program Files (x86)\MSBuild\14.0\Bin
call "%msBuildDir%\msbuild.exe"  MySolution.sln /p:Configuration=Release /l:FileLogger,Microsoft.Build.Engine;logfile=Manual_MSBuild_ReleaseVersion_LOG.log
set msBuildDir=

XCOPY .\MyProject\Bin\Release\*.* .\BuildResults\

That way, you remove a directory (just to make sure you get a super clean build), create it, build the solution/project and then copy the results of the build to the fresh directory.

Super fresh, every time. And if the build blows up, the \BuildResults directory is empty.

And a subtle little indicator, the datetime of the \BuildResults directory is the last time you built (or tried to build) the solution/project. Subtle, but sometimes helpful.

granadaCoder
  • 26,328
  • 10
  • 113
  • 146
  • 3
    using Visual Studio Command Prompt, i was able to build my solution without creating .bat file. `MSBuild "MySolution.sln" /P:Configuration=Debug` – zulucoda Dec 06 '11 at 14:15
  • granadaCoder actually .bat file is awesome, therefore dont have to keep going to Visual Studio Command Prompt. also nice to have that log file. thanks – zulucoda Dec 07 '11 at 09:14
  • Once you start doing it this way, you usually don't go back. One time setup, repeatable results. – granadaCoder Dec 08 '11 at 21:36
  • lol if Build Blows up!!! watch out that build is very unstable... anyway thanks again for update. – zulucoda Dec 09 '11 at 14:28
  • 3
    another useful setting is `/t:Clean,Build` this will clean up and rebuild `msbuild.exe MySolution.sln /t:Clean,Build` – zulucoda Dec 09 '11 at 14:34
  • 1
    Nice find on the Clean and Build ! – granadaCoder Dec 16 '11 at 15:34
  • I get errors when building this way, but building in visual studio works fine. What gives? – TetraDev Feb 16 '17 at 16:53
  • Do you have nuget packages? If so, manually run a "nuget restore MySolution.sln" before building. You can get nuget.exe from https://dist.nuget.org/index.html. If its not nuget, then you need to look at the log file and figure out specific errors. And probably post a new question about specific errors. – granadaCoder Feb 16 '17 at 18:46
  • This is really great info, except that the "super clean" build part doesn't make sense. If you're building to the same `bin/Release` folder, but never deleting it, how does copying it to a different folder make it cleaner? – jpaugh Jun 01 '17 at 21:24
  • 1
    I added RD .\MyProject\Bin\Release\ /S /Q to the answer to address that. Good point, thanks,. – granadaCoder Jun 02 '17 at 15:23
16

Maybe with this command:

  >> devenv myproject.sln /Build "Release|x86"

You can find devenv in "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe" path.

Andres Rojano Ruiz
  • 1,009
  • 1
  • 11
  • 17
  • Using Visual Studio Command Prompt (Developer Command Prompt) no need to look for the path of the devenv.exe – Tareq Mar 25 '21 at 11:19
9

I used a modification of the answer by @granadaCoder above.

For .NET version 4.5 onwards, there is no corresponding build directory in

%WINDIR%\Microsoft.NET\Framework\

and using MSBuild for an older framework will not let you compile newer constructs like interpolated strings.

TO get around this, you'll need to install the latest MS Build Tools (2015 currently) and use MSBuild.exe in

%programfiles(x86)%\MSBuild\14.0\Bin

Ash
  • 5,786
  • 5
  • 22
  • 42
2

Install the .NET SDK and use the MsBuild.exe command line tool. It's what Visual Studio uses when you build a project or solution.

David M
  • 71,481
  • 13
  • 158
  • 186
  • Not necessarily. At least Visual Studio is able to build some project types that MSBuild cannot (setup projects, for example). You can call `devenv` to compile, though. – Joey Apr 14 '11 at 22:07
  • 1
    I believe msbuild.exe comes with the ~~Framework~~ (only) install, not the ~SDK. See http://www.csharp411.com/where-to-find-msbuild-exe/ . It started to be included with 2.0 and on. Aka, the SDK install may be unnecessary. – granadaCoder Apr 15 '11 at 14:08
  • Joey is correct, msbuild.exe will build ~most applications. Here is an example I've run across where msbuild.exe will NOT work. The ".rulep" is a custom IBM application (project) type and must be built using devenv.exe http://www.ibm.com/developerworks/forums/thread.jspa?messageID=14402518&tstart=0 – granadaCoder Apr 15 '11 at 14:10
  • i installed the .net sdk but the environment variable is still not registering when I type msbuild or msbuild.exe in the cmd – user708774 Apr 17 '11 at 03:02
  • Did you look at or try the .bat file solution listed later in this post? – granadaCoder Apr 19 '11 at 13:32
  • @user708774 You might need to restart your shell. – pensono Nov 15 '19 at 03:00
0

This is another option: https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-build

I've tried this in 7.0 .net sdk.

G:\Other\Sonarr\src>dotnet --version
7.0.107
G:\Other\Sonarr\src>dotnet build
MSBuild version 17.4.4+8e9cb2952 for .NET
...
...
已成功生成。 In English, build successfully.

Code: https://github.com/Sonarr/Sonarr.git

hideDragon
  • 344
  • 2
  • 5