简体   繁体   中英

Conditionally Copy Dependencies MSBuild

We have our solution set up so that Project A References Project B, Project B references Project C, and Project C has a .NET wrapper that references native DLLs.

A -> B -> C -> Native DLLs via .NET Wrapper

The reason for the convolution is we are dealing with 3rd party hardware, or we would reference the DLLs straight from project B. Anyway --

We are trying to get some files (native DLLs) that are referenced in project C, copied over to project A. Copy local with build action of "Content" does not work because MSBuild will fail saying that it cannot overwrite the files as once they start they launch, the processes do not clean up after themselves and remain running.

  1. Is it possible to have MSBuild attempt to kill processes before building?

    OR

  2. Is it possible to conditionally copy the native DLLs from project B to project A if they do not exist with MSBuild? Project A's bin folder is where they launch from, and this is where the build fails (not from project C to B).

It sounds like your main problem is that your application doesn't clean up after itself. If your application is starting the processes, it should stop them when it is closed. You should call Process.Kill() on any processes that you've started before your application is closed.

Having said that, both 1) and 2) are possible:

1) You can call any executable from msbuild using the Exec task . You can use this task to call the "taskkill" command.

 <Target Name="BeforeBuild">
    <Exec Command="taskkill /IM ProcessToKill.exe /F"/>
</Target>

2) The msbuild Copy task has a "SkipUnchangedFiles" parameter that will only copy files if the files are newer than the existing files:

<Target Name="AfterBuild">
    <Copy
        SourceFiles="@(DllLocation)"
        DestinationFolder="@(DllLocation->'$(OutputDir)%(RecursiveDir)%(Filename)%(Extension)')"
        SkipUnchangedFiles="true"
    />
</Target>

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