简体   繁体   中英

MSBuild target before build

I have project A that need to insert a TextId.cs before build. And, there is project B. TextId.cs would generated after project B is compiled and executed.

Now I'd like to integrate the compile and execute in Directory.Build.targets in project A. It is not worked as I expect. TextId.cs will generate but the build would still failed as no TextId.cs if I set BeforeTargets="BeforeBuild" as below.

Anyone knows that which target is OK? or, any other solution?

<Project>
    <ItemGroup>
        <ProjectReferences Include="c:\code\textidfilegenerator\*.*proj" />
    </ItemGroup>
    <Target Name="BuildOtherProjects">
        <Message Importance="High" Text="-----------------------" />
        <MSBuild
            Projects="@(ProjectReferences)"
            Targets="Build">
        </MSBuild>
    </Target>
    
    <Target Name="CopyText" DependsOnTargets="BuildOtherProjects" BeforeTargets="BeforeBuild">
        <Message Importance="High" Text="**********************" />
        <Exec Command="C:\Code\TextIdFileGenerator\bin\Debug\net6.0\TextIdFileGenerator.exe C:\Code\Sys1500TestDriver\TextProvider\TextIds.cs" IgnoreExitCode="true"/>
    </Target>
</Project>

Before you will dive into the comments below, go through these docs:

<Project>
    <ItemGroup>
        <ProjectReferences Include="c:\code\textidfilegenerator\*.*proj" /> <!--you should use relative path like <ProjectReference Include="../**/*.csproj or absolute with MSBuild well-known properties" />-->
    </ItemGroup>
    <Target Name="BuildOtherProjects"> <!--you don't need this target if you have project reference-->
        <Message Importance="High" Text="-----------------------" />
        <MSBuild
            Projects="@(ProjectReferences)"
            Targets="Build">
        </MSBuild>
    </Target>
    
    <Target Name="CopyText" DependsOnTargets="BuildOtherProjects" BeforeTargets="BeforeBuild"><!-- 'BeforeBuild' will not work because TextIdFileGenerator.exe needs to be created before you will do anything with it so you should use 'AfterTargets="Build"'-->
        <Message Importance="High" Text="**********************" />
        <Exec Command="C:\Code\TextIdFileGenerator\bin\Debug\net6.0\TextIdFileGenerator.exe C:\Code\Sys1500TestDriver\TextProvider\TextIds.cs" IgnoreExitCode="true"/><!-- again, you should use relative paths or absolute ones with combination of MSBuildProjectDirectory and OutDir -->
    </Target>
</Project>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM