如何使用MSBuild编译ASP.Net MVC项目?我们使用Continuous Integration服务器来编译和部署我们的应用程序。为了简单起见,我在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命令吗?
答案 0 :(得分:6)
如果编译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>
我没有做任何其他事情而且有效。我实际上创建了我的配置,以便只有发布版本部署应用程序(通过移动PropertyGroups下的MvcBuildViews属性。然后我可以在开发(调试)和部署(发布)中使用相同的.csproj。
答案 1 :(得分:6)
此构建脚本编译asp.net MVC 3应用程序。由于整个互联网似乎已经忘记了“构建脚本”的概念,因此这个概念不需要你在目标机器上安装Visual Studio或“lol,你只需要编辑你的csproj文件来获得msbuild !!” / p>
继续前进。
确保安装了.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替换等。我真的希望这会有所帮助。
答案 2 :(得分:0)
您可以使用具有“msbuild”任务的NAnt,它将为您完成。 NAnt是CI构建的好方法。
NAnt home page NAnt Contrib主页 来自NAnt Contrib的MSBuild task reference
... contrib库添加了一些vanilla NAnt没有的强大功能。这很简单。我在这里添加了我的.build文件片段,以便您了解我是如何使用它的:
<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>
答案 3 :(得分:0)
我发现最简单的方法是将WebDeployment项目添加到您的解决方案中。 http://www.microsoft.com/DOWNLOADS/details.aspx?FamilyID=0aa30ae8-c73b-4bdd-bb1b-fe697256c459&displaylang=en
您可以在WebDeployment项目中设置构建的属性(如预编译)。 Buildserver构建了wdprj。
在我的环境中,我必须首先建立网络。之后我可以启动wdprj。
这是我的脚本。在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>