如何自定义网站项目的构建?

时间:2022-09-11 16:35:28

We have some custom MS Build scripts that generates code and configuration files for web application projects when they build. Currently we call these build scripts by overriding the "BeforeBuild" and "AfterBuild" targets in the .csproj file of the web project. However, in another solution I have a web site project which is opened directly from the local IIS and has no .csproj file. How can I customize the build of this type of project?

我们有一些自定义的MS Build脚本,可以在构建时为Web应用程序项目生成代码和配置文件。目前,我们通过覆盖Web项目的.csproj文件中的“BeforeBuild”和“AfterBuild”目标来调用这些构建脚本。但是,在另一个解决方案中,我有一个网站项目,它直接从本地IIS打开,没有.csproj文件。如何自定义此类项目的构建?

If it is not possible to customize the MS Build process of the web site, it would be good enough to invoke something like a .bat file which that would run my custom MS Build script. We are currently using a .bat file but the developers have to manually run it but they sometimes forget this. I can't see a simple way to kick it off automatically when clicking build.

如果无法自定义网站的MS Build过程,那么调用像运行我的自定义MS Build脚本的.bat文件这样的东西就足够了。我们目前正在使用.bat文件,但开发人员必须手动运行它,但有时会忘记这一点。点击构建时,我看不到一种简单的方法来自动启动它。

I would like something that runs as part of the solution build, both in Visual Studio and Team Build.

我希望在Visual Studio和Team Build中作为解决方案构建的一部分运行。

4 个解决方案

#1


4  

Note that unlike with Web Applications, Visual Studio doesn't really ever build Web Sites per se. Of course, there is certainly a Build command that you can run from VS, but all it does is basically check that your site can build without error. The key point is that when you build a Web Site from VS, it does not produce any build artifacts at all. Instead, everything that needs to be built happens at runtime when you send http requests to the site.

请注意,与Web应用程序不同,Visual Studio本身并不构建Web站点。当然,你可以从VS运行一个Build命令,但它所做的一切基本上都是检查你的站点是否可以构建而没有错误。关键点在于,当您从VS构建Web站点时,它根本不会产生任何构建工件。相反,当您向站点发送http请求时,需要构建的所有内容都会在运行时发生。

There are various ways that you can customize how things are built at runtime (e.g. using Build Providers), but if your goal is to generate a web.config file, then this will not help you.

有多种方法可以自定义在运行时构建内容的方式(例如使用Build Providers),但如果您的目标是生成web.config文件,那么这对您没有帮助。

As an alternative, you could potentially have your Web Site take a dependency on another project (e.g. a library), and then have that project perform some custom build actions on behalf of the site (e.g. by modifying the site's files). With the project dependency, you'll be guaranteed that the other project is always built before the Web Site.

作为替代方案,您可能会使您的Web站点依赖于另一个项目(例如库),然后让该项目代表站点执行一些自定义构建操作(例如,通过修改站点的文件)。通过项目依赖性,您可以保证在Web站点之前始终构建其他项目。

#2


3  

Visual Studio < 2012: Web Deployment Projects

Since 2005 Microsoft has released a separate installable project type for Visual Studio called the Web Deployment Project (sometimes referred to as WDP). It was first revealed by Scott Guthrie on his blog, and it hasn't changed much over the years, so his introduction is still a good read to understand in detail what WDP has to offer (or go to the MSDN reference).

自2005年以来,Microsoft已为Visual Studio发布了一个名为Web部署项目(有时称为WDP)的单独可安装项目类型。这是Scott Guthrie在他的博客上首次披露的,多年来它没有太大变化,所以他的介绍仍然是一个很好的阅读,以详细了解WDP提供的内容(或转到MSDN参考)。

When you create a WDP, the project is added to your solution with a built-in reference to an exsiting Web Site project. It's really only an MSBuild file (.wdproj), the contents of which are controlled through a settings dialog that gets added to Visual Studio. When this project is built, it outputs assemblies that are normally generated by the ASP.NET runtime on the server (e.g. on the first request for an ASP.NET file per each directory). In essence, you get what is called a pre-compiled website.

创建WDP时,项目将添加到您的解决方案中,并具有对现有Web站点项目的内置引用。它实际上只是一个MSBuild文件(.wdproj),其内容通过添加到Visual Studio的设置对话框来控制。构建此项目时,它会输出通常由服务器上的ASP.NET运行时生成的程序集(例如,每个目录对ASP.NET文件的第一次请求)。从本质上讲,您将获得所谓的预编译网站。

Because it is based on MSBuild, you can easily add this to your existing build scripts, or add your customizations to the <BeforeBuild> and <AfterBuild> elements (as well as <BeforeMerge> and <AfterMerge>). This should also work on a build server, although some features may require that a Windows SDK is installed. (In my experience aspnet_merge.exe was the main issue, it's responsible for merging all output assemblies into a single assembly if that's configured in the project file.)

因为它基于MSBuild,所以您可以轻松地将其添加到现有构建脚本中,或将自定义项添加到 元素(以及 )。这也适用于构建服务器,但某些功能可能需要安装Windows SDK。 (根据我的经验,aspnet_merge.exe是主要问题,如果在项目文件中配置了它,则它负责将所有输出程序集合并到一个程序集中。)

This solution is the 'official' way to MSBuild a web site project, and is less convoluted than adding empty Class Library projects with dependency to simulate before and after build events. As a downside, it may increase build time when your web site is large. But to end with on a positive note, having pre-compiled websites built for you may improve your overall deployment efforts.

此解决方案是MSBuild网站项目的“官方”方式,并且比在构建事件之前和之后添加具有依赖性的空类库项目更简单。作为缺点,当您的网站很大时,它可能会增加构建时间。但要以积极的方式结束,为您构建预编译的网站可能会改善您的整体部署工作。

Downloads for Visual Studio 2008 and 2010 (sorry, I can't find the one for VS2005, it might be part of SP1 though).

Visual Studio 2008和2010的下载(对不起,我找不到VS2005的版本,但它可能是SP1的一部分)。

Visual Studio 2012

For Visual Studio 2012 the situation has changed. There is no WDP release planned (although there is demand for it), but the new publish features will be supported for Web Site projects, according to this blog post from the web dev tools blog. Publish profiles are MSBuild files as well, so integrating them into you existing build scripts should be possible just as it is with WDP. See also: Visual Studio 2012 Web Deployment Projects are Dead – Long Live Publishing Profiles.

对于Visual Studio 2012,情况已发生变化。根据web开发工具博客的博客文章,计划没有WDP版本(尽管有需求),但网站项目将支持新的发布功能。发布配置文件也是MSBuild文件,因此应该可以将它们集成到现有的构建脚本中,就像使用WDP一样。另请参阅:Visual Studio 2012 Web部署项目已停止 - 长期发布配置文件。

If you are now using both Web Application and Web Site projects, then you may find it beneficial to have the same build setup for both types of projects. In that case, when Publish Profile support for Web Site projects is released in the near future (there's now an ASP.NET and Web Tools 2012.2 RC with support for this), you should definitely switch to that, instead of two separate empty Class Library projects.

如果您现在同时使用Web应用程序和Web站点项目,那么您可能会发现为两种类型的项目设置相同的构建设置是有益的。在这种情况下,当在不久的将来发布对Web站点项目的发布配置文件支持时(现在有一个支持这个的ASP.NET和Web Tools 2012.2 RC),你应该切换到那个,而不是两个单独的空类库项目。

#3


1  

I have now achieved what I need by adding a separate MS Build file in my solution and ensuring it builds before the web site by using the project dependencies in Visual Studio.

我现在已经通过在我的解决方案中添加一个单独的MS Build文件并通过使用Visual Studio中的项目依赖项确保它在网站之前构建来实现我所需要的。

The actual build file is quite minimal and is called "Configure.csproj" and sits in the root of the web site:

实际的构建文件很小,称为“Configure.csproj”,位于网站的根目录中:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" DefaultTargets="Build">
  <Import Project="CodeGeneration.targets" />
  <Target Name="Build">
    <CallTarget Targets="GenerateCode" />
  </Target>
  <Target Name="Rebuild">
    <CallTarget Targets="GenerateCode" />
  </Target>
  <Target Name="Clean">
  </Target>
</Project>

Right-click the solution and choose "Add existing project" and select this file.

右键单击解决方案,然后选择“添加现有项目”并选择此文件。

It seems odd to have this named *.csproj when there is no C# to build, but this file extension is one recognised by Visual Studio when clicking "Add existing project".

当没有构建C#时,将此命名为* .csproj似乎很奇怪,但此文件扩展名是Visual Studio在单击“添加现有项目”时可识别的文件扩展名。

Then to make sure this is built before the web site, right-click the solution and choose "Project dependencies". Select the web site project from the "Projects" drop-down list and then check the box next to "Configure".

然后,要确保在网站之前构建它,请右键单击解决方案并选择“项目依赖项”。从“项目”下拉列表中选择网站项目,然后选中“配置”旁边的框。

It is of course possible to run a custom build after the web site by adding another project and adding a dependency on the web site.

当然可以通过添加另一个项目并在网站上添加依赖项来在网站之后运行自定义构建。

#4


-3  

You could create your own MSBuild proj file for this. Its fairly simple and there is a walkthrough here: http://msdn.microsoft.com/en-us/library/dd576348.aspx

您可以为此创建自己的MSBuild proj文件。它非常简单,这里有一个演练:http://msdn.microsoft.com/en-us/library/dd576348.aspx

You can then just add this to the file you create for your post build commands.

然后,您可以将其添加到为post构建命令创建的文件中。

<Target Name="AfterEndToEndIteration">
  <Exec Command="YourPath\YourCommand.bat" />
</Target>

Good Luck

祝你好运

#1


4  

Note that unlike with Web Applications, Visual Studio doesn't really ever build Web Sites per se. Of course, there is certainly a Build command that you can run from VS, but all it does is basically check that your site can build without error. The key point is that when you build a Web Site from VS, it does not produce any build artifacts at all. Instead, everything that needs to be built happens at runtime when you send http requests to the site.

请注意,与Web应用程序不同,Visual Studio本身并不构建Web站点。当然,你可以从VS运行一个Build命令,但它所做的一切基本上都是检查你的站点是否可以构建而没有错误。关键点在于,当您从VS构建Web站点时,它根本不会产生任何构建工件。相反,当您向站点发送http请求时,需要构建的所有内容都会在运行时发生。

There are various ways that you can customize how things are built at runtime (e.g. using Build Providers), but if your goal is to generate a web.config file, then this will not help you.

有多种方法可以自定义在运行时构建内容的方式(例如使用Build Providers),但如果您的目标是生成web.config文件,那么这对您没有帮助。

As an alternative, you could potentially have your Web Site take a dependency on another project (e.g. a library), and then have that project perform some custom build actions on behalf of the site (e.g. by modifying the site's files). With the project dependency, you'll be guaranteed that the other project is always built before the Web Site.

作为替代方案,您可能会使您的Web站点依赖于另一个项目(例如库),然后让该项目代表站点执行一些自定义构建操作(例如,通过修改站点的文件)。通过项目依赖性,您可以保证在Web站点之前始终构建其他项目。

#2


3  

Visual Studio < 2012: Web Deployment Projects

Since 2005 Microsoft has released a separate installable project type for Visual Studio called the Web Deployment Project (sometimes referred to as WDP). It was first revealed by Scott Guthrie on his blog, and it hasn't changed much over the years, so his introduction is still a good read to understand in detail what WDP has to offer (or go to the MSDN reference).

自2005年以来,Microsoft已为Visual Studio发布了一个名为Web部署项目(有时称为WDP)的单独可安装项目类型。这是Scott Guthrie在他的博客上首次披露的,多年来它没有太大变化,所以他的介绍仍然是一个很好的阅读,以详细了解WDP提供的内容(或转到MSDN参考)。

When you create a WDP, the project is added to your solution with a built-in reference to an exsiting Web Site project. It's really only an MSBuild file (.wdproj), the contents of which are controlled through a settings dialog that gets added to Visual Studio. When this project is built, it outputs assemblies that are normally generated by the ASP.NET runtime on the server (e.g. on the first request for an ASP.NET file per each directory). In essence, you get what is called a pre-compiled website.

创建WDP时,项目将添加到您的解决方案中,并具有对现有Web站点项目的内置引用。它实际上只是一个MSBuild文件(.wdproj),其内容通过添加到Visual Studio的设置对话框来控制。构建此项目时,它会输出通常由服务器上的ASP.NET运行时生成的程序集(例如,每个目录对ASP.NET文件的第一次请求)。从本质上讲,您将获得所谓的预编译网站。

Because it is based on MSBuild, you can easily add this to your existing build scripts, or add your customizations to the <BeforeBuild> and <AfterBuild> elements (as well as <BeforeMerge> and <AfterMerge>). This should also work on a build server, although some features may require that a Windows SDK is installed. (In my experience aspnet_merge.exe was the main issue, it's responsible for merging all output assemblies into a single assembly if that's configured in the project file.)

因为它基于MSBuild,所以您可以轻松地将其添加到现有构建脚本中,或将自定义项添加到 元素(以及 )。这也适用于构建服务器,但某些功能可能需要安装Windows SDK。 (根据我的经验,aspnet_merge.exe是主要问题,如果在项目文件中配置了它,则它负责将所有输出程序集合并到一个程序集中。)

This solution is the 'official' way to MSBuild a web site project, and is less convoluted than adding empty Class Library projects with dependency to simulate before and after build events. As a downside, it may increase build time when your web site is large. But to end with on a positive note, having pre-compiled websites built for you may improve your overall deployment efforts.

此解决方案是MSBuild网站项目的“官方”方式,并且比在构建事件之前和之后添加具有依赖性的空类库项目更简单。作为缺点,当您的网站很大时,它可能会增加构建时间。但要以积极的方式结束,为您构建预编译的网站可能会改善您的整体部署工作。

Downloads for Visual Studio 2008 and 2010 (sorry, I can't find the one for VS2005, it might be part of SP1 though).

Visual Studio 2008和2010的下载(对不起,我找不到VS2005的版本,但它可能是SP1的一部分)。

Visual Studio 2012

For Visual Studio 2012 the situation has changed. There is no WDP release planned (although there is demand for it), but the new publish features will be supported for Web Site projects, according to this blog post from the web dev tools blog. Publish profiles are MSBuild files as well, so integrating them into you existing build scripts should be possible just as it is with WDP. See also: Visual Studio 2012 Web Deployment Projects are Dead – Long Live Publishing Profiles.

对于Visual Studio 2012,情况已发生变化。根据web开发工具博客的博客文章,计划没有WDP版本(尽管有需求),但网站项目将支持新的发布功能。发布配置文件也是MSBuild文件,因此应该可以将它们集成到现有的构建脚本中,就像使用WDP一样。另请参阅:Visual Studio 2012 Web部署项目已停止 - 长期发布配置文件。

If you are now using both Web Application and Web Site projects, then you may find it beneficial to have the same build setup for both types of projects. In that case, when Publish Profile support for Web Site projects is released in the near future (there's now an ASP.NET and Web Tools 2012.2 RC with support for this), you should definitely switch to that, instead of two separate empty Class Library projects.

如果您现在同时使用Web应用程序和Web站点项目,那么您可能会发现为两种类型的项目设置相同的构建设置是有益的。在这种情况下,当在不久的将来发布对Web站点项目的发布配置文件支持时(现在有一个支持这个的ASP.NET和Web Tools 2012.2 RC),你应该切换到那个,而不是两个单独的空类库项目。

#3


1  

I have now achieved what I need by adding a separate MS Build file in my solution and ensuring it builds before the web site by using the project dependencies in Visual Studio.

我现在已经通过在我的解决方案中添加一个单独的MS Build文件并通过使用Visual Studio中的项目依赖项确保它在网站之前构建来实现我所需要的。

The actual build file is quite minimal and is called "Configure.csproj" and sits in the root of the web site:

实际的构建文件很小,称为“Configure.csproj”,位于网站的根目录中:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" DefaultTargets="Build">
  <Import Project="CodeGeneration.targets" />
  <Target Name="Build">
    <CallTarget Targets="GenerateCode" />
  </Target>
  <Target Name="Rebuild">
    <CallTarget Targets="GenerateCode" />
  </Target>
  <Target Name="Clean">
  </Target>
</Project>

Right-click the solution and choose "Add existing project" and select this file.

右键单击解决方案,然后选择“添加现有项目”并选择此文件。

It seems odd to have this named *.csproj when there is no C# to build, but this file extension is one recognised by Visual Studio when clicking "Add existing project".

当没有构建C#时,将此命名为* .csproj似乎很奇怪,但此文件扩展名是Visual Studio在单击“添加现有项目”时可识别的文件扩展名。

Then to make sure this is built before the web site, right-click the solution and choose "Project dependencies". Select the web site project from the "Projects" drop-down list and then check the box next to "Configure".

然后,要确保在网站之前构建它,请右键单击解决方案并选择“项目依赖项”。从“项目”下拉列表中选择网站项目,然后选中“配置”旁边的框。

It is of course possible to run a custom build after the web site by adding another project and adding a dependency on the web site.

当然可以通过添加另一个项目并在网站上添加依赖项来在网站之后运行自定义构建。

#4


-3  

You could create your own MSBuild proj file for this. Its fairly simple and there is a walkthrough here: http://msdn.microsoft.com/en-us/library/dd576348.aspx

您可以为此创建自己的MSBuild proj文件。它非常简单,这里有一个演练:http://msdn.microsoft.com/en-us/library/dd576348.aspx

You can then just add this to the file you create for your post build commands.

然后,您可以将其添加到为post构建命令创建的文件中。

<Target Name="AfterEndToEndIteration">
  <Exec Command="YourPath\YourCommand.bat" />
</Target>

Good Luck

祝你好运