1

We have a nuget package we rely on but it has a .targets file that looks like this

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <Reference Include="PackageName">
      <HintPath>$(MSBuildThisFileDirectory)..\..\sdk\net45\PackageName.dll</HintPath>
    </Reference>
  </ItemGroup>
  <PropertyGroup>
    <PostBuildEvent>
pushd $(MSBuildThisFileDirectory)..\..\sdk\net45\
for /d %%i in (PackageName*) do (
  xcopy "%%i" "$(TargetDir)%%i\" /S /Y
)
popd
    </PostBuildEvent>
  </PropertyGroup>
</Project>

We do not need nor want the copying of 400+ files to happen upon build, so how can we prevent this specific PostBuildEvent? We have been able to define

<PropertyGroup> 
    <PostBuildEvent>    
    </PostBuildEvent>   
  </PropertyGroup>

in our csproj but that eliminates all PostBuildEvents, whereas there may be a nuget package in the future where we want to obey its PostBuildEvent, we just dont want to obey this specific package's

We also want to keep the ItemGroup hints if possible, but if needed we can add them to our csproj

CuriousDeveloper
  • 849
  • 2
  • 8
  • 27

1 Answers1

0

Prevent nuget PostBuildEvent

If you do not want to eliminates all PostBuildEvents, you could temporarily disable build events by setting them from the command line to an empty string:

msbuild foo.sln /p:PreBuildEvent= /p:PostBuildEvent=

Of course, if we want to finally solve this issue, we still have to back the nuget package. You could remove the .targets file from the project file .csproj. To accomplish this, unload your project, find the code of the importing .targets file, like:

<Import Project="..\packages\xxx\Build\xxx.targets" Condition="Exists('..\packages\xxx\Build\xxx.targets')" />

With this method, you need add ItemGroup hints to your csproj file, if you want to keep it.

Alternatively, you can remove this build event from the nuget package. Download the NuGet Package Explorer from Microsoft Store, open that nuget package, double click the .targets file and edit it, remove the build event:

enter image description here

Then save this to the local feed, install the nuget package from local feed.

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135