如何让 Visual Studio 2010 将变量传递给 MSBuild? [英] How do I get Visual Studio 2010 to pass a variable to MSBuild?

查看:41
本文介绍了如何让 Visual Studio 2010 将变量传递给 MSBuild?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个自定义的 msbuild deploy.targets 文件,该文件允许我将生成的二进制文件发布到我在命令行中指定的目录中.这意味着如果我运行

I have built a custom msbuild deploy.targets file that allows me to publish the resulting binaries to a directory that I specify on the command line. What that means is that if I run

$>msbuild/p:DestServer=\myserver\final-dest

$>msbuild /p:DestServer=\myserver\final-dest

然后我的项目将被编译,生成的 *.dlls 将被复制到一个临时实例 - 在这种情况下是 myserver 上的目录 final-dest.我想要这个功能,因为当我做一个好的编译时,我想要该目录中 *.dlls 的副本,但我也想要它们是本地的.

then my project will be compiled, and the resulting *.dlls will be copied to a staging instance - in this case the directory final-dest on myserver. I want this functionality because when I do a compile for good, I want a copy of the *.dlls in that directory, but I also want them local.

这是我的问题 - 我真的不想从命令行发出这个问题.当我选择发布构建配置(项目 | 属性 | 构建)时,我希望能够指定/p:DestServer=\myserver\final-dest 作为 msbuild 将使用 作为它的参数正常构建.

Here's my issue - I'd really like to not have to issue that from the command line. When I choose the Release Build Configuration (Project | Properties | Build), I'd like to be able to specify the /p:DestServer=\myserver\final-dest as an argument that msbuild would use as it does its normal build.

在哪里指定?

在项目属性中 |构建事件,我可以指定构建前或构建后事件 - 但这不是构建的一部分"事件吗?

In Project Properties | Build Events, I could specify pre-build or post-build events - but isn't this "part of the build" events?

理想情况下,如果有人能给我 Visual Studio 2010 中的菜单序列,我将不胜感激.

Ideally, if someone could give me the menu sequence in Visual Studio 2010, I'd appreciate it.

推荐答案

如果您不想编辑项目文件 - 还有另一种解决方案.

If you don't want to edit project files - there is another solution.

每个 *.csproj 文件都包含以下内容:<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>.

Each *.csproj file contains this: <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />.

在我的电脑和 .Net 4.0 上,此文件位于 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.targets"

On my pc and .Net 4.0 this file is located in "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.targets"

Project 中添加新的 PropertyGroup 如:

Add new PropertyGroup inside Project like:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
...
 <PropertyGroup>  
    <DestServer>\myserver\final-dest</DestServer>
  </PropertyGroup>
...
<!-- a lot of stuff -->
...
</Project>

您现在的所有构建(来自 MSBuild 和 Visual Studio)都将使用此属性,因此您无需修改​​ .csproj 或从命令行传递参数

All your builds now (either from MSBuild and Visual Studio) will use this property, so you don't need to modify .csproj or to pass args from Command Line

更新

这是一种快速而肮脏的方法,因为它将覆盖在该 PC 上构建的所有项目.只有在有很多项目并且您没有修改它们的权限时才这样做

That was a quick-and-dirty way to go, since it will override all projects building on that PC. Only do this, if there are a lot of projects and you have no permissions to modify them

通常您只需在单个项目中添加一个新的 PropertyGroup.

Normally you just add a new PropertyGroup inside single project.

如果有很多项目,你需要这个变量,你可以创建和导入你的自定义扩展目标下面默认目标:

If there are a lot of projects, where you need this variable, you can create and import your custom extension targets bellow default targets:

App1.csproj

App1.csproj

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
...
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <Import Project="..\myCustom.targets" />
...
</Project>

myCustom.targets

myCustom.targets

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>  
    <DestServer>\myserver\final-dest</DestServer>
  </PropertyGroup>
</Project>

这篇关于如何让 Visual Studio 2010 将变量传递给 MSBuild?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆