开发者

Copy files with MsBuild

I have a solution with two projects. One of them is a simple project that doesn't depend on any special assemblies to build. However, the application is allowed to launch an external executable (that I also own -- this is the second project of my solution). When I build the project, I would like the executable (+ dependencies such as app.config) of the external project to be copied under the target directory of my project.

How can I achieve this with MsBuild? My first idea was to add a reference to the executable (even though I don't need it to build) to copy the file. This seem a b开发者_如何学运维it hacky to me.

Thanks!

P.S. Another question: Let's say that I can tell MsBuild to copy the executable. What if the executable isn't build? Can I force it to build?


In your case you should make your MSBuild script replace your solution and declare the desired dependency there:

<Project DefaultTargets="Executable_A">
    <Target Name="Executable_A" DependsOnTargets="Executable_B">
        <MSBuild Projects="Executable_A.proj" Targets="Build" />
    </Target>

    <Target Name="Executable_B">
        <MSBuild Projects="Executable_B.proj" Targets="Build">
            <Output TaskParameter="TargetOutputs" ItemName="AssembliesBuilt" />
        </MSBuild>
        <Copy SourceFiles="@(AssembliesBuilt)" DestinationFolder="C:\My_Target_Path\" SkipUnchangedFiles="true" />
    </Target>
</Project>

... and you can instruct the target of your second project to copy its output to the desired location. You could even instruct your first project to store its output path into a variable to use when copying the output of your second project (DestinationFolder).


Have you looked the MSBuild Copy Task: Copy Task ?


If you will be satisfied with @Filburt answer I can suggest to improve this solution.

If you'll take a look how default Build target works:

 <Target
    Name="Build"
    Condition=" '$(_InvalidConfigurationWarning)' != 'true' "
    DependsOnTargets="$(BuildDependsOn)"
    Returns="$(TargetPath)" />

You'll see that the only output from build or rebuild target is $(TargetPath). $(TargetPath) is a full path to the built assembly. To change this behaviour you can hack build process. But it will be match simpler to define your own target in project B:

<Target
    Name="BuildWithConfig"
    DependsOnTargets="Clean;Build"
    Returns="$(TargetPath);$(TargetPath).config" />

In A project just define AfterBuild target:

<Target Name="AfterBuild">
    <MSBuild Projects="Executable_B.proj" Targets="BuildWithConfig">
        <Output TaskParameter="TargetOutputs" ItemName="AssembliesBuilt" />
    </MSBuild>
    <Copy SourceFiles="@(AssembliesBuilt)" DestinationFolder="$(OutDir)\SubDirForB" SkipUnchangedFiles="true" />   
</Target>

This solution doesn't require to create custom build script. You will get the result directly in VS.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜