【问题标题】:How do I compile an ASP.Net MVC project using MSBuild如何使用 MSBuild 编译 ASP.Net MVC 项目
【发布时间】:2011-01-12 20:00:06
【问题描述】:

如何使用 MSBuild 编译 ASP.Net MVC 项目?我们使用持续集成服务器来编译和部署我们的应用程序。为了简单起见,我在 VS2008 中创建了一个 MVC 1.0 项目。我立即创建了一个 MSBuild 脚本文件来编译它。我没有更改项目中的任何代码。 MSBuild 脚本包含以下目标。

   <AspNetCompiler
      VirtualPath="/"
       PhysicalPath="C:\Development\mvc1\"
        TargetPath="c:\publish\xxx"
        Force="true"
        Debug="false" 
 Updateable="true"

MVC 项目 sln 文件包含在 c:\development\mvc1\ 目录中。我正在运行 XP/Pro。

我收到错误 ASPCONFIG:在应用程序级别之外使用注册为 allowDefintion='MachineToApplication' 的部分是错误的。我从 Web 配置文件中删除了身份验证模式、成员资格提供程序等,直到我终于看到不同的错误信息。我现在收到一条错误消息,指出文件“/views/shared/site.master”不存在。

发生了什么事?提前感谢您的帮助!

我是否使用了错误的 MSBuild 命令?

【问题讨论】:

  • 你使用的是什么 CI 服务器?
  • 问题不在于 ci 服务器。我还没有把它添加到混合中。我正在尝试创建一个 MSBuild 脚本,它将在我的开发机器上编译应用程序。一旦成功,我将尝试在我们的 ci 服务器上实现它。我们使用 TeamCity

标签: asp.net asp.net-mvc msbuild


【解决方案1】:

如果你编译你的 sln 文件 (msbuild mysolution.sln) 或

<MSBuild Projects="msbuild mysolution.sln" Targets="Rebuild" ContinueOnError="false"
StopOnFirstFailure="false" /><!-- -d -errorstack -->

sln 文件有 ASP.NET MVC 项目 .csproj 文件,然后 .csproj 文件有你需要的一切。用记事本打开.csproj,查找:

1) 这应该是真的:

<MvcBuildViews>false</MvcBuildViews>

2) 目标名称="AfterBuildCompiler":

  <Target Name="AfterBuildCompiler" Condition="'$(MvcBuildViews)'=='true'">
    <AspNetCompiler VirtualPath="SomeVirtualDir" PhysicalPath="C:\Development\mvc1\" TargetPath="c:\publish\xxx\" />
  </Target>

我没有做任何其他事情,它奏效了。我实际上进行了配置,以便仅发布构建部署应用程序(通过将 MvcBuildViews-property 移动到 PropertyGroups 下。然后我可以在开发(调试)和部署(发布)中使用相同的 .csproj。

【讨论】:

    【解决方案2】:

    此构建脚本编译一个 asp.net MVC 3 应用程序。由于整个互联网似乎已经忘记了“构建脚本”的概念,所以这个不需要你在目标机器上安装 Visual Studio 或“大声笑,你只需要编辑你的 csproj 文件来获取 msbuild !!”

    继续前进。

    确保您已安装 .NET 4 和 MVC3。顺便说一句,我的构建脚本仅适用于 msbuild 4,因此请确保您使用的是正确的。

    大致流程如下(感谢我在这里得到的许多提示和答案!)

    1) Build the dependencies (you DLL's)
    2) Build the DLL for your web application.
    3) Call the asp.net compiler task.
    4) Check the scripts for additional comments.
    

    请注意,这是从编译其他 DLL(业务、数据访问等)的外部脚本调用的

    <Project ToolsVersion="4.0" DefaultTargets="build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
            <PropertyGroup>
            <BuildDir>..\..\dist</BuildDir>
            <Optimize>true</Optimize>
        </PropertyGroup>
        <ItemGroup >
            <Reference Include="System.dll" />
            <Reference Include="System.Core.dll" />
            <Reference Include="System.Web.Abstractions.dll" />
    
    <!-- add the remaining DLL's required. Check your References folder inside VS2010 and add the relevant entries here. It's a lot of references. I ommited them to make the post more compact.
    For reasons that are beyond me, I only managed to get some DLL's referenced by full path. Go figure... -->
    
            <Reference Include="C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.Helpers\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.Helpers.dll" />
            <Reference Include="C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll" />
            <Reference Include="C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.dll" />
    
    <!-- The "main build script" compiles the other DLL's from the project and places them on the BuildDir folder. Just reference it here-->
            <Reference Include="$(BuildDir)\*.dll"></Reference>
        </ItemGroup>
    
    <!-- Build a DLL for the code file inside your web project (controllers, models, the lot...) place it together with the other DLL's 
    WARNING: Simple build command. Resource files are not included in this.
    -->
    
        <Target Name="BuildWebDll">
            <ItemGroup>
                <CodeFiles Include=".\**\*.cs" />
            </ItemGroup>
            <CSC Sources="@(CodeFiles)" TargetType="Library" References="@(Reference)" OutputAssembly="$(BuildDir)\cth.web.dll" >
            </CSC>      
        </Target>
    
    <!-- For reasons also unkown, but covered in a number os posts in this forum, the asp.net compiler requires the necessary DLL's to be placed on the BIN/ folder of your web project. That's why we're copying every DLL we need to said folder. For debugging, check the Bin folder on Visual Studio after you compile the project. You need to replicate that in your BIN/
    -->
        <Target Name="CopyDLLs">
            <ItemGroup>
                <DllFiles Include="$(BuildDir)/*.dll"/>
            </ItemGroup>
            <Copy SourceFiles="@(DllFiles)" DestinationFolder="Bin\"></Copy>
        </Target>
    
        <Target Name="build">
            <CallTarget Targets="BuildWebDll"></CallTarget>
            <CallTarget Targets="CopyDLLs"></CallTarget>
    
            <!-- Call this from the webproject directory. PhysicalPath references ".". TargetPath can be everything you want -->
            <AspNetCompiler Updateable="true" VirtualPath="/CTH.Web" PhysicalPath="./"  TargetPath="$(BuildDir)/CTH.Web" Force="true" Debug="false"    />
        </Target>
    

    请记住,您必须包含资源文件、进行任何 web.config 替换等。我真的希望这会有所帮助。

    【讨论】:

    • 那么如何将视图、内容等放入“build”目录?你如何让发布工作?
    • Richard,与此同时,我改变了构建 MVC 项目的方式。检查这篇文章:stackoverflow.com/questions/12283701/…。它将构建和发布分为两个不同的阶段。
    【解决方案3】:

    我发现的最简单的方法是将 WebDeployment 项目添加到您的解决方案中。 http://www.microsoft.com/DOWNLOADS/details.aspx?FamilyID=0aa30ae8-c73b-4bdd-bb1b-fe697256c459&displaylang=en

    您在 WebDeployment 项目中设置构建的属性(如 precompile )。 Buildserver 构建 wdprj。

    在我的环境中,我必须首先构建网络。之后我可以启动 wdprj。

    这是我的 nant - 脚本。在 msbuild 中编写相同的内容应该很容易。它实际上在 TeamCity 中运行。

    xml version="1.0"?>
    <project name="GreatProjectWeb"
     default="build"  basedir="."
     xmlns="http://nant.sf.net/release/0.85/nant.xsd">
    
    
     <description>Build Script</description>
     <!-- builds only the csproj, not the entire solution-->
     <target name="build" description="Compile the project using Debug configuration for more verbose error descriptions">
     <echo message="Building..."> </echo>
    
     <exec program="C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe"    >  
    
     <arg value="GreatProjectWeb\GreatProjectWeb.csproj" />
     <arg value="/t:Build" />
      <arg value="/p:Configuration=Release" />
     </exec>
      <echo message="Building Projektfile finished. Starting WDP Project..."> </echo>
    
      <exec program="C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe"    >  
    
     <arg value="GreatProjectWeb_Build\GreatProjectWeb_Build.wdproj" />
     <arg value="/t:Build" />
      <arg value="/p:Configuration=Release" />
     </exec>
    
       <exec program="7z"    >  
     <arg value="a" />
     <arg value="GreatProjectWeb_Deploy\web_GreatProject.zip" />
      <arg value="GreatProjectWeb_Deploy\*" />
     </exec>
    
     </target>
    
    </project>
    

    【讨论】:

      【解决方案4】:

      你可以使用 NAnt,它里面有一个“msbuild”任务,它会为你做这件事。 NAnt 是进行 CI 构建的好方法。

      NAnt home page NAnt Contrib主页 来自 NAnt Contrib 的 MSBuild task reference

      ...contrib 库添加了一些原始 NAnt 所没有的强大功能。这很简单。我在此处包含了我的 .build 文件的 sn-p,因此您可以看到我是如何使用它的:

      <property name="DeployDestination" value="\\MyTestServerName\DestinationFolder"/>
      <property name="Solution.Configuration" value="Debug" overwrite="True" />
      <property name="nant.settings.currentframework" value="net-3.5" />
      <if test="${WebContentDestination=='Production'}">
          <property name="DeployDestination" value="\\MyProductionServer\DestinationFolder"/>
      </if>
      
      ...<snip>
      
      <target name="Build">
          <msbuild project="SolutionFileName.sln">
              <arg value="/p:Configuration=${Solution.Configuration}" />
          </msbuild>
      </target>
      
      <target name="Deploy">
          <copy todir="${DeployDestination}" flatten="true" >
              <fileset>All files to copy</fileset>
          </copy>
      </target>
      

      【讨论】:

      • 如果您使用 CruiseControl.Net,他们的页面上有一些教程。如果您使用 TeamCity 相同的东西。您使用的是什么构建服务器?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多