MSBuild seems to execute target "BeforeBuild" each time, also as if the library project itself does not need to build.
What do you mean the msbuild execute the target each time also if the library project itself does not need to build? As I know, BeforeBuild
is one necessary target during the build process, so if you build one project, it will always call this target. If the library project doesn't need to build, what's the reason you call msbuild to build it? More details could be better :)
I'd like to configure the build process to only run the PowerShell
script if the library needs to (re)build.
Is there a way to do this?
I tried to use targets "BeforeBuild" and "BeforeCompile". But it seems
to execute every time.
BeforeBuild
and BeforeCompile
are predefined targets in msbuild system. You can overwrite them to customize your build steps, but you can't avoid running them during build process. So they'll execute every time if the build starts, it's expected behavior by design.
According to your description, you may overwrite these targets and run the power shell script in them. That's why in your machine it will always call the ps script.
Here's a workaround if you build the project by msbuild.exe instead of VS IDE:
Assuming you have overwritten BeforeTarget
in your project file. You can add a condition to determine if to run the custom BeforeTarget
(call ps script) or original BeforeTarget
.
Define a property named RunPS
, and add a condition to the BeforeTarget you use in this way:
<PropertyGroup>
<RunPS>false</RunPS>
</PropertyGroup>
<Target Name="BeforeBuild" Condition="$(RunPS)=='true'">
<!--Run the ps script here-->
</Target>
Then you can control if use the default BeforeTarget or custom one by setting the value of RunPS
.
msbuild xx.csproj /p:RunPS=true
to run ps script during build process, msbuild xx.csproj
to run original BeforeTarget
And if you build the project in VS IDE,you can create new Configuration(CustomDebug) copied from Debug or Release, and set Condition="$(Configuration)=='CustomDebug'"
to the target.
Update:
For the workaround which works in VS and command-line:
See this document, in VS we can create new configurations easily by copying settings from Debug and Release Configurations.
1.In my opinion, for your library project, you have normal Debug and Release Configurations, you can follow tips in the document to create corresponding custom configurations. Copy the setting from Debug and create a new configuration named PSDebug, copy the setting from Release and create a new configuration named PSRelease.
2.Then edit the xx.csproj, add a condition in this format:
<Target Name="BeforeBuild" Condition="'$(Configuration)'=='PSDebug' OR '$(Configuration)'=='PSRelease'">
<!--Run the ps script here-->
<Message Text="showsth" Importance="high" />
</Target>
Then in VS IDE, if you build with normal Configuration Debug or Release
, it will use default BeforeBuild
target. And only when you build with custom PSDebug or PSRelease
, it will call your custom BeforeBuild
target.In VS, you can easily switch between those configurations by this box:
And for command-line, it also works. If you use command like msbuild xx.csproj /p:Configuration=Debug
, it use predefined BeforeBuild target to do this, and if you use command like msbuild xx.csproj /p:Configuration=PSDebug
, it will call your custom target and run the PS script.