如何将构建后事件限制为仅为一种类型的构建运行?

我正在使用事件将DLL文件复制到本地IIS虚拟目录,但我不希望在发布模式下的构建服务器上发生这种情况。


当前回答

截至2022年,我已经找到了两个解决方案。在我的特定情况下,我想打包到不同的目录,这取决于Configuration。

选项1

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="if $(Configuration) == Debug (dotnet pack --no-build -o ~/../../../../../nuget-repo/debug -p:PackageVersion=$(VersionInfo)) else (dotnet pack --no-build -o ~/../../../../../nuget-repo -p:PackageVersion=$(VersionInfo))" />
</Target>

选项2

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Condition="'$(Configuration)' == 'Debug'" Command="dotnet pack --no-build -o ~/../../../../../nuget-repo/debug -p:PackageVersion=$(VersionInfo)" />
    <Exec Condition="'$(Configuration)' == 'Release'" Command="dotnet pack --no-build -o ~/../../../../../nuget-repo -p:PackageVersion=$(VersionInfo)" />
</Target>

我更喜欢第二种选择。

其他回答

我发现我可以在项目文件中添加多个条件,就像这样:

  <Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition=" '$(Configuration)' != 'Debug' AND '$(Configuration)' != 'Release' ">
      <Exec Command="powershell.exe -ExecutionPolicy Unrestricted -NoProfile -NonInteractive -File $(ProjectDir)postBuild.ps1 -ProjectPath $(ProjectPath) -Build $(Configuration)" />
  </Target>

与任何项目设置一样,构建事件可以在每个Configuration中配置。只需在“属性页”对话框的下拉菜单中选择您想要更改的配置,并编辑后构建步骤。

截至2022年,我已经找到了两个解决方案。在我的特定情况下,我想打包到不同的目录,这取决于Configuration。

选项1

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="if $(Configuration) == Debug (dotnet pack --no-build -o ~/../../../../../nuget-repo/debug -p:PackageVersion=$(VersionInfo)) else (dotnet pack --no-build -o ~/../../../../../nuget-repo -p:PackageVersion=$(VersionInfo))" />
</Target>

选项2

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Condition="'$(Configuration)' == 'Debug'" Command="dotnet pack --no-build -o ~/../../../../../nuget-repo/debug -p:PackageVersion=$(VersionInfo)" />
    <Exec Condition="'$(Configuration)' == 'Release'" Command="dotnet pack --no-build -o ~/../../../../../nuget-repo -p:PackageVersion=$(VersionInfo)" />
</Target>

我更喜欢第二种选择。

生成前和生成后事件作为批处理脚本运行。您可以在$(ConfigurationName)上执行条件语句。

例如

if $(ConfigurationName) == Debug xcopy something somewhere

这在Visual Studio 2015中为我工作。

我将所有DLL文件从位于与我的解决方案文件夹同一级别的库文件夹中的文件夹复制到正在构建的项目的目标目录。

使用项目目录中的相对路径,用..\..\lib两步往上走文件夹结构

MySolutionFolder ……。 自由

if $(ConfigurationName) == Debug (
xcopy /Y "$(ProjectDir)..\..\lib\*.dll" "$(TargetDir)"
) ELSE (echo "Not Debug mode, no file copy from lib")