MSBuild: OutputPath directory is empty

2019-07-17 19:25发布

问题:

I want to deploy my ASP.NET MVC site and have the following script.

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="..\MyProjName\MyProjName.csproj"/>
  <PropertyGroup>
    <NewInstallDir>C:\DeployFolder\</NewInstallDir>
    <BinDir>$(NewInstallDir)bin\</BinDir>
  </PropertyGroup>
  <Target Name="Build">
    <MSBuild Projects="..\MySlnName.sln"
        Properties="Configuration=Release;Platform=Any CPU;OutputPath=$(BinDir)" />
    <Copy SourceFiles="@(Content->'..\MyProjName\%(RelativeDir)%(FileName)%(Extension)')"
        DestinationFiles="@(Content->'$(NewInstallDir)%(RelativeDir)%(FileName)%(Extension)')" />
    <Copy SourceFiles="@(None->'..\MyProjName\%(RelativeDir)%(FileName)%(Extension)')"
        DestinationFiles="@(None->'$(NewInstallDir)%(RelativeDir)%(FileName)%(Extension)')" />
    <MakeDir Directories="@(Folder->'$(NewInstallDir)%(RelativeDir)')" />
  </Target>
</Project>

Main idea.

I copied binary to C:\DeployFolder (take structure of folder from sources). I build my dll to C:\DeployFolder\Bin (I don't have this folder in sources folder so I need separately copy this).

I run my script - all works instead of copy DLLs to OutputPath. Same scripts works for other asp.net mvc project. I have no idea what is wrong in this case.

I complete this issue with workaround but I would like to know what is wrong with this script.

回答1:

The first thing I'd try is to replace your use of the deprecated $(OutputPath) with $(OutDir). From when I've seen this error, 9 times out of 10 it is due to a mismatch between the Platform/Configuration requested and how a particular project is actually defined. Take care to keep track of the discrepency between "Any CPU" (with a space) preferred by solution files and "AnyCPU" actually used inside project files for $(Platform). Some project wizards only set up an "x86" Platform or otherwise omit "AnyCPU" which can cause the OutputPath to be empty.

Beyond that, the approach of importing a project file and then building a solution (presumbably that includes the very same project" is a bit off center. Consider making the deployment changes you desire within the project file itself, through an import. You can either wire into the existing build targets at the right place, or perhaps add an additional target.