简体   繁体   中英

msbuild other projects and copy output to current project

Here is my code:

<ItemGroup>
    <ProjectsToBuild Include="$(ProjectDir)..\proj1.jsproj"/>
    <ProjectsToBuild Include="$(ProjectDir)..\proj2.jsproj"/>
    <!-- ... -->
  </ItemGroup>
  <PropertyGroup>
    <Config>Release</Config>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)' == 'Debug'">
    <Config>Debug</Config>
  </PropertyGroup>
    <Target Name="Build">
        <MSBuild Projects="@(ProjectsToBuild)" 
          ContinueOnError ="false"
          Properties="Configuration=@(Config)">
          <Output ItemName="OutputFiles" TaskParameter="TargetOutputs"/>
        </MSBuild>
        <!-- Need to copy single generated js file in each projects to current project (ie:mainProj\autogenerated folder) -->
    </Target>

So, proj1.jsproj generates proj1.js single file, proj2.jsproj generates proj2.js single file,...

Then I need to copy those files to:

$(ProjectDir)\\autogenerated\\proj1\\proj1.js

$(ProjectDir)\\autogenerated\\proj2\\proj2.js

...

Thanks in advance for your help.

This can be achieved in a number of ways, here's one which uses metadata to declare a destination directory, combines build and copy into one target and loops over the ItemGroup calling that target:

<ItemGroup>
  <ProjectsToBuild Include="$(ProjectDir)..\proj1.jsproj">
    <DestDir>proj1</DestDir>
  </ProjectsToBuild>
  <ProjectsToBuild Include="$(ProjectDir)..\proj2.jsproj">
    <DestDir>proj2</DestDir>
  </ProjectsToBuild>
</ItemGroup>

<Target Name="Build">
  <MSBuild Projects="$(MsBuildThisFile)" Targets="BuildAndCopy"
    Properties="ProjectToBuild=%(ProjectsToBuild.Identity);
                DestDir=$(ProjectDir)\autogenerated\%(ProjectsToBuild.DestDir)" />
</Target>

<Target Name="BuildAndCopy">
  <MSBuild Projects="$(ProjectToBuild)" Targets="Build" >
    <Output ItemName="OutputFiles" TaskParameter="TargetOutputs"/>
  </MSBuild>
  <Copy SourceFiles="@(OutputFiles)" DestinationFolder="$(DestDir)"/>
</Target>

Now if your projects are really named like that and your directory structure is like that things can be simplified: the only different part for every project is the name, so you could simply use

<ItemGroup>
  <ProjectsToBuild Include="proj1">
  <ProjectsToBuild Include="proj2">
</ItemGroup>

and then derive all other paths from it.

Another way is importing a common MsBuild file in every project, and make the common file declare a postbuild event to copy $(OutputFile) to $(ProjectDir)\\autogenerated\\$(ProjectName)\\$(OutputFile) or something like that; I'm not familiar with js projects so the names might be wrong but you get the idea. This removes the need for an extra MsBuild file just to build projects.

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