使用MSBuild Tools调用csproj项目文件发布网站时$(SolutionDir)宏参数值丢失为空的解决方案

使用Visual Studio打开解决方案,对<网站项目>右键点击<发布>,一切都是正常的,所有宏都可用,宏参数值也是正确的。

而通过批处理脚本命令调用MSBuild.exe对解决方案编译,一切也都是正常的,所有宏都可用,宏参数值也是正确的。

但如果你通过批处理脚本命令调用MSBuild.exe对解决方案下某个Web项目进行发布操作时,你会发现,某些针对解决方案可用的宏变得不正常,不可用,宏参数值都是错误的。

例如$(SolutionDir)在某些版本的MSBuild下它的值是"",某些版本的MSBuild下它的值是".."。

那么,这个有解决方法吗?答案是有的。

MSBuild有个/p命令选项,支持外部传值重置宏参数值。

在批处理命令里添加“/p:SolutionDir=path”选项,即可让$(SolutionDir)重新变为正常可用。

下面是我某个项目中提供给CI调用的发布/部署脚本文件代码:

echo Publish parameters initializing...

::These parameters are not used for the time being
::set DotNetFrameworkPath=%windir%Microsoft.NETFramework
::if exist %windir%SysWOW64 set DotNetFrameworkPath=%windir%Microsoft.NETFramework64

:Set user level's parameters
set SolutionPath=%~dp0..src
set SolutionName=Demo
set SolutionFile=%SolutionPath%%SolutionName%.sln
set ProjectName=WebTestset ProjectFile=%SolutionPath%%ProjectName%1621.%ProjectName%.csproj
set PublishProfile=WebTestDeployToRelease
set IfUseNuGetReStore=Y % value is Y or N %
set IfUpdateNuGetTool=Y % value is Y or N %

:Set system level's parameters
set Configuration=Release
set LogLevel=normal
:: Note: That the MSBuild tool version and VisualStudio version and the TargetFramework version have dependencies
set VisualStudioVersion=14.0
set TargetFrameworkVersion=4.6.1
set MicrosoftSdkVersion=v10.0A

set NuGetPath=%SolutionPath%.nuget
set NuGetExe=%NuGetPath%NuGet.exe
set NuGetArgs=restore "%SolutionFile%"

set AspnetMergePath="C:Program Files (x86)Microsoft SDKsWindows\%MicrosoftSdkVersion%inNETFX %TargetFrameworkVersion% Tools"
set MSBuildPath=C:Program Files (x86)MSBuild\%VisualStudioVersion%Bin
set MSBuildExe=%MSBuildPath%MSBuild.exe
set MSBuildArgs=/p:Configuration=%Configuration%;VisualStudioVersion=%VisualStudioVersion%;TargetFrameworkVersion=%TargetFrameworkVersion%;SolutionDir=%SolutionPath% /p:DeployOnBuild=true;PublishProfile=%PublishProfile% /p:AspnetMergePath=%AspnetMergePath% /verbosity:%LogLevel% /l:FileLogger,Microsoft.Build.Engine;encoding=utf-8;append=true;logfile=build\%ProjectName%_%Configuration%_Publish.log

echo Publish parameters initialize completed.

if %IfUseNuGetReStore% == Y (
if %IfUpdateNuGetTool% == Y (
echo NuGet tools Start updating...
"%NuGetExe%" update -Self
echo NuGet tools update completed.
) else (
echo Not update NuGet tools.
)
echo NuGet Start ReStoreing...
"%NuGetExe%" %NuGetArgs%
echo NuGet ReStore completed.
) else (
echo Not use NuGet tools.
)

echo Start publishing...
"%MSBuildExe%" %MSBuildArgs% "%ProjectFile%"
echo Publish completed.

感谢伟大的stackoverflow论坛,给我提供了很多帮助。

答案来自于:

https://stackoverflow.com/questions/15053915/how-to-get-rid-of-solutiondir-when-building-vs-project-from-outside-visual

原文地址:https://www.cnblogs.com/VAllen/p/how-to-get-rid-of-solutiondir-when-building-vs-project-from-outside-visual.html