How do I add a reference to a project with an Exec Target

John Work Daintree 0 Reputation points
2023-09-08T10:03:08.08+00:00

I have a dotnet project with the following source:

// See https://aka.ms/new-console-template for more information
String msg = "Hello,World!!!";
Console.WriteLine(msg);

and this .csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <ItemGroup>
    <ProjectReference Include="sub\APLGenerics.csproj" />
  </ItemGroup>

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>


It references a 2nd project in a subdirectory, with the following .csproj:

<Projct>

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

        <Target Name="APLGenerics">
                <Exec Command="THIS WILL FAIL"/>
        </Target>

</Project>

If I run:

dotnet build sub

I get the error:

error MSB3073: The command "THIS WILL FAIL" exited with code 9009.

Because the exec command has been executed and has obviously failed.

But, If I run:

dotnet build

There is no such output, indicating that the exec command has NOT been executed.

How do I get the Target to be built in the referenced project?

Thanks.

Community Center | Not monitored
Developer technologies | C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Johan Smarius 470 Reputation points MVP
    2023-09-08T10:44:33.8966667+00:00
    0 comments No comments

  2. Tianyu Sun-MSFT 34,441 Reputation points Microsoft External Staff
    2023-09-08T13:51:09.2466667+00:00

    Hello @John Work Daintree ,

    Welcome to Microsoft Q&A forum.

    In short:

    Try to modify your 2nd project’s project file to

    <Project Sdk="Microsoft.NET.Sdk"> 
       
    	<PropertyGroup>     
    		<TargetFramework>net6.0</TargetFramework>   
    	</PropertyGroup>    
    
    	<Target Name="APLGenerics" AfterTargets="Build">          
    		<Exec Command="THIS WILL FAIL"/>   
    	</Target>  
    
    </Project>
    

    Delete the bin and obj folders(in sub folder) and rerun the command(dotnet build).

    Several aspects:

    1. The 2nd project’s project file looks use the format of the new SDK-style project, you need to add the Sdk attribute like <Project Sdk="Microsoft.NET.Sdk"> instead of only using <Project>. Otherwise, your 2nd project will not be successfully referenced, as it misses project information.
    2. You have defined the Target in your 2nd project, and without Sdk attribute, it ran correctly separately. But if you add Sdk attribute, it will not run successfully. You need to try to specify the target order and run your customized Target by adding, for example, AfterTargets="Build". More details see SDK-style projects.
    3. After modifing the .csproj file, please try to delete the bin and obj folders(in sub folder), then run the command(dotnet build) again.

    Sincerely,

    Tianyu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.