问题
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
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.
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.
Where do I specify this?
In Project Properties | Build Events, I could specify pre-build or post-build events - but isn't this "part of the build" events?
Ideally, if someone could give me the menu sequence in Visual Studio 2010, I'd appreciate it.
回答1:
If you don't want to edit project files - there is another solution.
Each *.csproj file contains this: <Import Project="$(MSBuildToolsPath)\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"
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>
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
Update
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
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
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
...
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="..\myCustom.targets" />
...
</Project>
and
myCustom.targets
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<DestServer>\myserver\final-dest</DestServer>
</PropertyGroup>
</Project>
回答2:
I don't know of a way to set this via the VisualStudio GUI (there might well be none), but you should be able to just edit your .csproj/.vcproj file to add that property to the appropriate configurations:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
...
...etc
...
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DestServer>\myserver\final-dest</DestServer> <!-- <<<<< HERE <<<<< -->
...
Visual Studio will preserve this even if you edit other properties via the GUI (at least it tends to...)
来源:https://stackoverflow.com/questions/8733085/how-do-i-get-visual-studio-2010-to-pass-a-variable-to-msbuild