none
issue with msbuild target calling msbuild with another msbuild target RRS feed

  • Question

  • I have a msbuild process, which is split into two sections. The first analyzes nuget packages and creates some files. Since it can only analyze nuget packages after the restore it has be called after "CoreCompile" has happened. The second part should embedd those files in the application as embedded resources and there lies my issue. The embedding part has to happen before the "PrepareForBuild" target, but after "CoreCompile" has happened.
    My solution for that was to start another build after the restore has happened.
    This looks like the following:

     
     <Project>
          <PropertyGroup>
            <GivenDirectory>some directory</GivenDirectory>
        	<Variable>false</Variable>    
        	<Variable2>false</Variable2>
          </PropertyGroup>  
        
        
        	<Target Name="SomeTarget" AfterTargets="CoreCompile" Condition="'$(Variable)' == 'true'">		
        		<MakeDir Directories="$(GivenDirectory)"/>
        		<!--this consoletool generates files in the GivenDirectory-->
        		<Exec Command="$(ConsoleTool)" />
        		
        
        		<!--this calls the msbuild with the other target-->
        		<MSBuild Properties="Variable2=true"  Targets="EmbeddFiles" Projects="$(MSBuildProjectFullPath)" />       
        	</Target>
        	
        	<Target Name="EmbeddFiles" BeforeTargets="PrepareForBuild" Condition="'$(Variable2)' == 'true'">
        		<ItemGroup>
        		  <EmbeddedResource Include="GivenDirectory\*.*"/>         
        		</ItemGroup>
        	</Target>
        	
        </Project>  



    This works fine to the point, that the embedded resources are not added to my application.
    I can call it with

        msbuild .\myapp.csproj /t:rebuild /Variable=true

    and I can see that both targets are executed the way I wanted them, but somehow the embedded resource is not added.

    If I comment the first target out the embeddedresources are added though. What did I do wrong?

    I tried to do it like in this thread.
    Thursday, October 10, 2019 1:10 PM

Answers

  • Hi msbuildamateur,

    Welcome to the MSDN forum.

    As your description, why did you want to generate the file after corecompile event and then  embed the generated file into the program before the PrepareForBuild event?

    Besides, why did you also operate this csproj file in a nested msbuild? This can lead to an infinite loop.

    The corecompile event and the restore are not the same concept.When you execute Build process, it will first perform restore nuget package then prepareforbuild then  Corecompile. So you don't need to nest msbuild. 

    > >You can try:

      <Target Name="SomeTarget" BeforeTargets="PrepareForBuild" Condition="'$(Variable)' == 'true'">
        <MakeDir Directories="$(GivenDirectory)"/>
        <!--this consoletool generates files in the GivenDirectory-->
        <Exec Command="$(ConsoleTool)" />
        <Message Text="$(Variable2)" Importance="high"></Message>
        <!--this calls the msbuild with the other target-->
    
      </Target>
    
    
      <Target Name="EmbeddFiles" AfterTargets="SomeTarget">
        <ItemGroup>
          <EmbeddedResource Include="GivenDirectory\*.*"/>
        </ItemGroup>
        <Message Text="$(Variable2)" Importance="high"></Message>
      </Target>

    Any feedback will be expected.

    Best Regards,

    Perry



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Friday, October 11, 2019 9:38 AM

All replies

  • Hi msbuildamateur,

    Welcome to the MSDN forum.

    As your description, why did you want to generate the file after corecompile event and then  embed the generated file into the program before the PrepareForBuild event?

    Besides, why did you also operate this csproj file in a nested msbuild? This can lead to an infinite loop.

    The corecompile event and the restore are not the same concept.When you execute Build process, it will first perform restore nuget package then prepareforbuild then  Corecompile. So you don't need to nest msbuild. 

    > >You can try:

      <Target Name="SomeTarget" BeforeTargets="PrepareForBuild" Condition="'$(Variable)' == 'true'">
        <MakeDir Directories="$(GivenDirectory)"/>
        <!--this consoletool generates files in the GivenDirectory-->
        <Exec Command="$(ConsoleTool)" />
        <Message Text="$(Variable2)" Importance="high"></Message>
        <!--this calls the msbuild with the other target-->
    
      </Target>
    
    
      <Target Name="EmbeddFiles" AfterTargets="SomeTarget">
        <ItemGroup>
          <EmbeddedResource Include="GivenDirectory\*.*"/>
        </ItemGroup>
        <Message Text="$(Variable2)" Importance="high"></Message>
      </Target>

    Any feedback will be expected.

    Best Regards,

    Perry



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Friday, October 11, 2019 9:38 AM
  • Hi friend,

    Any update for this issue?

    It's not recommended that we call msbuild when building another project. At least that's not good suggestion for your situation. Like what Perry suggests, cause Restore nuget target is always the first one to run, so there's no need to execute the logic after CoreCompile target. The order of these targets is :

    Restore=>PrepareForBuild=>CoreCompile, see:

    So you don't need to separate it into two sections. In this situation we don't need to call msbuild target, just follow the answer in this issue,  and write your logic in that target, and it will work.

    Note: Change the msbuild log verbosity to Detailed and we can find the build order in Output window.

    Best Regards

    Lance


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 15, 2019 10:01 AM
  • Sorry for the late reply,

    was ill for the past week and couldn't wait to get back to work to try it out and see if it works. 

    And it works exactly as you described, I changed my first target to aftertargets=restore and boom my embedded resources were included as expected.

    A thousand thanks for that :)


    Friday, October 18, 2019 5:41 AM